Skip to content

Commit 516e57d

Browse files
authored
fix: when --seed is used AND code-first is enabled, it still syncs al… (#38)
Fixes an issue where using the --seed flag with code-first mode enabled would not sync all discovered definitions before seeding assignments. The fix ensures that when --seed is used with code-first enabled, all permissions, roles, and capabilities are synced from code-first definitions before applying the configured assignments.
1 parent 6fa12ce commit 516e57d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Commands/SyncCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public function handle(
7878
$isDryRun = (bool) $this->option('dry-run');
7979
/** @var string|null $guard */
8080
$guard = $this->option('guard') ?: null;
81-
$syncAll = ! $this->option('permissions') && ! $this->option('roles') && ! $this->option('capabilities') && ! $seedOnly;
81+
// Sync all if no specific flags passed, or if --seed is used with code-first enabled
82+
// This ensures discovered permissions are synced before seeding assignments
83+
$syncAll = ! $this->option('permissions') && ! $this->option('roles') && ! $this->option('capabilities')
84+
&& (! $seedOnly || $codeFirstEnabled);
8285

8386
if ($isDryRun) {
8487
$this->components->info('Dry run mode - no changes will be made.');

tests/Feature/Commands/SyncCommandTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,31 @@
264264
expect($role->hasCapability('test-capability'))->toBeTrue();
265265
expect($role->hasCapability('another-capability'))->toBeTrue();
266266
});
267+
268+
it('syncs all discovered permissions when using --seed with code-first enabled', function () {
269+
config(['mandate.code_first.enabled' => true]);
270+
config(['mandate.code_first.paths.permissions' => __DIR__.'/../../Fixtures/CodeFirst']);
271+
config(['mandate.code_first.paths.roles' => __DIR__.'/../../Fixtures/CodeFirst']);
272+
273+
// Only assign one permission, but all should be synced
274+
config(['mandate.assignments' => [
275+
'partial-role' => [
276+
'permissions' => ['article:view'],
277+
],
278+
]]);
279+
280+
$this->artisan('mandate:sync', ['--seed' => true])
281+
->assertSuccessful();
282+
283+
// All permissions from code-first should be synced, not just those in assignments
284+
expect(Permission::where('name', 'article:view')->exists())->toBeTrue();
285+
expect(Permission::where('name', 'article:create')->exists())->toBeTrue();
286+
expect(Permission::where('name', 'article:edit')->exists())->toBeTrue();
287+
expect(Permission::where('name', 'article:delete')->exists())->toBeTrue();
288+
289+
// But only article:view should be assigned to the role
290+
$role = Role::where('name', 'partial-role')->first();
291+
expect($role->hasPermission('article:view'))->toBeTrue();
292+
expect($role->hasPermission('article:create'))->toBeFalse();
293+
});
267294
});

0 commit comments

Comments
 (0)