]> BookStack Code Mirror - bookstack/blob - app/Auth/Permissions/PermissionFormData.php
18d45591fd2948bf7602069ba3a226d0cc3fbb77
[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 permissions with assigned roles.
19      */
20     public function permissionsWithRoles(): array
21     {
22         return $this->entity->permissions()
23             ->with('role')
24             ->whereNotNull('role_id')
25             ->get()
26             ->sortBy('role.display_name')
27             ->all();
28     }
29
30     /**
31      * Get the roles that don't yet have specific permissions for the
32      * entity we're managing permissions for.
33      */
34     public function rolesNotAssigned(): array
35     {
36         $assigned = $this->entity->permissions()->whereNotNull('role_id')->pluck('role_id');
37         return Role::query()
38             ->where('system_name', '!=', 'admin')
39             ->whereNotIn('id', $assigned)
40             ->orderBy('display_name', 'asc')
41             ->get()
42             ->all();
43     }
44
45     /**
46      * Get the entity permission for the "Everyone Else" option.
47      */
48     public function everyoneElseEntityPermission(): EntityPermission
49     {
50         /** @var ?EntityPermission $permission */
51         $permission = $this->entity->permissions()
52             ->whereNull(['role_id', 'user_id'])
53             ->first();
54         return $permission ?? (new EntityPermission());
55     }
56
57     /**
58      * Check if the "Everyone else" option is inheriting default role system permissions.
59      * Is determined by any system entity_permission existing for the current entity.
60      */
61     public function everyoneElseInheriting(): bool
62     {
63         return !$this->entity->permissions()
64             ->whereNull(['role_id', 'user_id'])
65             ->exists();
66     }
67 }