1 <?php namespace BookStack\Auth\Permissions;
3 use BookStack\Auth\Permissions;
4 use BookStack\Auth\Role;
5 use BookStack\Exceptions\PermissionsException;
6 use Illuminate\Support\Str;
11 protected $permission;
13 protected $permissionService;
15 protected $systemRoles = ['admin', 'public'];
18 * PermissionsRepo constructor.
19 * @param RolePermission $permission
21 * @param \BookStack\Auth\Permissions\PermissionService $permissionService
23 public function __construct(RolePermission $permission, Role $role, Permissions\PermissionService $permissionService)
25 $this->permission = $permission;
27 $this->permissionService = $permissionService;
31 * Get all the user roles from the system.
32 * @return \Illuminate\Database\Eloquent\Collection|static[]
34 public function getAllRoles()
36 return $this->role->all();
40 * Get all the roles except for the provided one.
44 public function getAllRolesExcept(Role $role)
46 return $this->role->where('id', '!=', $role->id)->get();
50 * Get a role via its ID.
54 public function getRoleById($id)
56 return $this->role->findOrFail($id);
60 * Save a new role into the system.
61 * @param array $roleData
64 public function saveNewRole($roleData)
66 $role = $this->role->newInstance($roleData);
67 $role->name = str_replace(' ', '-', strtolower($roleData['display_name']));
68 // Prevent duplicate names
69 while ($this->role->where('name', '=', $role->name)->count() > 0) {
70 $role->name .= strtolower(Str::random(2));
74 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
75 $this->assignRolePermissions($role, $permissions);
76 $this->permissionService->buildJointPermissionForRole($role);
81 * Updates an existing role.
82 * Ensure Admin role always have core permissions.
85 * @throws PermissionsException
87 public function updateRole($roleId, $roleData)
89 $role = $this->role->findOrFail($roleId);
91 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
92 if ($role->system_name === 'admin') {
93 $permissions = array_merge($permissions, [
96 'restrictions-manage-all',
97 'restrictions-manage-own',
102 $this->assignRolePermissions($role, $permissions);
104 $role->fill($roleData);
106 $this->permissionService->buildJointPermissionForRole($role);
110 * Assign an list of permission names to an role.
112 * @param array $permissionNameArray
114 public function assignRolePermissions(Role $role, $permissionNameArray = [])
117 $permissionNameArray = array_values($permissionNameArray);
118 if ($permissionNameArray && count($permissionNameArray) > 0) {
119 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
121 $role->permissions()->sync($permissions);
125 * Delete a role from the system.
126 * Check it's not an admin role or set as default before deleting.
127 * If an migration Role ID is specified the users assign to the current role
128 * will be added to the role of the specified id.
130 * @param $migrateRoleId
131 * @throws PermissionsException
133 public function deleteRole($roleId, $migrateRoleId)
135 $role = $this->role->findOrFail($roleId);
137 // Prevent deleting admin role or default registration role.
138 if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
139 throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
140 } else if ($role->id == setting('registration-role')) {
141 throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
144 if ($migrateRoleId) {
145 $newRole = $this->role->find($migrateRoleId);
147 $users = $role->users->pluck('id')->toArray();
148 $newRole->users()->sync($users);
152 $this->permissionService->deleteJointPermissionsForRole($role);