]> BookStack Code Mirror - bookstack/blob - app/Auth/Permissions/PermissionsRepo.php
Started new permission-caching/querying model
[bookstack] / app / Auth / Permissions / PermissionsRepo.php
1 <?php
2
3 namespace BookStack\Auth\Permissions;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Role;
7 use BookStack\Exceptions\PermissionsException;
8 use BookStack\Facades\Activity;
9 use Exception;
10 use Illuminate\Database\Eloquent\Collection;
11
12 class PermissionsRepo
13 {
14     protected JointPermissionBuilder $permissionBuilder;
15     protected $systemRoles = ['admin', 'public'];
16
17     /**
18      * PermissionsRepo constructor.
19      */
20     public function __construct(JointPermissionBuilder $permissionBuilder)
21     {
22         $this->permissionBuilder = $permissionBuilder;
23     }
24
25     /**
26      * Get all the user roles from the system.
27      */
28     public function getAllRoles(): Collection
29     {
30         return Role::query()->get();
31     }
32
33     /**
34      * Get all the roles except for the provided one.
35      */
36     public function getAllRolesExcept(Role $role): Collection
37     {
38         return Role::query()->where('id', '!=', $role->id)->get();
39     }
40
41     /**
42      * Get a role via its ID.
43      */
44     public function getRoleById($id): Role
45     {
46         return Role::query()->findOrFail($id);
47     }
48
49     /**
50      * Save a new role into the system.
51      */
52     public function saveNewRole(array $roleData): Role
53     {
54         $role = new Role($roleData);
55         $role->mfa_enforced = ($roleData['mfa_enforced'] ?? 'false') === 'true';
56         $role->save();
57
58         $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
59         $this->assignRolePermissions($role, $permissions);
60
61         Activity::add(ActivityType::ROLE_CREATE, $role);
62
63         return $role;
64     }
65
66     /**
67      * Updates an existing role.
68      * Ensure Admin role always have core permissions.
69      */
70     public function updateRole($roleId, array $roleData)
71     {
72         $role = $this->getRoleById($roleId);
73
74         $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
75         if ($role->system_name === 'admin') {
76             $permissions = array_merge($permissions, [
77                 'users-manage',
78                 'user-roles-manage',
79                 'restrictions-manage-all',
80                 'restrictions-manage-own',
81                 'settings-manage',
82             ]);
83         }
84
85         $this->assignRolePermissions($role, $permissions);
86
87         $role->fill($roleData);
88         $role->mfa_enforced = ($roleData['mfa_enforced'] ?? 'false') === 'true';
89         $role->save();
90
91         Activity::add(ActivityType::ROLE_UPDATE, $role);
92     }
93
94     /**
95      * Assign a list of permission names to a role.
96      */
97     protected function assignRolePermissions(Role $role, array $permissionNameArray = [])
98     {
99         $permissions = [];
100         $permissionNameArray = array_values($permissionNameArray);
101
102         if ($permissionNameArray) {
103             $permissions = RolePermission::query()
104                 ->whereIn('name', $permissionNameArray)
105                 ->pluck('id')
106                 ->toArray();
107         }
108
109         $role->permissions()->sync($permissions);
110     }
111
112     /**
113      * Delete a role from the system.
114      * Check it's not an admin role or set as default before deleting.
115      * If an migration Role ID is specified the users assign to the current role
116      * will be added to the role of the specified id.
117      *
118      * @throws PermissionsException
119      * @throws Exception
120      */
121     public function deleteRole($roleId, $migrateRoleId)
122     {
123         $role = $this->getRoleById($roleId);
124
125         // Prevent deleting admin role or default registration role.
126         if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
127             throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
128         } elseif ($role->id === intval(setting('registration-role'))) {
129             throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
130         }
131
132         if ($migrateRoleId) {
133             $newRole = Role::query()->find($migrateRoleId);
134             if ($newRole) {
135                 $users = $role->users()->pluck('id')->toArray();
136                 $newRole->users()->sync($users);
137             }
138         }
139
140         $role->entityPermissions()->delete();
141         $role->jointPermissions()->delete();
142         Activity::add(ActivityType::ROLE_DELETE, $role);
143         $role->delete();
144     }
145 }