Skip to content

Commit

Permalink
feat: ability to sync roles opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
craigAtCD committed May 22, 2024
1 parent 494aaee commit 4d2ae81
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Traits/SeedsPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ trait SeedsPermissions
*/
protected static array $cachedRoles = [];

protected function shouldSyncRolePermissions() : bool
{
return property_exists($this, 'syncRolePermissions') && $this->syncRolePermissions;
}

public function run(): void
{
app()[PermissionRegistrar::class]->forgetCachedPermissions();
Expand All @@ -39,8 +44,12 @@ protected function seedPermissions(): void
{
foreach ($this->permissions as $permission => $roles) {
$rolePermissions = Permission::firstOrCreate(['name' => $permission]);
foreach ($roles as $roleName) {
$this->getRole($roleName)->givePermissionTo($rolePermissions);
if($this->shouldSyncRolePermissions()){
$rolePermissions->syncRoles($roles);
}else {
foreach ($roles as $roleName) {
$this->getRole($roleName)->givePermissionTo($rolePermissions);
}
}
}
}
Expand Down

0 comments on commit 4d2ae81

Please sign in to comment.