1 <?php namespace BookStack\Auth\Permissions;
3 use BookStack\Auth\Permissions;
4 use BookStack\Exceptions\PermissionsException;
5 use BookStack\Auth\Permissions\RolePermission;
6 use BookStack\Auth\Role;
7 use BookStack\Auth\Permissions\PermissionService;
13 protected $permission;
15 protected $permissionService;
17 protected $systemRoles = ['admin', 'public'];
20 * PermissionsRepo constructor.
21 * @param RolePermission $permission
23 * @param \BookStack\Auth\Permissions\PermissionService $permissionService
25 public function __construct(RolePermission $permission, Role $role, Permissions\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 have core 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 if ($role->system_name === 'admin') {
95 $permissions = array_merge($permissions, [
98 'restrictions-manage-all',
99 'restrictions-manage-own',
104 $this->assignRolePermissions($role, $permissions);
106 $role->fill($roleData);
108 $this->permissionService->buildJointPermissionForRole($role);
112 * Assign an list of permission names to an role.
114 * @param array $permissionNameArray
116 public function assignRolePermissions(Role $role, $permissionNameArray = [])
119 $permissionNameArray = array_values($permissionNameArray);
120 if ($permissionNameArray && count($permissionNameArray) > 0) {
121 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
123 $role->permissions()->sync($permissions);
127 * Delete a role from the system.
128 * Check it's not an admin role or set as default before deleting.
129 * If an migration Role ID is specified the users assign to the current role
130 * will be added to the role of the specified id.
132 * @param $migrateRoleId
133 * @throws PermissionsException
135 public function deleteRole($roleId, $migrateRoleId)
137 $role = $this->role->findOrFail($roleId);
139 // Prevent deleting admin role or default registration role.
140 if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
141 throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
142 } else if ($role->id == setting('registration-role')) {
143 throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
146 if ($migrateRoleId) {
147 $newRole = $this->role->find($migrateRoleId);
149 $users = $role->users->pluck('id')->toArray();
150 $newRole->users()->sync($users);
154 $this->permissionService->deleteJointPermissionsForRole($role);