]> BookStack Code Mirror - bookstack/blob - app/Auth/Permissions/PermissionsRepo.php
Updated callout link formatting
[bookstack] / app / Auth / Permissions / PermissionsRepo.php
1 <?php namespace BookStack\Auth\Permissions;
2
3 use BookStack\Auth\Role;
4 use BookStack\Exceptions\PermissionsException;
5 use Exception;
6 use Illuminate\Database\Eloquent\Collection;
7 use Illuminate\Support\Str;
8
9 class PermissionsRepo
10 {
11
12     protected $permission;
13     protected $role;
14     protected $permissionService;
15
16     protected $systemRoles = ['admin', 'public'];
17
18     /**
19      * PermissionsRepo constructor.
20      */
21     public function __construct(RolePermission $permission, Role $role, PermissionService $permissionService)
22     {
23         $this->permission = $permission;
24         $this->role = $role;
25         $this->permissionService = $permissionService;
26     }
27
28     /**
29      * Get all the user roles from the system.
30      */
31     public function getAllRoles(): Collection
32     {
33         return $this->role->all();
34     }
35
36     /**
37      * Get all the roles except for the provided one.
38      */
39     public function getAllRolesExcept(Role $role): Collection
40     {
41         return $this->role->where('id', '!=', $role->id)->get();
42     }
43
44     /**
45      * Get a role via its ID.
46      */
47     public function getRoleById($id): Role
48     {
49         return $this->role->newQuery()->findOrFail($id);
50     }
51
52     /**
53      * Save a new role into the system.
54      */
55     public function saveNewRole(array $roleData): Role
56     {
57         $role = $this->role->newInstance($roleData);
58         $role->save();
59
60         $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
61         $this->assignRolePermissions($role, $permissions);
62         $this->permissionService->buildJointPermissionForRole($role);
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         /** @var Role $role */
73         $role = $this->role->newQuery()->findOrFail($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->save();
90         $this->permissionService->buildJointPermissionForRole($role);
91     }
92
93     /**
94      * Assign an list of permission names to an role.
95      */
96     public function assignRolePermissions(Role $role, array $permissionNameArray = [])
97     {
98         $permissions = [];
99         $permissionNameArray = array_values($permissionNameArray);
100
101         if ($permissionNameArray) {
102             $permissions = $this->permission->newQuery()
103                 ->whereIn('name', $permissionNameArray)
104                 ->pluck('id')
105                 ->toArray();
106         }
107
108         $role->permissions()->sync($permissions);
109     }
110
111     /**
112      * Delete a role from the system.
113      * Check it's not an admin role or set as default before deleting.
114      * If an migration Role ID is specified the users assign to the current role
115      * will be added to the role of the specified id.
116      * @throws PermissionsException
117      * @throws Exception
118      */
119     public function deleteRole($roleId, $migrateRoleId)
120     {
121         /** @var Role $role */
122         $role = $this->role->newQuery()->findOrFail($roleId);
123
124         // Prevent deleting admin role or default registration role.
125         if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
126             throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
127         } else if ($role->id === intval(setting('registration-role'))) {
128             throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
129         }
130
131         if ($migrateRoleId) {
132             $newRole = $this->role->newQuery()->find($migrateRoleId);
133             if ($newRole) {
134                 $users = $role->users()->pluck('id')->toArray();
135                 $newRole->users()->sync($users);
136             }
137         }
138
139         $this->permissionService->deleteJointPermissionsForRole($role);
140         $role->delete();
141     }
142 }