]> BookStack Code Mirror - bookstack/blob - app/Auth/Permissions/PermissionFormData.php
Added force option for update-url command
[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             ->where('role_id', '!=', 0)
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()->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             ->where('role_id', '=', 0)
53             ->first();
54         return $permission ?? (new EntityPermission());
55     }
56
57     /**
58      * Get the "Everyone Else" role entry.
59      */
60     public function everyoneElseRole(): Role
61     {
62         return (new Role())->forceFill([
63             'id' => 0,
64             'display_name' => trans('entities.permissions_role_everyone_else'),
65             'description' => trans('entities.permissions_role_everyone_else_desc'),
66         ]);
67     }
68 }