]> BookStack Code Mirror - bookstack/blob - app/Auth/Permissions/PermissionFormData.php
Centralised handling of permission form data to own class
[bookstack] / app / Auth / Permissions / PermissionFormData.php
1 <?php
2
3 namespace BookStack\Auth\Permissions;
4
5 use BookStack\Auth\Role;
6 use BookStack\Entities\Models\Entity;
7
8 class PermissionFormData
9 {
10     protected Entity $entity;
11
12     public function __construct(Entity $entity)
13     {
14         $this->entity = $entity;
15     }
16
17     /**
18      * Get the roles with permissions assigned.
19      */
20     public function rolesWithPermissions(): array
21     {
22         return $this->entity->permissions()
23             ->with('role')
24             ->where('role_id', '!=', 0)
25             ->get(['id', 'role_id'])
26             ->pluck('role')
27             ->sortBy('display_name')
28             ->all();
29     }
30
31     /**
32      * Get the roles that don't yet have specific permissions for the
33      * entity we're managing permissions for.
34      */
35     public function rolesNotAssigned(): array
36     {
37         $assigned = $this->entity->permissions()->pluck('role_id');
38         return Role::query()
39             ->where('system_name', '!=', 'admin')
40             ->whereNotIn('id', $assigned)
41             ->orderBy('display_name', 'asc')
42             ->get()
43             ->all();
44     }
45
46     /**
47      * Get the "Everyone Else" role entry.
48      */
49     public function everyoneElseRole(): Role
50     {
51         return (new Role())->forceFill([
52             'id' => 0,
53             'display_name' => 'Everyone Else',
54             'description' => 'Set permissions for all roles not specifically overridden.'
55         ]);
56     }
57 }