1 | <?php |
||||
2 | |||||
3 | namespace jeremykenedy\LaravelRoles\Traits; |
||||
4 | |||||
5 | use Illuminate\Support\Facades\DB; |
||||
6 | |||||
7 | trait RolesAndPermissionsHelpersTrait |
||||
8 | { |
||||
9 | /** |
||||
10 | * Delete a permission. |
||||
11 | * |
||||
12 | * @param int $id The identifier |
||||
13 | * |
||||
14 | * @return collection |
||||
0 ignored issues
–
show
|
|||||
15 | */ |
||||
16 | public function deletePermission($id) |
||||
17 | { |
||||
18 | $permission = $this->getPermission($id); |
||||
19 | $permission->delete(); |
||||
20 | |||||
21 | return $permission; |
||||
22 | } |
||||
23 | |||||
24 | /** |
||||
25 | * Destroy all the deleted roles. |
||||
26 | * |
||||
27 | * @return array |
||||
28 | */ |
||||
29 | public function destroyAllTheDeletedPermissions() |
||||
30 | { |
||||
31 | $deletedPermissions = $this->getDeletedPermissions()->get(); |
||||
32 | $deletedPermissionsCount = $deletedPermissions->count(); |
||||
33 | $status = 'error'; |
||||
34 | |||||
35 | if ($deletedPermissionsCount > 0) { |
||||
36 | foreach ($deletedPermissions as $deletedPermission) { |
||||
37 | $this->removeUsersAndRolesFromPermissions($deletedPermission); |
||||
38 | $deletedPermission->forceDelete(); |
||||
39 | } |
||||
40 | $status = 'success'; |
||||
41 | } |
||||
42 | |||||
43 | return [ |
||||
44 | 'status' => $status, |
||||
45 | 'count' => $deletedPermissionsCount, |
||||
46 | ]; |
||||
47 | } |
||||
48 | |||||
49 | /** |
||||
50 | * Destroy a permission from storage. |
||||
51 | * |
||||
52 | * @param int $id The identifier |
||||
53 | * |
||||
54 | * @return collection |
||||
55 | */ |
||||
56 | public function destroyPermission($id) |
||||
57 | { |
||||
58 | $permission = $this->getDeletedPermission($id); |
||||
59 | $this->removeUsersAndRolesFromPermissions($permission); |
||||
60 | $permission->forceDelete(); |
||||
61 | |||||
62 | return $permission; |
||||
63 | } |
||||
64 | |||||
65 | /** |
||||
66 | * Delete a role. |
||||
67 | * |
||||
68 | * @param int $id The identifier |
||||
69 | * |
||||
70 | * @return collection |
||||
71 | */ |
||||
72 | public function deleteRole($id) |
||||
73 | { |
||||
74 | $role = $this->getRole($id); |
||||
75 | $role->delete(); |
||||
76 | |||||
77 | return $role; |
||||
78 | } |
||||
79 | |||||
80 | /** |
||||
81 | * Destroy a role from storage. |
||||
82 | * |
||||
83 | * @param int $id The identifier |
||||
84 | * |
||||
85 | * @return collection |
||||
86 | */ |
||||
87 | public function destroyRole($id) |
||||
88 | { |
||||
89 | $role = $this->getDeletedRole($id); |
||||
90 | $this->removeUsersAndPermissionsFromRole($role); |
||||
91 | $role->forceDelete(); |
||||
92 | |||||
93 | return $role; |
||||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * Destroy all the deleted roles. |
||||
98 | * |
||||
99 | * @return array |
||||
100 | */ |
||||
101 | public function destroyAllTheDeletedRoles() |
||||
102 | { |
||||
103 | $deletedRoles = $this->getDeletedRoles()->get(); |
||||
104 | $deletedRolesCount = $deletedRoles->count(); |
||||
105 | $status = 'error'; |
||||
106 | |||||
107 | if ($deletedRolesCount > 0) { |
||||
108 | foreach ($deletedRoles as $deletedRole) { |
||||
109 | $this->removeUsersAndPermissionsFromRole($deletedRole); |
||||
110 | $deletedRole->forceDelete(); |
||||
111 | } |
||||
112 | $status = 'success'; |
||||
113 | } |
||||
114 | |||||
115 | return [ |
||||
116 | 'status' => $status, |
||||
117 | 'count' => $deletedRolesCount, |
||||
118 | ]; |
||||
119 | } |
||||
120 | |||||
121 | /** |
||||
122 | * Get Soft Deleted Permission. |
||||
123 | * |
||||
124 | * @param int $id |
||||
125 | * |
||||
126 | * @return \Illuminate\Http\Response || collection |
||||
0 ignored issues
–
show
|
|||||
127 | */ |
||||
128 | public function getDeletedPermission($id) |
||||
129 | { |
||||
130 | $item = config('roles.models.permission')::onlyTrashed()->where('id', $id)->get(); |
||||
131 | if (count($item) != 1) { |
||||
132 | return abort(redirect('laravelroles::roles.index') |
||||
133 | ->with('error', trans('laravelroles::laravelroles.errors.errorDeletedPermissionNotFound'))); |
||||
134 | } |
||||
135 | |||||
136 | return $item[0]; |
||||
137 | } |
||||
138 | |||||
139 | /** |
||||
140 | * Get Soft Deleted Role. |
||||
141 | * |
||||
142 | * @param int $id |
||||
143 | * |
||||
144 | * @return \Illuminate\Http\Response || collection |
||||
0 ignored issues
–
show
|
|||||
145 | */ |
||||
146 | public function getDeletedRole($id) |
||||
147 | { |
||||
148 | $item = config('roles.models.role')::onlyTrashed()->where('id', $id)->get(); |
||||
149 | if (count($item) != 1) { |
||||
150 | return abort(redirect('laravelroles::roles.index') |
||||
151 | ->with('error', trans('laravelroles::laravelroles.errors.errorDeletedRoleNotFound'))); |
||||
152 | } |
||||
153 | |||||
154 | return $item[0]; |
||||
155 | } |
||||
156 | |||||
157 | /** |
||||
158 | * Gets the roles. |
||||
159 | * |
||||
160 | * @return collection The roles. |
||||
161 | */ |
||||
162 | public function getRoles() |
||||
163 | { |
||||
164 | return config('roles.models.role')::all(); |
||||
165 | } |
||||
166 | |||||
167 | /** |
||||
168 | * Gets the role. |
||||
169 | * |
||||
170 | * @param int $id The identifier |
||||
171 | * |
||||
172 | * @return collection The role. |
||||
173 | */ |
||||
174 | public function getRole($id) |
||||
175 | { |
||||
176 | return config('roles.models.role')::findOrFail($id); |
||||
177 | } |
||||
178 | |||||
179 | /** |
||||
180 | * Gets the permissions. |
||||
181 | * |
||||
182 | * @return collection The permissions. |
||||
183 | */ |
||||
184 | public function getPermissions() |
||||
185 | { |
||||
186 | return config('roles.models.permission')::all(); |
||||
187 | } |
||||
188 | |||||
189 | /** |
||||
190 | * Gets the permission. |
||||
191 | * |
||||
192 | * @param int $id The identifier |
||||
193 | * |
||||
194 | * @return collection The permission. |
||||
195 | */ |
||||
196 | public function getPermission($id) |
||||
197 | { |
||||
198 | return config('roles.models.permission')::findOrFail($id); |
||||
199 | } |
||||
200 | |||||
201 | /** |
||||
202 | * Gets the users. |
||||
203 | * |
||||
204 | * @return collection The users. |
||||
205 | */ |
||||
206 | public function getUsers() |
||||
207 | { |
||||
208 | return config('roles.models.defaultUser')::all(); |
||||
209 | } |
||||
210 | |||||
211 | /** |
||||
212 | * Gets the user. |
||||
213 | * |
||||
214 | * @param int $id The user id |
||||
215 | * |
||||
216 | * @return User The user. |
||||
0 ignored issues
–
show
The type
jeremykenedy\LaravelRoles\Traits\User was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
217 | */ |
||||
218 | public function getUser($id) |
||||
219 | { |
||||
220 | return config('roles.models.defaultUser')::findOrFail($id); |
||||
221 | } |
||||
222 | |||||
223 | /** |
||||
224 | * Gets the deleted roles. |
||||
225 | * |
||||
226 | * @return collection The deleted roles. |
||||
227 | */ |
||||
228 | public function getDeletedRoles() |
||||
229 | { |
||||
230 | return config('roles.models.role')::onlyTrashed(); |
||||
231 | } |
||||
232 | |||||
233 | /** |
||||
234 | * Gets the deleted permissions. |
||||
235 | * |
||||
236 | * @return collection The deleted permissions. |
||||
237 | */ |
||||
238 | public function getDeletedPermissions() |
||||
239 | { |
||||
240 | return config('roles.models.permission')::onlyTrashed(); |
||||
241 | } |
||||
242 | |||||
243 | /** |
||||
244 | * Gets the permissions with roles. |
||||
245 | * |
||||
246 | * @param int $roleId The role Id |
||||
247 | * |
||||
248 | * @return collection The permissions with roles. |
||||
249 | */ |
||||
250 | public function getPermissionsWithRoles($roleId = null) |
||||
251 | { |
||||
252 | $query = DB::connection(config('roles.connection'))->table(config('roles.permissionsRoleTable')); |
||||
253 | |||||
254 | if ($roleId) { |
||||
0 ignored issues
–
show
The expression
$roleId of type integer|null is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||||
255 | $query->where('role_id', '=', $roleId); |
||||
256 | } |
||||
257 | |||||
258 | return $query->get(); |
||||
259 | } |
||||
260 | |||||
261 | /** |
||||
262 | * Gets the permission users. |
||||
263 | * |
||||
264 | * @param int $permissionId The permission identifier |
||||
265 | * |
||||
266 | * @return Collection The permission users. |
||||
0 ignored issues
–
show
The type
jeremykenedy\LaravelRoles\Traits\Collection was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
267 | */ |
||||
268 | public function getPermissionUsers($permissionId = null) |
||||
269 | { |
||||
270 | $query = DB::connection(config('roles.connection'))->table(config('roles.permissionsUserTable')); |
||||
271 | |||||
272 | if ($permissionId) { |
||||
0 ignored issues
–
show
The expression
$permissionId of type integer|null is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||||
273 | $query->where('permission_id', '=', $permissionId); |
||||
274 | } |
||||
275 | |||||
276 | return $query->get(); |
||||
277 | } |
||||
278 | |||||
279 | /** |
||||
280 | * Gets the permission models. |
||||
281 | * |
||||
282 | * @return The permission models. |
||||
0 ignored issues
–
show
The type
jeremykenedy\LaravelRoles\Traits\The was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
283 | */ |
||||
284 | public function getPermissionModels() |
||||
285 | { |
||||
286 | $permissionModel = config('roles.models.permission'); |
||||
287 | |||||
288 | return DB::table(config('roles.permissionsTable'))->pluck('model')->merge(collect(class_basename(new $permissionModel())))->unique(); |
||||
0 ignored issues
–
show
class_basename(new $permissionModel()) of type string is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
289 | } |
||||
290 | |||||
291 | /** |
||||
292 | * Gets the permission item data. |
||||
293 | * |
||||
294 | * @param int $id The Permission ID |
||||
295 | * |
||||
296 | * @return array The Permission item data. |
||||
297 | */ |
||||
298 | public function getPermissionItemData($id) |
||||
299 | { |
||||
300 | $permission = config('roles.models.permission')::findOrFail($id); |
||||
301 | $users = $this->getUsers(); |
||||
302 | $roles = $this->getRoles(); |
||||
303 | $permissions = $this->getPermissions(); |
||||
304 | $sortedRolesWithUsers = $this->getSortedUsersWithRoles($roles, $users); |
||||
305 | $sortedPermissionsRolesUsers = $this->getSortedPermissonsWithRolesAndUsers($sortedRolesWithUsers, $permissions, $users); |
||||
306 | |||||
307 | $data = []; |
||||
308 | |||||
309 | foreach ($sortedPermissionsRolesUsers as $item) { |
||||
310 | if ($item['permission']->id === $permission->id) { |
||||
311 | $data = [ |
||||
312 | 'item' => $item, |
||||
313 | ]; |
||||
314 | } |
||||
315 | } |
||||
316 | |||||
317 | return $data; |
||||
318 | } |
||||
319 | |||||
320 | /** |
||||
321 | * Gets the role permissions. |
||||
322 | * |
||||
323 | * @param int $id The Role Id |
||||
324 | * |
||||
325 | * @return array The role permissions. |
||||
326 | */ |
||||
327 | public function getRolePermissions($id) |
||||
328 | { |
||||
329 | $permissionPivots = $this->getPermissionsWithRoles($id); |
||||
330 | $permissions = []; |
||||
331 | |||||
332 | if (count($permissionPivots) != 0) { |
||||
333 | foreach ($permissionPivots as $permissionPivot) { |
||||
334 | $permissions[] = $this->getPermission($permissionPivot->permission_id); |
||||
335 | } |
||||
336 | } |
||||
337 | |||||
338 | return collect($permissions); |
||||
0 ignored issues
–
show
$permissions of type array|jeremykenedy\Larav...les\Traits\collection[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
339 | } |
||||
340 | |||||
341 | /** |
||||
342 | * Gets the role permissions identifiers. |
||||
343 | * |
||||
344 | * @param int $id The Role Id |
||||
345 | * |
||||
346 | * @return array The role permissions Ids. |
||||
347 | */ |
||||
348 | public function getRolePermissionsIds($id) |
||||
349 | { |
||||
350 | $permissionPivots = $this->getPermissionsWithRoles($id); |
||||
351 | $permissionIds = []; |
||||
352 | |||||
353 | if (count($permissionPivots) != 0) { |
||||
354 | foreach ($permissionPivots as $permissionPivot) { |
||||
355 | $permissionIds[] = $permissionPivot->permission_id; |
||||
356 | } |
||||
357 | } |
||||
358 | |||||
359 | return $permissionIds; |
||||
360 | } |
||||
361 | |||||
362 | /** |
||||
363 | * Gets the role users. |
||||
364 | * |
||||
365 | * @param int $roleId The role identifier |
||||
366 | * |
||||
367 | * @return array The role users. |
||||
368 | */ |
||||
369 | public function getRoleUsers($roleId) |
||||
370 | { |
||||
371 | $queryRolesPivot = DB::table(config('roles.roleUserTable')); |
||||
372 | $users = []; |
||||
373 | |||||
374 | if ($roleId) { |
||||
375 | $queryRolesPivot->where('role_id', '=', $roleId); |
||||
376 | } |
||||
377 | |||||
378 | $pivots = $queryRolesPivot->get(); |
||||
379 | |||||
380 | if ($pivots->count() > 0) { |
||||
381 | foreach ($pivots as $pivot) { |
||||
382 | $users[] = $this->getUser($pivot->user_id); |
||||
383 | } |
||||
384 | } |
||||
385 | |||||
386 | return $users; |
||||
387 | } |
||||
388 | |||||
389 | /** |
||||
390 | * Gets the deleted permission and details (Roles and Users). |
||||
391 | * |
||||
392 | * @param int $id The identifier |
||||
393 | * |
||||
394 | * @return Permission The permission and details. |
||||
395 | */ |
||||
396 | public function getDeletedPermissionAndDetails($id) |
||||
397 | { |
||||
398 | $permission = $this->getDeletedPermission($id); |
||||
399 | $users = $this->getAllUsersForPermission($permission); |
||||
400 | $permission['users'] = $users; |
||||
401 | |||||
402 | return $permission; |
||||
403 | } |
||||
404 | |||||
405 | /** |
||||
406 | * Gets all users for permission. |
||||
407 | * |
||||
408 | * @param collection $permission The permission |
||||
409 | * |
||||
410 | * @return collection All users for permission. |
||||
411 | */ |
||||
412 | public function getAllUsersForPermission($permission) |
||||
413 | { |
||||
414 | $roles = $permission->roles()->get(); |
||||
415 | $users = []; |
||||
416 | foreach ($roles as $role) { |
||||
417 | $users[] = $this->getRoleUsers($role->id); |
||||
418 | } |
||||
419 | $users = array_shift($users); |
||||
420 | $permissionUserPivots = $this->getPermissionUsers($permission->id); |
||||
421 | if ($permissionUserPivots->count() > 0) { |
||||
422 | foreach ($permissionUserPivots as $permissionUserPivot) { |
||||
423 | $users[] = $this->getUser($permissionUserPivot->user_id); |
||||
424 | } |
||||
425 | } |
||||
426 | |||||
427 | return collect($users)->unique(); |
||||
0 ignored issues
–
show
|
|||||
428 | } |
||||
429 | |||||
430 | /** |
||||
431 | * Gets the dashboard data. |
||||
432 | * |
||||
433 | * @return array The dashboard data and view. |
||||
434 | */ |
||||
435 | public function getDashboardData() |
||||
436 | { |
||||
437 | $roles = $this->getRoles(); |
||||
438 | $permissions = $this->getPermissions(); |
||||
439 | $deletedRoleItems = $this->getDeletedRoles(); |
||||
440 | $deletedPermissionsItems = $this->getDeletedPermissions(); |
||||
441 | $users = $this->getUsers(); |
||||
442 | $sortedRolesWithUsers = $this->getSortedUsersWithRoles($roles, $users); |
||||
443 | $sortedRolesWithPermissionsAndUsers = $this->getSortedRolesWithPermissionsAndUsers($sortedRolesWithUsers, $permissions); |
||||
444 | $sortedPermissionsRolesUsers = $this->getSortedPermissonsWithRolesAndUsers($sortedRolesWithUsers, $permissions, $users); |
||||
445 | |||||
446 | $data = [ |
||||
447 | 'roles' => $roles, |
||||
448 | 'permissions' => $permissions, |
||||
449 | 'deletedRoleItems' => $deletedRoleItems, |
||||
450 | 'deletedPermissionsItems' => $deletedPermissionsItems, |
||||
451 | 'users' => $users, |
||||
452 | 'sortedRolesWithUsers' => $sortedRolesWithUsers, |
||||
453 | 'sortedRolesWithPermissionsAndUsers' => $sortedRolesWithPermissionsAndUsers, |
||||
454 | 'sortedPermissionsRolesUsers' => $sortedPermissionsRolesUsers, |
||||
455 | ]; |
||||
456 | |||||
457 | $view = 'laravelroles::laravelroles.crud.dashboard'; |
||||
458 | |||||
459 | return [ |
||||
460 | 'data' => $data, |
||||
461 | 'view' => $view, |
||||
462 | ]; |
||||
463 | } |
||||
464 | |||||
465 | /** |
||||
466 | * Restore all the deleted permissions. |
||||
467 | * |
||||
468 | * @return array |
||||
469 | */ |
||||
470 | public function restoreAllTheDeletedPermissions() |
||||
471 | { |
||||
472 | $deletedPermissions = $this->getDeletedPermissions()->get(); |
||||
473 | $deletedPermissionsCount = $deletedPermissions->count(); |
||||
474 | $status = 'error'; |
||||
475 | |||||
476 | if ($deletedPermissionsCount > 0) { |
||||
477 | foreach ($deletedPermissions as $deletedPermission) { |
||||
478 | $deletedPermission->restore(); |
||||
479 | } |
||||
480 | $status = 'success'; |
||||
481 | } |
||||
482 | |||||
483 | return [ |
||||
484 | 'status' => $status, |
||||
485 | 'count' => $deletedPermissionsCount, |
||||
486 | ]; |
||||
487 | } |
||||
488 | |||||
489 | /** |
||||
490 | * Restore all the deleted roles. |
||||
491 | * |
||||
492 | * @return array |
||||
493 | */ |
||||
494 | public function restoreAllTheDeletedRoles() |
||||
495 | { |
||||
496 | $deletedRoles = $this->getDeletedRoles()->get(); |
||||
497 | $deletedRolesCount = $deletedRoles->count(); |
||||
498 | $status = 'error'; |
||||
499 | |||||
500 | if ($deletedRolesCount > 0) { |
||||
501 | foreach ($deletedRoles as $deletedRole) { |
||||
502 | $deletedRole->restore(); |
||||
503 | } |
||||
504 | $status = 'success'; |
||||
505 | } |
||||
506 | |||||
507 | return [ |
||||
508 | 'status' => $status, |
||||
509 | 'count' => $deletedRolesCount, |
||||
510 | ]; |
||||
511 | } |
||||
512 | |||||
513 | /** |
||||
514 | * Restore a deleted permission. |
||||
515 | * |
||||
516 | * @param int $id The identifier |
||||
517 | * |
||||
518 | * @return collection |
||||
519 | */ |
||||
520 | public function restoreDeletedPermission($id) |
||||
521 | { |
||||
522 | $permission = $this->getDeletedPermission($id); |
||||
523 | $permission->restore(); |
||||
524 | |||||
525 | return $permission; |
||||
526 | } |
||||
527 | |||||
528 | /** |
||||
529 | * Restore a deleted role. |
||||
530 | * |
||||
531 | * @param int $id The identifier |
||||
532 | * |
||||
533 | * @return collection |
||||
534 | */ |
||||
535 | public function restoreDeletedRole($id) |
||||
536 | { |
||||
537 | $role = $this->getDeletedRole($id); |
||||
538 | $role->restore(); |
||||
539 | |||||
540 | return $role; |
||||
541 | } |
||||
542 | |||||
543 | /** |
||||
544 | * Retrieves permission roles. |
||||
545 | * |
||||
546 | * @param Permission $permission The permission |
||||
547 | * @param Collection $permissionsAndRolesPivot The permissions and roles pivot |
||||
548 | * @param Collection $sortedRolesWithUsers The sorted roles with users |
||||
549 | * |
||||
550 | * @return Collection of permission roles |
||||
551 | */ |
||||
552 | public function retrievePermissionRoles($permission, $permissionsAndRolesPivot, $sortedRolesWithUsers) |
||||
553 | { |
||||
554 | $roles = []; |
||||
555 | foreach ($permissionsAndRolesPivot as $permissionAndRoleKey => $permissionAndRoleValue) { |
||||
556 | if ($permission->id === $permissionAndRoleValue->permission_id) { |
||||
557 | foreach ($sortedRolesWithUsers as $sortedRolesWithUsersItemKey => $sortedRolesWithUsersItemValue) { |
||||
558 | if ($sortedRolesWithUsersItemValue['role']->id === $permissionAndRoleValue->role_id) { |
||||
559 | $roles[] = $sortedRolesWithUsersItemValue['role']; |
||||
560 | } |
||||
561 | } |
||||
562 | } |
||||
563 | } |
||||
564 | |||||
565 | return collect($roles); |
||||
0 ignored issues
–
show
$roles of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
566 | } |
||||
567 | |||||
568 | /** |
||||
569 | * Retrieves permission users. |
||||
570 | * |
||||
571 | * @param Permission $permission The permission |
||||
572 | * @param Collection $permissionsAndRolesPivot The permissions and roles pivot |
||||
573 | * @param Collection $sortedRolesWithUsers The sorted roles with users |
||||
574 | * @param Collection $permissionUsersPivot The permission users pivot |
||||
575 | * @param Collection $users The users |
||||
576 | * |
||||
577 | * @return Collection of Permission Users |
||||
578 | */ |
||||
579 | public function retrievePermissionUsers($permission, $permissionsAndRolesPivot, $sortedRolesWithUsers, $permissionUsersPivot, $appUsers) |
||||
580 | { |
||||
581 | $users = []; |
||||
582 | $userIds = []; |
||||
583 | |||||
584 | // Get Users from permissions associated with roles |
||||
585 | foreach ($permissionsAndRolesPivot as $permissionsAndRolesPivotItemKey => $permissionsAndRolesPivotItemValue) { |
||||
586 | if ($permission->id === $permissionsAndRolesPivotItemValue->permission_id) { |
||||
587 | foreach ($sortedRolesWithUsers as $sortedRolesWithUsersItemKey => $sortedRolesWithUsersItemValue) { |
||||
588 | if ($permissionsAndRolesPivotItemValue->role_id === $sortedRolesWithUsersItemValue['role']->id) { |
||||
589 | foreach ($sortedRolesWithUsersItemValue['users'] as $sortedRolesWithUsersItemValueUser) { |
||||
590 | $users[] = $sortedRolesWithUsersItemValueUser; |
||||
591 | } |
||||
592 | } |
||||
593 | } |
||||
594 | } |
||||
595 | } |
||||
596 | |||||
597 | // Setup Users IDs from permissions associated with roles |
||||
598 | foreach ($users as $userKey => $userValue) { |
||||
599 | $userIds[] = $userValue->id; |
||||
600 | } |
||||
601 | |||||
602 | // Get Users from permissions pivot table that are not already in users from permissions associated with roles |
||||
603 | foreach ($permissionUsersPivot as $permissionUsersPivotKey => $permissionUsersPivotItem) { |
||||
604 | if (!in_array($permissionUsersPivotItem->user_id, $userIds) && $permission->id === $permissionUsersPivotItem->permission_id) { |
||||
605 | foreach ($appUsers as $appUser) { |
||||
606 | if ($appUser->id === $permissionUsersPivotItem->user_id) { |
||||
607 | $users[] = $appUser; |
||||
608 | } |
||||
609 | } |
||||
610 | } |
||||
611 | } |
||||
612 | |||||
613 | return collect($users); |
||||
0 ignored issues
–
show
$users of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
614 | } |
||||
615 | |||||
616 | /** |
||||
617 | * Gets the sorted users with roles. |
||||
618 | * |
||||
619 | * @param collection $roles The roles |
||||
620 | * @param collection $users The users |
||||
621 | * |
||||
622 | * @return collection The sorted users with roles. |
||||
623 | */ |
||||
624 | public function getSortedUsersWithRoles($roles, $users) |
||||
625 | { |
||||
626 | $sortedUsersWithRoles = []; |
||||
627 | |||||
628 | foreach ($roles as $rolekey => $roleValue) { |
||||
629 | $sortedUsersWithRoles[] = [ |
||||
630 | 'role' => $roleValue, |
||||
631 | 'users' => [], |
||||
632 | ]; |
||||
633 | foreach ($users as $user) { |
||||
634 | foreach ($user->roles as $userRole) { |
||||
635 | if ($userRole->id === $sortedUsersWithRoles[$rolekey]['role']['id']) { |
||||
636 | $sortedUsersWithRoles[$rolekey]['users'][] = $user; |
||||
637 | } |
||||
638 | } |
||||
639 | } |
||||
640 | } |
||||
641 | |||||
642 | return collect($sortedUsersWithRoles); |
||||
0 ignored issues
–
show
$sortedUsersWithRoles of type array|array<mixed,array<string,array|mixed>> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
643 | } |
||||
644 | |||||
645 | /** |
||||
646 | * Gets the sorted roles with permissions. |
||||
647 | * |
||||
648 | * @param collection $sortedRolesWithUsers The sorted roles with users |
||||
649 | * @param collection $permissions The permissions |
||||
650 | * |
||||
651 | * @return collection The sorted roles with permissions. |
||||
652 | */ |
||||
653 | public function getSortedRolesWithPermissionsAndUsers($sortedRolesWithUsers, $permissions) |
||||
654 | { |
||||
655 | $sortedRolesWithPermissions = []; |
||||
656 | $permissionsAndRoles = $this->getPermissionsWithRoles(); |
||||
657 | |||||
658 | foreach ($sortedRolesWithUsers as $sortedRolekey => $sortedRoleValue) { |
||||
659 | $role = $sortedRoleValue['role']; |
||||
660 | $users = $sortedRoleValue['users']; |
||||
661 | $sortedRolesWithPermissions[] = [ |
||||
662 | 'role' => $role, |
||||
663 | 'permissions' => collect([]), |
||||
0 ignored issues
–
show
array() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
664 | 'users' => collect([]), |
||||
665 | ]; |
||||
666 | |||||
667 | // Add Permission with Role |
||||
668 | foreach ($permissionsAndRoles as $permissionAndRole) { |
||||
669 | if ($permissionAndRole->role_id == $role->id) { |
||||
670 | foreach ($permissions as $permissionKey => $permissionValue) { |
||||
671 | if ($permissionValue->id == $permissionAndRole->permission_id) { |
||||
672 | $sortedRolesWithPermissions[$sortedRolekey]['permissions'][] = $permissionValue; |
||||
673 | } |
||||
674 | } |
||||
675 | } |
||||
676 | } |
||||
677 | |||||
678 | // Add Users with Role |
||||
679 | foreach ($users as $user) { |
||||
680 | foreach ($user->roles as $userRole) { |
||||
681 | if ($userRole->id === $sortedRolesWithPermissions[$sortedRolekey]['role']['id']) { |
||||
682 | $sortedRolesWithPermissions[$sortedRolekey]['users'][] = $user; |
||||
683 | } |
||||
684 | } |
||||
685 | } |
||||
686 | } |
||||
687 | |||||
688 | return collect($sortedRolesWithPermissions); |
||||
0 ignored issues
–
show
$sortedRolesWithPermissions of type array|array<mixed,array<...port\Collection|mixed>> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
689 | } |
||||
690 | |||||
691 | /** |
||||
692 | * Gets the sorted permissons with roles and users. |
||||
693 | * |
||||
694 | * @param collection $sortedRolesWithUsers The sorted roles with users |
||||
695 | * @param collection $permissions The permissions |
||||
696 | * @param colection $users The users |
||||
0 ignored issues
–
show
The type
jeremykenedy\LaravelRoles\Traits\colection was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
697 | * |
||||
698 | * @return collection The sorted permissons with roles and users. |
||||
699 | */ |
||||
700 | public function getSortedPermissonsWithRolesAndUsers($sortedRolesWithUsers, $permissions, $users) |
||||
701 | { |
||||
702 | $sortedPermissionsWithRoles = []; |
||||
703 | $permissionsAndRolesPivot = $this->getPermissionsWithRoles(); |
||||
704 | $permissionUsersPivot = $this->getPermissionUsers(); |
||||
705 | |||||
706 | foreach ($permissions as $permissionKey => $permissionValue) { |
||||
707 | $sortedPermissionsWithRoles[] = [ |
||||
708 | 'permission' => $permissionValue, |
||||
709 | 'roles' => $this->retrievePermissionRoles($permissionValue, $permissionsAndRolesPivot, $sortedRolesWithUsers), |
||||
710 | 'users' => $this->retrievePermissionUsers($permissionValue, $permissionsAndRolesPivot, $sortedRolesWithUsers, $permissionUsersPivot, $users), |
||||
711 | ]; |
||||
712 | } |
||||
713 | |||||
714 | return collect($sortedPermissionsWithRoles); |
||||
0 ignored issues
–
show
$sortedPermissionsWithRoles of type array|array<mixed,array<...aits\Collection|mixed>> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
715 | } |
||||
716 | |||||
717 | /** |
||||
718 | * Removes an users and permissions from role. |
||||
719 | * |
||||
720 | * @param Role $role The role |
||||
721 | * |
||||
722 | * @return void |
||||
723 | */ |
||||
724 | public function removeUsersAndPermissionsFromRole($role) |
||||
725 | { |
||||
726 | $users = $this->getUsers(); |
||||
727 | $roles = $this->getRoles(); |
||||
728 | $sortedRolesWithUsers = $this->getSortedUsersWithRoles($roles, $users); |
||||
729 | $roleUsers = []; |
||||
730 | |||||
731 | // Remove Users Attached to Role |
||||
732 | foreach ($sortedRolesWithUsers as $sortedRolesWithUsersKey => $sortedRolesWithUsersValue) { |
||||
733 | if ($sortedRolesWithUsersValue['role'] == $role) { |
||||
734 | $roleUsers[] = $sortedRolesWithUsersValue['users']; |
||||
735 | } |
||||
736 | } |
||||
737 | foreach ($roleUsers as $roleUserKey => $roleUserValue) { |
||||
738 | if (!empty($roleUserValue)) { |
||||
739 | $roleUserValue[$roleUserKey]->detachRole($role); |
||||
740 | } |
||||
741 | } |
||||
742 | |||||
743 | // Remove Permissions from Role |
||||
744 | $role->detachAllPermissions(); |
||||
745 | } |
||||
746 | |||||
747 | /** |
||||
748 | * Removes an users and permissions from permission. |
||||
749 | * |
||||
750 | * @param Permission $permission The Permission |
||||
751 | * |
||||
752 | * @return void |
||||
753 | */ |
||||
754 | public function removeUsersAndRolesFromPermissions($permission) |
||||
755 | { |
||||
756 | $users = $this->getUsers(); |
||||
757 | $roles = $this->getRoles(); |
||||
758 | $permissions = $this->getPermissions(); |
||||
759 | $sortedRolesWithUsers = $this->getSortedUsersWithRoles($roles, $users); |
||||
760 | $sortedPermissionsRolesUsers = $this->getSortedPermissonsWithRolesAndUsers($sortedRolesWithUsers, $permissions, $users); |
||||
761 | |||||
762 | foreach ($sortedPermissionsRolesUsers as $sortedPermissionsRolesUsersKey => $sortedPermissionsRolesUsersItem) { |
||||
763 | if ($sortedPermissionsRolesUsersItem['permission']->id === $permission->id) { |
||||
764 | // Remove Permission from roles |
||||
765 | foreach ($sortedPermissionsRolesUsersItem['roles'] as $permissionRoleKey => $permissionRoleItem) { |
||||
766 | $permissionRoleItem->detachPermission($permission); |
||||
767 | } |
||||
768 | |||||
769 | // Permission Permission from Users |
||||
770 | foreach ($sortedPermissionsRolesUsersItem['users'] as $permissionUserKey => $permissionUserItem) { |
||||
771 | $permissionUserItem->detachPermission($permission); |
||||
772 | } |
||||
773 | } |
||||
774 | } |
||||
775 | } |
||||
776 | |||||
777 | /** |
||||
778 | * Stores role with permissions. |
||||
779 | * |
||||
780 | * @param array $roleData The role data |
||||
781 | * @param object $rolePermissions The role permissions |
||||
782 | * |
||||
783 | * @return collection The Role |
||||
784 | */ |
||||
785 | public function storeRoleWithPermissions($roleData, $rolePermissions) |
||||
786 | { |
||||
787 | $role = config('roles.models.role')::create($roleData); |
||||
788 | |||||
789 | if ($rolePermissions) { |
||||
0 ignored issues
–
show
|
|||||
790 | $permissionIds = []; |
||||
791 | foreach ($rolePermissions as $permission) { |
||||
792 | $permissionIds[] = json_decode($permission)->id; |
||||
793 | } |
||||
794 | $role->syncPermissions($permissionIds); |
||||
795 | } |
||||
796 | |||||
797 | return $role; |
||||
798 | } |
||||
799 | |||||
800 | /** |
||||
801 | * Update Role with permissions. |
||||
802 | * |
||||
803 | * @param int $id The identifier |
||||
804 | * @param array $roleData The role data |
||||
805 | * @param object $rolePermissions The role permissions |
||||
806 | * |
||||
807 | * @return collection The Role |
||||
808 | */ |
||||
809 | public function updateRoleWithPermissions($id, $roleData, $rolePermissions) |
||||
810 | { |
||||
811 | $role = config('roles.models.role')::findOrFail($id); |
||||
812 | |||||
813 | $role->fill($roleData); |
||||
814 | $role->save(); |
||||
815 | $role->detachAllPermissions(); |
||||
816 | |||||
817 | if ($rolePermissions) { |
||||
0 ignored issues
–
show
|
|||||
818 | $permissionIds = []; |
||||
819 | foreach ($rolePermissions as $permission) { |
||||
820 | $permissionIds[] = json_decode($permission)->id; |
||||
821 | } |
||||
822 | $role->syncPermissions($permissionIds); |
||||
823 | } |
||||
824 | |||||
825 | return $role; |
||||
826 | } |
||||
827 | |||||
828 | /** |
||||
829 | * Stores a new permission. |
||||
830 | * |
||||
831 | * @param array $permissionData The permission data |
||||
832 | * |
||||
833 | * @return collection The New Permission |
||||
834 | */ |
||||
835 | public function storeNewPermission($permissionData) |
||||
836 | { |
||||
837 | return config('roles.models.permission')::create($permissionData); |
||||
838 | } |
||||
839 | |||||
840 | /** |
||||
841 | * Update a permission. |
||||
842 | * |
||||
843 | * @param int $id The identifier |
||||
844 | * @param array $permissionData The permission data |
||||
845 | * |
||||
846 | * @return collection |
||||
847 | */ |
||||
848 | public function updatePermission($id, $permissionData) |
||||
849 | { |
||||
850 | $permission = config('roles.models.permission')::findOrFail($id); |
||||
851 | $permission->fill($permissionData); |
||||
852 | $permission->save(); |
||||
853 | |||||
854 | return $permission; |
||||
855 | } |
||||
856 | } |
||||
857 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths