]> BookStack Code Mirror - bookstack/blob - app/Auth/Permissions/PermissionFormData.php
Added method for using enity ownership in relation queries
[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 permissions with assigned users.
32      */
33     public function permissionsWithUsers(): array
34     {
35         return $this->entity->permissions()
36             ->with('user')
37             ->whereNotNull('user_id')
38             ->get()
39             ->sortBy('user.name')
40             ->all();
41     }
42
43     /**
44      * Get the roles that don't yet have specific permissions for the
45      * entity we're managing permissions for.
46      */
47     public function rolesNotAssigned(): array
48     {
49         $assigned = $this->entity->permissions()->whereNotNull('role_id')->pluck('role_id');
50         return Role::query()
51             ->where('system_name', '!=', 'admin')
52             ->whereNotIn('id', $assigned)
53             ->orderBy('display_name', 'asc')
54             ->get()
55             ->all();
56     }
57
58     /**
59      * Get the entity permission for the "Everyone Else" option.
60      */
61     public function everyoneElseEntityPermission(): EntityPermission
62     {
63         /** @var ?EntityPermission $permission */
64         $permission = $this->entity->permissions()
65             ->whereNull(['role_id', 'user_id'])
66             ->first();
67         return $permission ?? (new EntityPermission());
68     }
69
70     /**
71      * Check if the "Everyone else" option is inheriting default role system permissions.
72      * Is determined by any system entity_permission existing for the current entity.
73      */
74     public function everyoneElseInheriting(): bool
75     {
76         return !$this->entity->permissions()
77             ->whereNull(['role_id', 'user_id'])
78             ->exists();
79     }
80 }