Skip to content

Commit

Permalink
Added Events PermissionAttached, PermissionDetached, `RoleAttache…
Browse files Browse the repository at this point in the history
…d` and `RoleDetached`
  • Loading branch information
sven-arztkonsultation committed Oct 28, 2024
1 parent 7483792 commit 93ef4a0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Events/PermissionAttached.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class PermissionAttached
use InteractsWithSockets;
use SerializesModels;

public function __construct(public Model $model)
public function __construct(public Model $model, public array $permissionIds)
{}
}
8 changes: 7 additions & 1 deletion src/Events/PermissionDetached.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Spatie\Permission\Contracts\Permission;

class PermissionDetached
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public function __construct(public Model $model)
/**
* @param Model $model
* @param Permission|Permission[]|Collection $permission
*/
public function __construct(public Model $model, public mixed $permission)
{}
}
2 changes: 1 addition & 1 deletion src/Events/RoleAttached.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class RoleAttached
use InteractsWithSockets;
use SerializesModels;

public function __construct(public Model $model)
public function __construct(public Model $model, public array $roleIds)
{}
}
3 changes: 2 additions & 1 deletion src/Events/RoleDetached.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Spatie\Permission\Contracts\Role;

class RoleDetached
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public function __construct(public Model $model)
public function __construct(public Model $model, public Role $role)
{}
}
8 changes: 5 additions & 3 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function ($object) use ($permissions, $model, $teamPivot, &$saved) {
$this->forgetCachedPermissions();
}

event(new PermissionAttached($this->getModel()));
event(new PermissionAttached($this->getModel(), $permissions));

$this->forgetWildcardPermissionIndex();

Expand Down Expand Up @@ -463,13 +463,15 @@ public function syncPermissions(...$permissions)
*/
public function revokePermissionTo($permission)
{
$this->permissions()->detach($this->getStoredPermission($permission));
$storedPermission = $this->getStoredPermission($permission);

$this->permissions()->detach($storedPermission);

if (is_a($this, Role::class)) {
$this->forgetCachedPermissions();
}

event(new PermissionDetached($this->getModel()));
event(new PermissionDetached($this->getModel(), $storedPermission));

$this->forgetWildcardPermissionIndex();

Expand Down
8 changes: 5 additions & 3 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function ($object) use ($roles, $model, $teamPivot, &$saved) {
$this->forgetCachedPermissions();
}

event(new RoleAttached($this->getModel()));
event(new RoleAttached($this->getModel(), $roles));

return $this;
}
Expand All @@ -193,15 +193,17 @@ function ($object) use ($roles, $model, $teamPivot, &$saved) {
*/
public function removeRole($role)
{
$this->roles()->detach($this->getStoredRole($role));
$storedRole = $this->getStoredRole($role);

$this->roles()->detach($storedRole);

$this->unsetRelation('roles');

if (is_a($this, Permission::class)) {
$this->forgetCachedPermissions();
}

event(new RoleDetached($this->getModel()));
event(new RoleDetached($this->getModel(), $storedRole));

return $this;
}
Expand Down
26 changes: 19 additions & 7 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,17 @@ public function it_fires_an_event_when_a_permission_is_added()
{
Event::fake();

$this->testUser->givePermissionTo('edit-news');
$this->testUser->givePermissionTo(['edit-articles', 'edit-news']);

$ids = app(Permission::class)::whereIn('name', ['edit-articles', 'edit-news'])
->pluck($this->testUserPermission->getKeyName())
->toArray();

Event::assertDispatched(PermissionAttached::class, function ($event) {
return $event->model instanceof User && $event->model->hasPermissionTo('edit-news');
Event::assertDispatched(PermissionAttached::class, function ($event) use ($ids) {
return $event->model instanceof User
&& $event->model->hasPermissionTo('edit-news')
&& $event->model->hasPermissionTo('edit-articles')
&& $ids === $event->permissionIds;
});
}

Expand All @@ -787,12 +794,17 @@ public function it_fires_an_event_when_a_permission_is_removed()
{
Event::fake();

$this->testUser->givePermissionTo('edit-news');
$permissions = app(Permission::class)::whereIn('name', ['edit-articles', 'edit-news'])->get();

$this->testUser->givePermissionTo($permissions);

$this->testUser->revokePermissionTo('edit-news');
$this->testUser->revokePermissionTo($permissions);

Event::assertDispatched(PermissionDetached::class, function ($event) {
return $event->model instanceof User && !$event->model->hasPermissionTo('edit-news');
Event::assertDispatched(PermissionDetached::class, function ($event) use ($permissions) {
return $event->model instanceof User
&& !$event->model->hasPermissionTo('edit-news')
&& !$event->model->hasPermissionTo('edit-articles')
&& $event->permission === $permissions;
});
}
}
17 changes: 13 additions & 4 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,10 +865,17 @@ public function it_fires_an_event_when_a_role_is_added()
{
Event::fake();

$this->testUser->assignRole('testRole');
$this->testUser->assignRole(['testRole', 'testRole2']);

$roleIds = app(Role::class)::whereIn('name', ['testRole', 'testRole2'])
->pluck($this->testUserRole->getKeyName())
->toArray();

Event::assertDispatched(RoleAttached::class, function ($event) {
return $event->model instanceof User && $event->model->hasRole('testRole');
Event::assertDispatched(RoleAttached::class, function ($event) use ($roleIds) {
return $event->model instanceof User
&& $event->model->hasRole('testRole')
&& $event->model->hasRole('testRole2')
&& $event->roleIds === $roleIds;
});
}

Expand All @@ -882,7 +889,9 @@ public function it_fires_an_event_when_a_role_is_removed()
$this->testUser->removeRole('testRole');

Event::assertDispatched(RoleDetached::class, function ($event) {
return $event->model instanceof User && !$event->model->hasRole('testRole');
return $event->model instanceof User
&& !$event->model->hasRole('testRole')
&& $event->role->name === 'testRole';
});
}
}

0 comments on commit 93ef4a0

Please sign in to comment.