Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4 events for adding and removing roles or permissions #2742

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Events/PermissionAttached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Spatie\Permission\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PermissionAttached
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public function __construct(public Model $model, public array $permissionIds)
{}
}
26 changes: 26 additions & 0 deletions src/Events/PermissionDetached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Spatie\Permission\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
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;

/**
* @param Model $model
* @param Permission|Permission[]|Collection $permission
*/
public function __construct(public Model $model, public mixed $permission)
{}
}
20 changes: 20 additions & 0 deletions src/Events/RoleAttached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Spatie\Permission\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;

class RoleAttached
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public function __construct(public Model $model, public array $roleIds)
{}
}
21 changes: 21 additions & 0 deletions src/Events/RoleDetached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Spatie\Permission\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
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 Role $role)
{}
}
10 changes: 9 additions & 1 deletion src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Contracts\Wildcard;
use Spatie\Permission\Events\PermissionAttached;
use Spatie\Permission\Events\PermissionDetached;
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
use Spatie\Permission\Exceptions\WildcardPermissionInvalidArgument;
Expand Down Expand Up @@ -422,6 +424,8 @@ function ($object) use ($permissions, $model, $teamPivot, &$saved) {
$this->forgetCachedPermissions();
}

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

$this->forgetWildcardPermissionIndex();

return $this;
Expand Down Expand Up @@ -459,12 +463,16 @@ 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(), $storedPermission));

$this->forgetWildcardPermissionIndex();

$this->unsetRelation('permissions');
Expand Down
10 changes: 9 additions & 1 deletion src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Illuminate\Support\Collection;
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Events\RoleAttached;
use Spatie\Permission\Events\RoleDetached;
use Spatie\Permission\PermissionRegistrar;

trait HasRoles
Expand Down Expand Up @@ -179,6 +181,8 @@ function ($object) use ($roles, $model, $teamPivot, &$saved) {
$this->forgetCachedPermissions();
}

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

return $this;
}

Expand All @@ -189,14 +193,18 @@ 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(), $storedRole));

return $this;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace Spatie\Permission\Tests;

use DB;
use Illuminate\Support\Facades\Event;
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Events\PermissionAttached;
use Spatie\Permission\Events\PermissionDetached;
use Spatie\Permission\Events\RoleAttached;
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
use Spatie\Permission\Tests\TestModels\SoftDeletingUser;
Expand Down Expand Up @@ -765,4 +769,42 @@ public function it_can_reject_permission_based_on_logged_in_user_guard()
'status' => false,
]);
}

/** @test */
public function it_fires_an_event_when_a_permission_is_added()
{
Event::fake();

$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) use ($ids) {
return $event->model instanceof User
&& $event->model->hasPermissionTo('edit-news')
&& $event->model->hasPermissionTo('edit-articles')
&& $ids === $event->permissionIds;
});
}

/** @test */
public function it_fires_an_event_when_a_permission_is_removed()
{
Event::fake();

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

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

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

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;
});
}
}
38 changes: 38 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Spatie\Permission\Tests;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Events\RoleAttached;
use Spatie\Permission\Events\RoleDetached;
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
use Spatie\Permission\Exceptions\RoleDoesNotExist;
use Spatie\Permission\Tests\TestModels\Admin;
Expand Down Expand Up @@ -856,4 +859,39 @@ public function it_does_not_detach_roles_when_user_soft_deleting()

$this->assertTrue($user->hasRole('testRole'));
}

/** @test */
public function it_fires_an_event_when_a_role_is_added()
{
Event::fake();

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

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

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

/** @test */
public function it_fires_an_event_when_a_role_is_removed()
{
Event::fake();

$this->testUser->assignRole('testRole');

$this->testUser->removeRole('testRole');

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