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