1 <?php namespace BookStack\Repos;
4 use BookStack\Exceptions\PermissionsException;
5 use BookStack\RolePermission;
7 use BookStack\Services\PermissionService;
13 protected $permission;
15 protected $permissionService;
17 protected $systemRoles = ['admin', 'public'];
20 * PermissionsRepo constructor.
21 * @param RolePermission $permission
23 * @param PermissionService $permissionService
25 public function __construct(RolePermission $permission, Role $role, PermissionService $permissionService)
27 $this->permission = $permission;
29 $this->permissionService = $permissionService;
33 * Get all the user roles from the system.
34 * @return \Illuminate\Database\Eloquent\Collection|static[]
36 public function getAllRoles()
38 return $this->role->all();
42 * Get all the roles except for the provided one.
46 public function getAllRolesExcept(Role $role)
48 return $this->role->where('id', '!=', $role->id)->get();
52 * Get a role via its ID.
56 public function getRoleById($id)
58 return $this->role->findOrFail($id);
62 * Save a new role into the system.
63 * @param array $roleData
66 public function saveNewRole($roleData)
68 $role = $this->role->newInstance($roleData);
69 $role->name = str_replace(' ', '-', strtolower($roleData['display_name']));
70 // Prevent duplicate names
71 while ($this->role->where('name', '=', $role->name)->count() > 0) {
72 $role->name .= strtolower(str_random(2));
76 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
77 $this->assignRolePermissions($role, $permissions);
78 $this->permissionService->buildJointPermissionForRole($role);
83 * Updates an existing role.
84 * Ensure Admin role always has all permissions.
87 * @throws PermissionsException
89 public function updateRole($roleId, $roleData)
91 $role = $this->role->findOrFail($roleId);
93 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
94 $this->assignRolePermissions($role, $permissions);
96 if ($role->name === 'admin') {
97 $permissions = $this->permission->all()->pluck('id')->toArray();
98 $role->permissions()->sync($permissions);
101 $role->fill($roleData);
103 $this->permissionService->buildJointPermissionForRole($role);
107 * Assign an list of permission names to an role.
109 * @param array $permissionNameArray
111 public function assignRolePermissions(Role $role, $permissionNameArray = [])
114 $permissionNameArray = array_values($permissionNameArray);
115 if ($permissionNameArray && count($permissionNameArray) > 0) {
116 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
118 $role->permissions()->sync($permissions);
122 * Delete a role from the system.
123 * Check it's not an admin role or set as default before deleting.
124 * If an migration Role ID is specified the users assign to the current role
125 * will be added to the role of the specified id.
127 * @param $migrateRoleId
128 * @throws PermissionsException
130 public function deleteRole($roleId, $migrateRoleId)
132 $role = $this->role->findOrFail($roleId);
134 // Prevent deleting admin role or default registration role.
135 if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
136 throw new PermissionsException('This role is a system role and cannot be deleted');
137 } else if ($role->id == setting('registration-role')) {
138 throw new PermissionsException('This role cannot be deleted while set as the default registration role.');
141 if ($migrateRoleId) {
142 $newRole = $this->role->find($migrateRoleId);
144 $users = $role->users->pluck('id')->toArray();
145 $newRole->users()->sync($users);
149 $this->permissionService->deleteJointPermissionsForRole($role);