1 <?php namespace BookStack\Repos;
4 use BookStack\Exceptions\PermissionsException;
5 use BookStack\Permission;
7 use BookStack\Services\RestrictionService;
13 protected $permission;
15 protected $restrictionService;
18 * PermissionsRepo constructor.
19 * @param Permission $permission
21 * @param RestrictionService $restrictionService
23 public function __construct(Permission $permission, Role $role, RestrictionService $restrictionService)
25 $this->permission = $permission;
27 $this->restrictionService = $restrictionService;
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->restrictionService->buildEntityPermissionForRole($role);
81 * Updates an existing role.
82 * Ensure Admin role always has all permissions.
86 public function updateRole($roleId, $roleData)
88 $role = $this->role->findOrFail($roleId);
89 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
90 $this->assignRolePermissions($role, $permissions);
92 if ($role->name === 'admin') {
93 $permissions = $this->permission->all()->pluck('id')->toArray();
94 $role->permissions()->sync($permissions);
97 $role->fill($roleData);
99 $this->restrictionService->buildEntityPermissionForRole($role);
103 * Assign an list of permission names to an role.
105 * @param array $permissionNameArray
107 public function assignRolePermissions(Role $role, $permissionNameArray = [])
110 $permissionNameArray = array_values($permissionNameArray);
111 if ($permissionNameArray && count($permissionNameArray) > 0) {
112 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
114 $role->permissions()->sync($permissions);
118 * Delete a role from the system.
119 * Check it's not an admin role or set as default before deleting.
120 * If an migration Role ID is specified the users assign to the current role
121 * will be added to the role of the specified id.
123 * @param $migrateRoleId
124 * @throws PermissionsException
126 public function deleteRole($roleId, $migrateRoleId)
128 $role = $this->role->findOrFail($roleId);
130 // Prevent deleting admin role or default registration role.
131 if ($role->name === 'admin') {
132 throw new PermissionsException('The admin role cannot be deleted');
133 } else if ($role->id == setting('registration-role')) {
134 throw new PermissionsException('This role cannot be deleted while set as the default registration role.');
137 if ($migrateRoleId) {
138 $newRole = $this->role->find($migrateRoleId);
140 $users = $role->users->pluck('id')->toArray();
141 $newRole->users()->sync($users);
145 $this->restrictionService->deleteEntityPermissionsForRole($role);