1 <?php namespace BookStack\Auth\Permissions;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Auth\Role;
5 use BookStack\Exceptions\PermissionsException;
6 use BookStack\Facades\Activity;
8 use Illuminate\Database\Eloquent\Collection;
13 protected $permission;
15 protected $permissionService;
17 protected $systemRoles = ['admin', 'public'];
20 * PermissionsRepo constructor.
22 public function __construct(RolePermission $permission, Role $role, PermissionService $permissionService)
24 $this->permission = $permission;
26 $this->permissionService = $permissionService;
30 * Get all the user roles from the system.
32 public function getAllRoles(): Collection
34 return $this->role->all();
38 * Get all the roles except for the provided one.
40 public function getAllRolesExcept(Role $role): Collection
42 return $this->role->where('id', '!=', $role->id)->get();
46 * Get a role via its ID.
48 public function getRoleById($id): Role
50 return $this->role->newQuery()->findOrFail($id);
54 * Save a new role into the system.
56 public function saveNewRole(array $roleData): Role
58 $role = $this->role->newInstance($roleData);
61 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
62 $this->assignRolePermissions($role, $permissions);
63 $this->permissionService->buildJointPermissionForRole($role);
64 Activity::add(ActivityType::ROLE_CREATE, $role);
69 * Updates an existing role.
70 * Ensure Admin role always have core permissions.
72 public function updateRole($roleId, array $roleData)
74 /** @var Role $role */
75 $role = $this->role->newQuery()->findOrFail($roleId);
77 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
78 if ($role->system_name === 'admin') {
79 $permissions = array_merge($permissions, [
82 'restrictions-manage-all',
83 'restrictions-manage-own',
88 $this->assignRolePermissions($role, $permissions);
90 $role->fill($roleData);
92 $this->permissionService->buildJointPermissionForRole($role);
93 Activity::add(ActivityType::ROLE_UPDATE, $role);
97 * Assign an list of permission names to an role.
99 protected function assignRolePermissions(Role $role, array $permissionNameArray = [])
102 $permissionNameArray = array_values($permissionNameArray);
104 if ($permissionNameArray) {
105 $permissions = $this->permission->newQuery()
106 ->whereIn('name', $permissionNameArray)
111 $role->permissions()->sync($permissions);
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 * @throws PermissionsException
122 public function deleteRole($roleId, $migrateRoleId)
124 /** @var Role $role */
125 $role = $this->role->newQuery()->findOrFail($roleId);
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 } else if ($role->id === intval(setting('registration-role'))) {
131 throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
134 if ($migrateRoleId) {
135 $newRole = $this->role->newQuery()->find($migrateRoleId);
137 $users = $role->users()->pluck('id')->toArray();
138 $newRole->users()->sync($users);
142 $this->permissionService->deleteJointPermissionsForRole($role);
143 Activity::add(ActivityType::ROLE_DELETE, $role);