Skip to content

Commit 492d973

Browse files
Added Events PermissionAttached, PermissionDetached, RoleAttached and RoleDetached
1 parent 43bc084 commit 492d973

8 files changed

+121
-0
lines changed

src/Events/PermissionAttached.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spatie\Permission\Events;
6+
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class PermissionAttached
13+
{
14+
use Dispatchable;
15+
use InteractsWithSockets;
16+
use SerializesModels;
17+
18+
public function __construct(public Model $model)
19+
{}
20+
}

src/Events/PermissionDetached.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spatie\Permission\Events;
6+
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class PermissionDetached
13+
{
14+
use Dispatchable;
15+
use InteractsWithSockets;
16+
use SerializesModels;
17+
18+
public function __construct(public Model $model)
19+
{}
20+
}

src/Events/RoleAttached.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spatie\Permission\Events;
6+
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class RoleAttached
13+
{
14+
use Dispatchable;
15+
use InteractsWithSockets;
16+
use SerializesModels;
17+
18+
public function __construct(public Model $model)
19+
{}
20+
}

src/Events/RoleDetached.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spatie\Permission\Events;
6+
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class RoleDetached
13+
{
14+
use Dispatchable;
15+
use InteractsWithSockets;
16+
use SerializesModels;
17+
18+
public function __construct(public Model $model)
19+
{}
20+
}

src/Traits/HasPermissions.php

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Spatie\Permission\Contracts\Permission;
1010
use Spatie\Permission\Contracts\Role;
1111
use Spatie\Permission\Contracts\Wildcard;
12+
use Spatie\Permission\Events\PermissionAttached;
13+
use Spatie\Permission\Events\PermissionDetached;
1214
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
1315
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
1416
use Spatie\Permission\Exceptions\WildcardPermissionInvalidArgument;
@@ -422,6 +424,8 @@ function ($object) use ($permissions, $model, $teamPivot, &$saved) {
422424
$this->forgetCachedPermissions();
423425
}
424426

427+
event(new PermissionAttached($this->getModel()));
428+
425429
$this->forgetWildcardPermissionIndex();
426430

427431
return $this;
@@ -465,6 +469,8 @@ public function revokePermissionTo($permission)
465469
$this->forgetCachedPermissions();
466470
}
467471

472+
event(new PermissionDetached($this->getModel()));
473+
468474
$this->forgetWildcardPermissionIndex();
469475

470476
$this->unsetRelation('permissions');

src/Traits/HasRoles.php

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Illuminate\Support\Collection;
99
use Spatie\Permission\Contracts\Permission;
1010
use Spatie\Permission\Contracts\Role;
11+
use Spatie\Permission\Events\RoleAttached;
12+
use Spatie\Permission\Events\RoleDetached;
1113
use Spatie\Permission\PermissionRegistrar;
1214

1315
trait HasRoles
@@ -179,6 +181,8 @@ function ($object) use ($roles, $model, $teamPivot, &$saved) {
179181
$this->forgetCachedPermissions();
180182
}
181183

184+
event(new RoleAttached($this->getModel()));
185+
182186
return $this;
183187
}
184188

@@ -197,6 +201,8 @@ public function removeRole($role)
197201
$this->forgetCachedPermissions();
198202
}
199203

204+
event(new RoleDetached($this->getModel()));
205+
200206
return $this;
201207
}
202208

tests/HasPermissionsTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace Spatie\Permission\Tests;
44

55
use DB;
6+
use Illuminate\Support\Facades\Event;
67
use Spatie\Permission\Contracts\Permission;
78
use Spatie\Permission\Contracts\Role;
9+
use Spatie\Permission\Events\PermissionAttached;
10+
use Spatie\Permission\Events\RoleAttached;
811
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
912
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
1013
use Spatie\Permission\Tests\TestModels\SoftDeletingUser;
@@ -765,4 +768,16 @@ public function it_can_reject_permission_based_on_logged_in_user_guard()
765768
'status' => false,
766769
]);
767770
}
771+
772+
/** @test */
773+
public function it_fires_an_event_when_a_permission_is_added()
774+
{
775+
Event::fake();
776+
777+
$this->testUser->givePermissionTo('edit-news');
778+
779+
Event::assertDispatched(PermissionAttached::class, function ($event) {
780+
return $event->model instanceof User && $event->model->hasPermissionTo('edit-news');
781+
});
782+
}
768783
}

tests/HasRolesTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Spatie\Permission\Tests;
44

55
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Event;
67
use Spatie\Permission\Contracts\Role;
8+
use Spatie\Permission\Events\RoleAttached;
79
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
810
use Spatie\Permission\Exceptions\RoleDoesNotExist;
911
use Spatie\Permission\Tests\TestModels\Admin;
@@ -856,4 +858,16 @@ public function it_does_not_detach_roles_when_user_soft_deleting()
856858

857859
$this->assertTrue($user->hasRole('testRole'));
858860
}
861+
862+
/** @test */
863+
public function it_fires_an_event_when_a_role_is_added()
864+
{
865+
Event::fake();
866+
867+
$this->testUser->assignRole('testRole');
868+
869+
Event::assertDispatched(RoleAttached::class, function ($event) {
870+
return $event->model instanceof User && $event->model->hasRole('testRole');
871+
});
872+
}
859873
}

0 commit comments

Comments
 (0)