1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\Permission\Contracts\Permission; |
7
|
|
|
use Spatie\Permission\Exceptions\GuardDoesNotMatch; |
8
|
|
|
use Spatie\Permission\PermissionRegistrar; |
9
|
|
|
|
10
|
|
|
trait HasPermissions |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Grant the given permission(s) to a role. |
14
|
|
|
* |
15
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
16
|
|
|
* |
17
|
|
|
* @return $this |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function givePermissionTo(...$permissions) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$permissions = collect($permissions) |
22
|
|
|
->flatten() |
23
|
|
|
->map(function ($permission) { |
24
|
|
|
return $this->getStoredPermission($permission); |
25
|
|
|
}) |
26
|
|
|
->each(function ($permission) { |
27
|
|
|
$this->ensureModelSharesGuard($permission); |
28
|
|
|
}) |
29
|
|
|
->all(); |
30
|
|
|
|
31
|
|
|
$this->permissions()->saveMany($permissions); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$this->forgetCachedPermissions(); |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Remove all current permissions and set the given ones. |
40
|
|
|
* |
41
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function syncPermissions(...$permissions) |
46
|
|
|
{ |
47
|
|
|
$this->permissions()->detach(); |
|
|
|
|
48
|
|
|
|
49
|
|
|
return $this->givePermissionTo($permissions); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Revoke the given permission. |
54
|
|
|
* |
55
|
|
|
* @param \Spatie\Permission\Contracts\Permission|string $permission |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function revokePermissionTo($permission) |
60
|
|
|
{ |
61
|
|
|
$this->permissions()->detach($this->getStoredPermission($permission)); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$this->forgetCachedPermissions(); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
70
|
|
|
* |
71
|
|
|
* @return \Spatie\Permission\Contracts\Permission |
72
|
|
|
*/ |
73
|
|
|
protected function getStoredPermission($permissions): Permission |
74
|
|
|
{ |
75
|
|
|
if (is_string($permissions)) { |
76
|
|
|
return app(Permission::class)->findByName($permissions, $this->getDefaultGuardName()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (is_array($permissions)) { |
80
|
|
|
return app(Permission::class) |
81
|
|
|
->whereIn('name', $permissions) |
82
|
|
|
->whereId('guard_name', $this->getGuardNames()) |
83
|
|
|
->get(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $permissions; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
91
|
|
|
* |
92
|
|
|
* @throws \Spatie\Permission\Exceptions\GuardMismatch |
93
|
|
|
*/ |
94
|
|
|
protected function ensureModelSharesGuard($roleOrPermission) |
95
|
|
|
{ |
96
|
|
|
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) { |
97
|
|
|
throw GuardDoesNotMatch::create($roleOrPermission->guard_name, $this->getGuardNames()); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function getGuardNames(): Collection |
102
|
|
|
{ |
103
|
|
|
if ($this->guard_name) { |
104
|
|
|
return collect($this->guard_name); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return collect(config('auth.guards')) |
108
|
|
|
->map(function ($guard) { |
109
|
|
|
return config("auth.providers.{$guard['provider']}.model"); |
110
|
|
|
}) |
111
|
|
|
->filter(function ($model) { |
112
|
|
|
return get_class($this) === $model; |
113
|
|
|
}) |
114
|
|
|
->keys(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function getDefaultGuardName(): string |
118
|
|
|
{ |
119
|
|
|
$default = config('auth.defaults.guard'); |
120
|
|
|
|
121
|
|
|
return $this->getGuardNames()->first() ?: $default; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Forget the cached permissions. |
126
|
|
|
*/ |
127
|
|
|
public function forgetCachedPermissions() |
128
|
|
|
{ |
129
|
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.