1 | <?php |
||
17 | trait HasRole |
||
18 | { |
||
19 | private $roleClass; |
||
20 | |||
21 | /** |
||
22 | * Check if user have access using any of the acl. |
||
23 | * |
||
24 | * @param array $acl |
||
25 | * @return boolean |
||
26 | */ |
||
27 | public function canAccess(array $acl): bool |
||
28 | { |
||
29 | return $this->canAtLeast($acl) || $this->hasRole($acl); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Check if user has at least one of the given permissions |
||
34 | * |
||
35 | * @param array $permissions |
||
36 | * @return bool |
||
37 | */ |
||
38 | public function canAtLeast(array $permissions): bool |
||
39 | { |
||
40 | $can = false; |
||
41 | |||
42 | if (auth()->check()) { |
||
|
|||
43 | foreach ($this->roles as $role) { |
||
44 | if ($role->canAtLeast($permissions)) { |
||
45 | $can = true; |
||
46 | } |
||
47 | } |
||
48 | } else { |
||
49 | $guest = $this->findRoleBySlug('guest'); |
||
50 | |||
51 | if ($guest) { |
||
52 | return $guest->canAtLeast($permissions); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | return $can; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get Role class. |
||
61 | * |
||
62 | * @return Role |
||
63 | */ |
||
64 | public function getRoleClass(): Role |
||
65 | { |
||
66 | if (!isset($this->roleClass)) { |
||
67 | $this->roleClass = resolve(config('acl.role')); |
||
68 | } |
||
69 | |||
70 | return $this->roleClass; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Check if user has the given role. |
||
75 | * |
||
76 | * @param string|array $role |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function hasRole($role): bool |
||
80 | { |
||
81 | if (is_string($role)) { |
||
82 | return $this->roles->contains('slug', $role); |
||
83 | } |
||
84 | |||
85 | if (is_array($role)) { |
||
86 | $roles = $this->getRoleSlugs(); |
||
87 | |||
88 | $intersection = array_intersect($roles, (array) $role); |
||
89 | $intersectionCount = count($intersection); |
||
90 | |||
91 | return $intersectionCount > 0; |
||
92 | } |
||
93 | |||
94 | return !!$role->intersect($this->roles)->count(); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Get all user roles. |
||
99 | * |
||
100 | * @return array|null |
||
101 | */ |
||
102 | public function getRoleSlugs() |
||
103 | { |
||
104 | if (!is_null($this->roles)) { |
||
105 | return $this->roles->pluck('slug')->toArray(); |
||
106 | } |
||
107 | |||
108 | return null; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Attach a role to user using slug. |
||
113 | * |
||
114 | * @param string $slug |
||
115 | */ |
||
116 | public function attachRoleBySlug(string $slug) |
||
117 | { |
||
118 | $this->attachRole($this->findRoleBySlug($slug)); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Attach a role to user. |
||
123 | * |
||
124 | * @param mixed $role |
||
125 | */ |
||
126 | public function attachRole($role) |
||
130 | |||
131 | /** |
||
132 | * Model can have many roles. |
||
133 | * |
||
134 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
135 | */ |
||
136 | public function roles(): BelongsToMany |
||
140 | |||
141 | /** |
||
142 | * Find a role by slug. |
||
143 | * |
||
144 | * @param string $slug |
||
145 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null |
||
146 | */ |
||
147 | protected function findRoleBySlug(string $slug): ?Role |
||
151 | |||
152 | /** |
||
153 | * Query scope for user having the given roles. |
||
154 | * |
||
155 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
156 | * @param array $roles |
||
157 | * @return \Illuminate\Database\Eloquent\Builder |
||
158 | */ |
||
159 | public function scopeHavingRoles(Builder $query, array $roles): Builder |
||
168 | |||
169 | /** |
||
170 | * Query scope for user having the given roles by slugs. |
||
171 | * |
||
172 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
173 | * @param array $slugs |
||
174 | * @return \Illuminate\Database\Eloquent\Builder |
||
175 | */ |
||
176 | public function scopeHavingRolesBySlugs(Builder $query, array $slugs): Builder |
||
182 | |||
183 | /** |
||
184 | * Revokes the given role from the user using slug. |
||
185 | * |
||
186 | * @param string $slug |
||
187 | * @return int |
||
188 | */ |
||
189 | public function revokeRoleBySlug(string $slug): int |
||
195 | |||
196 | /** |
||
197 | * Revokes the given role from the user. |
||
198 | * |
||
199 | * @param mixed $role |
||
200 | * @return int |
||
201 | */ |
||
202 | public function revokeRole($role = ""): int |
||
206 | |||
207 | /** |
||
208 | * Syncs the given role(s) with the user. |
||
209 | * |
||
210 | * @param array $roles |
||
211 | * @return array |
||
212 | */ |
||
213 | public function syncRoles(array $roles): array |
||
217 | |||
218 | /** |
||
219 | * Revokes all roles from the user. |
||
220 | * |
||
221 | * @return int |
||
222 | */ |
||
223 | public function revokeAllRoles(): int |
||
227 | |||
228 | /** |
||
229 | * Get all user role permissions. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public function getPermissions(): array |
||
243 | |||
244 | /** |
||
245 | * Magic __call method to handle dynamic methods. |
||
246 | * |
||
247 | * @param string $method |
||
248 | * @param array $arguments |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public function __call($method, $arguments = []) |
||
269 | |||
270 | /** |
||
271 | * Checks if the user has the given role. |
||
272 | * |
||
273 | * @param string $slug |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function isRole(string $slug): bool |
||
288 | |||
289 | /** |
||
290 | * Check if the given entity/model is owned by the user. |
||
291 | * |
||
292 | * @param \Illuminate\Database\Eloquent\Model $entity |
||
293 | * @param string $relation |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function owns(Model $entity, $relation = 'user_id'): bool |
||
300 | } |
||
301 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: