]> BookStack Code Mirror - bookstack/blob - app/Auth/Permissions/PermissionsRepo.php
Added more complexity in an attempt to make ldap host failover fit
[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         $this->permissionBuilder->rebuildForRole($role);
61
62         Activity::add(ActivityType::ROLE_CREATE, $role);
63
64         return $role;
65     }
66
67     /**
68      * Updates an existing role.
69      * Ensure Admin role always have core permissions.
70      */
71     public function updateRole($roleId, array $roleData)
72     {
73         $role = $this->getRoleById($roleId);
74
75         $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
76         if ($role->system_name === 'admin') {
77             $permissions = array_merge($permissions, [
78                 'users-manage',
79                 'user-roles-manage',
80                 'restrictions-manage-all',
81                 'restrictions-manage-own',
82                 'settings-manage',
83             ]);
84         }
85
86         $this->assignRolePermissions($role, $permissions);
87
88         $role->fill($roleData);
89         $role->mfa_enforced = ($roleData['mfa_enforced'] ?? 'false') === 'true';
90         $role->save();
91         $this->permissionBuilder->rebuildForRole($role);
92
93         Activity::add(ActivityType::ROLE_UPDATE, $role);
94     }
95
96     /**
97      * Assign a list of permission names to a role.
98      */
99     protected function assignRolePermissions(Role $role, array $permissionNameArray = [])
100     {
101         $permissions = [];
102         $permissionNameArray = array_values($permissionNameArray);
103
104         if ($permissionNameArray) {
105             $permissions = RolePermission::query()
106                 ->whereIn('name', $permissionNameArray)
107                 ->pluck('id')
108                 ->toArray();
109         }
110
111         $role->permissions()->sync($permissions);
112     }
113
114     /**
115      * Delete a role from the system.
116      * Check it's not an admin role or set as default before deleting.
117      * If an migration Role ID is specified the users assign to the current role
118      * will be added to the role of the specified id.
119      *
120      * @throws PermissionsException
121      * @throws Exception
122      */
123     public function deleteRole($roleId, $migrateRoleId)
124     {
125         $role = $this->getRoleById($roleId);
126
127         // Prevent deleting admin role or default registration role.
128         if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
129             throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
130         } elseif ($role->id === intval(setting('registration-role'))) {
131             throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
132         }
133
134         if ($migrateRoleId) {
135             $newRole = Role::query()->find($migrateRoleId);
136             if ($newRole) {
137                 $users = $role->users()->pluck('id')->toArray();
138                 $newRole->users()->sync($users);
139             }
140         }
141
142         $role->entityPermissions()->delete();
143         $role->jointPermissions()->delete();
144         Activity::add(ActivityType::ROLE_DELETE, $role);
145         $role->delete();
146     }
147 }