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