Skip to content

Commit

Permalink
fix: move permission creation to a migration. (#2120)
Browse files Browse the repository at this point in the history
* fix: move permission creation to a migration.

* Apply suggestions from code review

Co-authored-by: Grant Eben <[email protected]>

* remove role seeder from test cases.

---------

Co-authored-by: Grant Eben <[email protected]>
  • Loading branch information
wreality and gmeben authored Jul 8, 2024
1 parent 330f7ae commit bc6feac
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$roles = Role::getArrayOfAllRoleNames();
foreach ($roles as $key => $role) {
Role::factory()->create([
'id' => $key + 1,
'name' => $role,
]);
}

Permission::create(['name' => Permission::UPDATE_USERS])
->assignRole(Role::APPLICATION_ADMINISTRATOR);
Permission::create(['name' => Permission::UPDATE_USERS_IN_OWN_PUBLICATION])
->assignRole(Role::PUBLICATION_ADMINISTRATOR);
Permission::create(['name' => Permission::CREATE_PUBLICATION])
->assignRole(Role::APPLICATION_ADMINISTRATOR);
Permission::create(['name' => Permission::VIEW_ALL_PUBLICATIONS])
->assignRole(Role::APPLICATION_ADMINISTRATOR);

Permission::create(['name' => Permission::ASSIGN_REVIEWER])
->assignRole(Role::APPLICATION_ADMINISTRATOR)
->assignRole(Role::PUBLICATION_ADMINISTRATOR)
->assignRole(Role::EDITOR);

Permission::create(['name' => Permission::UNASSIGN_REVIEWER])
->assignRole(Role::APPLICATION_ADMINISTRATOR)
->assignRole(Role::PUBLICATION_ADMINISTRATOR)
->assignRole(Role::EDITOR);

Permission::create(['name' => Permission::ASSIGN_REVIEW_COORDINATOR])
->assignRole(Role::APPLICATION_ADMINISTRATOR)
->assignRole(Role::PUBLICATION_ADMINISTRATOR)
->assignRole(Role::EDITOR);

Permission::create(['name' => Permission::UNASSIGN_REVIEW_COORDINATOR])
->assignRole(Role::APPLICATION_ADMINISTRATOR)
->assignRole(Role::PUBLICATION_ADMINISTRATOR)
->assignRole(Role::EDITOR);

Permission::create(['name' => Permission::ASSIGN_EDITOR])
->assignRole(Role::APPLICATION_ADMINISTRATOR)
->assignRole(Role::PUBLICATION_ADMINISTRATOR);

Permission::create(['name' => Permission::UNASSIGN_EDITOR])
->assignRole(Role::APPLICATION_ADMINISTRATOR)
->assignRole(Role::PUBLICATION_ADMINISTRATOR);

Permission::create(['name' => Permission::UPDATE_SITE_SETTINGS])
->assignRole(Role::APPLICATION_ADMINISTRATOR);
}


/**
* Reverse the migrations.
*/
public function down(): void
{
}
};
1 change: 0 additions & 1 deletion backend/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class DatabaseSeeder extends Seeder
public function run()
{
$this->call([
RoleSeeder::class,
UserSeeder::class,
PublicationSeeder::class,
StyleCriteriasSeeder::class,
Expand Down
11 changes: 6 additions & 5 deletions backend/database/seeders/RoleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
class RoleSeeder extends Seeder
{
/**
* Run the database seeds.
* Run the role seeder.
*
* @deprecated Permission seeding is done via migration. This seeder is only useful for development (maybe).
* @return void
*/
public function run()
{
$roles = Role::getArrayOfAllRoleNames();
foreach ($roles as $key => $role) {
Role::factory()->create([
'id' => $key + 1,
'name' => $role,
]);
Role::factory()->create([
'id' => $key + 1,
'name' => $role,
]);
}

Permission::create(['name' => Permission::UPDATE_USERS])
Expand Down
3 changes: 0 additions & 3 deletions backend/tests/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@
abstract class ApiTestCase extends TestCase
{
use MakesGraphQLRequests;

public $seed = true;
public $seeder = 'RoleSeeder';
}
3 changes: 0 additions & 3 deletions backend/tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ abstract class TestCase extends BaseTestCase
{
use CreatesApplication;

public $seed = true;
public $seeder = 'RoleSeeder';

/**
* Act as a new user with application administrator role
*
Expand Down

0 comments on commit bc6feac

Please sign in to comment.