1 <?php namespace BookStack\Auth\Permissions;
3 use BookStack\Auth\Permissions;
4 use BookStack\Auth\Role;
5 use BookStack\Exceptions\PermissionsException;
10 protected $permission;
12 protected $permissionService;
14 protected $systemRoles = ['admin', 'public'];
17 * PermissionsRepo constructor.
18 * @param RolePermission $permission
20 * @param \BookStack\Auth\Permissions\PermissionService $permissionService
22 public function __construct(RolePermission $permission, Role $role, Permissions\PermissionService $permissionService)
24 $this->permission = $permission;
26 $this->permissionService = $permissionService;
30 * Get all the user roles from the system.
31 * @return \Illuminate\Database\Eloquent\Collection|static[]
33 public function getAllRoles()
35 return $this->role->all();
39 * Get all the roles except for the provided one.
43 public function getAllRolesExcept(Role $role)
45 return $this->role->where('id', '!=', $role->id)->get();
49 * Get a role via its ID.
53 public function getRoleById($id)
55 return $this->role->findOrFail($id);
59 * Save a new role into the system.
60 * @param array $roleData
63 public function saveNewRole($roleData)
65 $role = $this->role->newInstance($roleData);
66 $role->name = str_replace(' ', '-', strtolower($roleData['display_name']));
67 // Prevent duplicate names
68 while ($this->role->where('name', '=', $role->name)->count() > 0) {
69 $role->name .= strtolower(str_random(2));
73 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
74 $this->assignRolePermissions($role, $permissions);
75 $this->permissionService->buildJointPermissionForRole($role);
80 * Updates an existing role.
81 * Ensure Admin role always have core permissions.
84 * @throws PermissionsException
86 public function updateRole($roleId, $roleData)
88 $role = $this->role->findOrFail($roleId);
90 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
91 if ($role->system_name === 'admin') {
92 $permissions = array_merge($permissions, [
95 'restrictions-manage-all',
96 'restrictions-manage-own',
101 $this->assignRolePermissions($role, $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(trans('errors.role_system_cannot_be_deleted'));
139 } else if ($role->id == setting('registration-role')) {
140 throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
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);