3 namespace BookStack\Auth\Permissions;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Role;
7 use BookStack\Exceptions\PermissionsException;
8 use BookStack\Facades\Activity;
10 use Illuminate\Database\Eloquent\Collection;
14 protected $permission;
16 protected $permissionService;
18 protected $systemRoles = ['admin', 'public'];
21 * PermissionsRepo constructor.
23 public function __construct(RolePermission $permission, Role $role, PermissionService $permissionService)
25 $this->permission = $permission;
27 $this->permissionService = $permissionService;
31 * Get all the user roles from the system.
33 public function getAllRoles(): Collection
35 return $this->role->all();
39 * Get all the roles except for the provided one.
41 public function getAllRolesExcept(Role $role): Collection
43 return $this->role->where('id', '!=', $role->id)->get();
47 * Get a role via its ID.
49 public function getRoleById($id): Role
51 return $this->role->newQuery()->findOrFail($id);
55 * Save a new role into the system.
57 public function saveNewRole(array $roleData): Role
59 $role = $this->role->newInstance($roleData);
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);
71 * Updates an existing role.
72 * Ensure Admin role always have core permissions.
74 public function updateRole($roleId, array $roleData)
76 /** @var Role $role */
77 $role = $this->role->newQuery()->findOrFail($roleId);
79 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
80 if ($role->system_name === 'admin') {
81 $permissions = array_merge($permissions, [
84 'restrictions-manage-all',
85 'restrictions-manage-own',
90 $this->assignRolePermissions($role, $permissions);
92 $role->fill($roleData);
94 $this->permissionService->buildJointPermissionForRole($role);
95 Activity::add(ActivityType::ROLE_UPDATE, $role);
99 * Assign an list of permission names to an role.
101 protected function assignRolePermissions(Role $role, array $permissionNameArray = [])
104 $permissionNameArray = array_values($permissionNameArray);
106 if ($permissionNameArray) {
107 $permissions = $this->permission->newQuery()
108 ->whereIn('name', $permissionNameArray)
113 $role->permissions()->sync($permissions);
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.
122 * @throws PermissionsException
125 public function deleteRole($roleId, $migrateRoleId)
127 /** @var Role $role */
128 $role = $this->role->newQuery()->findOrFail($roleId);
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'));
137 if ($migrateRoleId) {
138 $newRole = $this->role->newQuery()->find($migrateRoleId);
140 $users = $role->users()->pluck('id')->toArray();
141 $newRole->users()->sync($users);
145 $this->permissionService->deleteJointPermissionsForRole($role);
146 Activity::add(ActivityType::ROLE_DELETE, $role);