Skip to content

Commit

Permalink
Reset relations when permissions are updated or deleted (#24)
Browse files Browse the repository at this point in the history
* Refresh role model when permissions are updated

* Fix permissions not created when revoked
  • Loading branch information
dmason30 authored Oct 7, 2020
1 parent 6eb25e0 commit fbeb32d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public function setPermissions(array $permissions)

$this->revokeAll();

collect($permissions)->map(function ($permission) {
collect($permissions)->each(function ($permission) {
$this->grant($permission);
});

$this->setRelations([]);
}

/**
Expand Down Expand Up @@ -90,8 +92,6 @@ public function grant($permission)
'role_id' => $this->id,
'permission_slug' => $permission,
]);

return false;
}

/**
Expand All @@ -103,8 +103,12 @@ public function grant($permission)
*/
public function revoke($permission)
{
if (\is_string($permission)) {
return Permission::findOrFail($permission)->delete();
if (\is_string($permission)) {
Permission::findOrFail($permission)->delete();

$this->setRelations([]);

return true;
}

return false;
Expand All @@ -115,7 +119,11 @@ public function revoke($permission)
*/
public function revokeAll()
{
return $this->getPermissions()->delete();
$this->getPermissions()->delete();

$this->setRelations([]);

return true;
}

/**
Expand Down

0 comments on commit fbeb32d

Please sign in to comment.