1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Maklad\Permission\Traits; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Jenssegers\Mongodb\Eloquent\Model; |
8
|
|
|
use Maklad\Permission\Contracts\PermissionInterface as Permission; |
9
|
|
|
use Maklad\Permission\Exceptions\GuardDoesNotMatch; |
10
|
|
|
use Maklad\Permission\Helpers; |
11
|
|
|
use Maklad\Permission\PermissionRegistrar; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait HasPermissions |
15
|
|
|
* @package Maklad\Permission\Traits |
16
|
|
|
*/ |
17
|
|
|
trait HasPermissions |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Grant the given permission(s) to a role. |
21
|
|
|
* |
22
|
|
|
* @param string|array|Permission|\Illuminate\Support\Collection $permissions |
23
|
|
|
* |
24
|
|
|
* @return $this |
25
|
|
|
* @throws GuardDoesNotMatch |
26
|
|
|
*/ |
27
|
39 |
View Code Duplication |
public function givePermissionTo(...$permissions) |
|
|
|
|
28
|
|
|
{ |
29
|
39 |
|
$permissions = \collect($permissions) |
30
|
39 |
|
->flatten() |
31
|
39 |
|
->map(function ($permission) { |
32
|
39 |
|
return $this->getStoredPermission($permission); |
33
|
39 |
|
}) |
34
|
37 |
|
->each(function ($permission) { |
35
|
37 |
|
$this->ensureModelSharesGuard($permission); |
36
|
37 |
|
}) |
37
|
34 |
|
->all(); |
38
|
|
|
|
39
|
34 |
|
$this->permissions()->saveMany($permissions); |
|
|
|
|
40
|
|
|
|
41
|
34 |
|
$this->forgetCachedPermissions(); |
42
|
|
|
|
43
|
34 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Remove all current permissions and set the given ones. |
48
|
|
|
* |
49
|
|
|
* @param string|array|Permission|\Illuminate\Support\Collection $permissions |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
* @throws GuardDoesNotMatch |
53
|
|
|
*/ |
54
|
4 |
|
public function syncPermissions(...$permissions) |
55
|
|
|
{ |
56
|
4 |
|
$this->permissions()->detach(); |
|
|
|
|
57
|
|
|
|
58
|
4 |
|
return $this->givePermissionTo($permissions); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Revoke the given permission. |
63
|
|
|
* |
64
|
|
|
* @param Permission|string $permission |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
5 |
|
public function revokePermissionTo($permission) |
69
|
|
|
{ |
70
|
5 |
|
$this->permissions()->detach($this->getStoredPermission($permission)); |
|
|
|
|
71
|
|
|
|
72
|
5 |
|
$this->forgetCachedPermissions(); |
73
|
|
|
|
74
|
5 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|Permission $permissions |
79
|
|
|
* |
80
|
|
|
* @return Permission |
81
|
|
|
*/ |
82
|
40 |
|
protected function getStoredPermission($permissions): Permission |
83
|
|
|
{ |
84
|
40 |
|
if (\is_string($permissions)) { |
85
|
28 |
|
return \app(Permission::class)->findByName($permissions, $this->getDefaultGuardName()); |
86
|
|
|
} |
87
|
|
|
|
88
|
15 |
|
return $permissions; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Model $roleOrPermission |
93
|
|
|
* |
94
|
|
|
* @throws GuardDoesNotMatch |
95
|
|
|
*/ |
96
|
73 |
|
protected function ensureModelSharesGuard(Model $roleOrPermission) |
97
|
|
|
{ |
98
|
73 |
View Code Duplication |
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) { |
|
|
|
|
99
|
5 |
|
$expected = $this->getGuardNames(); |
100
|
5 |
|
$given = $roleOrPermission->guard_name; |
101
|
5 |
|
$helpers = new Helpers(); |
102
|
|
|
|
103
|
5 |
|
throw new GuardDoesNotMatch($helpers->getGuardDoesNotMatchMessage($expected, $given)); |
104
|
|
|
} |
105
|
69 |
|
} |
106
|
|
|
|
107
|
87 |
|
protected function getGuardNames(): Collection |
108
|
|
|
{ |
109
|
87 |
|
if ($this->guard_name) { |
110
|
27 |
|
return \collect($this->guard_name); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
71 |
|
return \collect(\config('auth.guards')) |
114
|
71 |
|
->map(function ($guard) { |
115
|
71 |
|
return \config("auth.providers.{$guard['provider']}.model"); |
116
|
71 |
|
}) |
117
|
71 |
|
->filter(function ($model) { |
118
|
71 |
|
return \get_class($this) === $model; |
119
|
71 |
|
}) |
120
|
71 |
|
->keys(); |
121
|
|
|
} |
122
|
|
|
|
123
|
68 |
|
protected function getDefaultGuardName(): string |
124
|
|
|
{ |
125
|
68 |
|
$default = \config('auth.defaults.guard'); |
126
|
|
|
|
127
|
68 |
|
return $this->getGuardNames()->first() ?: $default; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Forget the cached permissions. |
132
|
|
|
*/ |
133
|
68 |
|
public function forgetCachedPermissions() |
134
|
|
|
{ |
135
|
68 |
|
\app(PermissionRegistrar::class)->forgetCachedPermissions(); |
136
|
68 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Convert to Permission Models |
140
|
|
|
* |
141
|
|
|
* @param string|array|Collection $permissions |
142
|
|
|
* |
143
|
|
|
* @return Collection |
144
|
|
|
*/ |
145
|
7 |
View Code Duplication |
private function convertToPermissionModels($permissions): Collection |
|
|
|
|
146
|
|
|
{ |
147
|
7 |
|
if (\is_array($permissions)) { |
148
|
3 |
|
$permissions = \collect($permissions); |
149
|
|
|
} |
150
|
|
|
|
151
|
7 |
|
if (! $permissions instanceof Collection) { |
152
|
5 |
|
$permissions = \collect([$permissions]); |
153
|
|
|
} |
154
|
|
|
|
155
|
7 |
|
$permissions = $permissions->map(function ($permission) { |
156
|
7 |
|
return $this->getStoredPermission($permission); |
157
|
7 |
|
}); |
158
|
|
|
|
159
|
|
|
return $permissions; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.