]> BookStack Code Mirror - bookstack/blob - app/Permissions/PermissionFormData.php
Turned off autocomplete for TOTP codes
[bookstack] / app / Permissions / PermissionFormData.php
1 <?php
2
3 namespace BookStack\Permissions;
4
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Permissions\Models\EntityPermission;
7 use BookStack\Users\Models\Role;
8
9 class PermissionFormData
10 {
11     protected Entity $entity;
12
13     public function __construct(Entity $entity)
14     {
15         $this->entity = $entity;
16     }
17
18     /**
19      * Get the permissions with assigned roles.
20      */
21     public function permissionsWithRoles(): array
22     {
23         return $this->entity->permissions()
24             ->with('role')
25             ->where('role_id', '!=', 0)
26             ->get()
27             ->sortBy('role.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 entity permission for the "Everyone Else" option.
48      */
49     public function everyoneElseEntityPermission(): EntityPermission
50     {
51         /** @var ?EntityPermission $permission */
52         $permission = $this->entity->permissions()
53             ->where('role_id', '=', 0)
54             ->first();
55         return $permission ?? (new EntityPermission());
56     }
57
58     /**
59      * Get the "Everyone Else" role entry.
60      */
61     public function everyoneElseRole(): Role
62     {
63         return (new Role())->forceFill([
64             'id' => 0,
65             'display_name' => trans('entities.permissions_role_everyone_else'),
66             'description' => trans('entities.permissions_role_everyone_else_desc'),
67         ]);
68     }
69 }