|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\Permission\Contracts\Permission; |
|
6
|
|
|
|
|
7
|
|
|
trait HasPermissions |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Grant the given permission(s) to a role. |
|
11
|
|
|
* |
|
12
|
|
|
* @param string|array|Permission|\Illuminate\Support\Collection $permissions |
|
13
|
|
|
* |
|
14
|
|
|
* @return HasPermissions |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
|
|
public function givePermissionTo(...$permissions) |
|
17
|
|
|
{ |
|
18
|
|
|
collect($permissions) |
|
19
|
|
|
->flatten() |
|
20
|
|
|
->map(function ($permission) { |
|
21
|
|
|
return $this->getStoredPermission($permission); |
|
22
|
|
|
}) |
|
23
|
|
|
->each(function (Permission $permission) { |
|
24
|
|
|
return $this->permissions()->save($permission); |
|
|
|
|
|
|
25
|
|
|
}); |
|
26
|
|
|
|
|
27
|
|
|
$this->forgetCachedPermissions(); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Remove all current permissions and set the given ones. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array ...$permissions |
|
36
|
|
|
*/ |
|
37
|
|
|
public function syncPermissions(...$permissions) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->permissions()->detach(); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
collect($permissions) |
|
42
|
|
|
->flatten() |
|
43
|
|
|
->map(function ($permission) { |
|
44
|
|
|
return $this->getStoredPermission($permission); |
|
45
|
|
|
})->each(function (Permission $permission) { |
|
46
|
|
|
$this->permissions()->save($permission); |
|
|
|
|
|
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Revoke the given permission. |
|
52
|
|
|
* |
|
53
|
|
|
* @param $permission |
|
54
|
|
|
* |
|
55
|
|
|
* @return HasPermissions |
|
|
|
|
|
|
56
|
|
|
*/ |
|
57
|
|
|
public function revokePermissionTo($permission) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->permissions()->detach($this->getStoredPermission($permission)); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$this->forgetCachedPermissions(); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string|array|Permission|\Illuminate\Support\Collection $permissions |
|
68
|
|
|
* |
|
69
|
|
|
* @return Permission |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getStoredPermission($permissions) |
|
72
|
|
|
{ |
|
73
|
|
|
if (is_string($permissions)) { |
|
74
|
|
|
return app(Permission::class)->findByName($permissions); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (is_array($permissions)) { |
|
78
|
|
|
return app(Permission::class)->whereIn('name', $permissions)->get(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $permissions; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.