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->where('hidden', '=', false)->get();
42 * Get all the roles except for the provided one.
46 public function getAllRolesExcept(Role $role)
48 return $this->role->where('id', '!=', $role->id)->where('hidden', '=', false)->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 if ($role->hidden) throw new PermissionsException("Cannot update a hidden role");
95 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
96 $this->assignRolePermissions($role, $permissions);
98 if ($role->name === 'admin') {
99 $permissions = $this->permission->all()->pluck('id')->toArray();
100 $role->permissions()->sync($permissions);
103 $role->fill($roleData);
105 $this->permissionService->buildJointPermissionForRole($role);
109 * Assign an list of permission names to an role.
111 * @param array $permissionNameArray
113 public function assignRolePermissions(Role $role, $permissionNameArray = [])
116 $permissionNameArray = array_values($permissionNameArray);
117 if ($permissionNameArray && count($permissionNameArray) > 0) {
118 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
120 $role->permissions()->sync($permissions);
124 * Delete a role from the system.
125 * Check it's not an admin role or set as default before deleting.
126 * If an migration Role ID is specified the users assign to the current role
127 * will be added to the role of the specified id.
129 * @param $migrateRoleId
130 * @throws PermissionsException
132 public function deleteRole($roleId, $migrateRoleId)
134 $role = $this->role->findOrFail($roleId);
136 // Prevent deleting admin role or default registration role.
137 if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
138 throw new PermissionsException('This role is a system role and cannot be deleted');
139 } else if ($role->id == setting('registration-role')) {
140 throw new PermissionsException('This role cannot be deleted while set as the default registration role.');
143 if ($migrateRoleId) {
144 $newRole = $this->role->find($migrateRoleId);
146 $users = $role->users->pluck('id')->toArray();
147 $newRole->users()->sync($users);
151 $this->permissionService->deleteJointPermissionsForRole($role);