1 <?php namespace BookStack\Repos;
3 use BookStack\Exceptions\PermissionsException;
4 use BookStack\RolePermission;
6 use BookStack\Services\PermissionService;
12 protected $permission;
14 protected $permissionService;
16 protected $systemRoles = ['admin', 'public'];
19 * PermissionsRepo constructor.
20 * @param RolePermission $permission
22 * @param PermissionService $permissionService
24 public function __construct(RolePermission $permission, Role $role, PermissionService $permissionService)
26 $this->permission = $permission;
28 $this->permissionService = $permissionService;
32 * Get all the user roles from the system.
33 * @return \Illuminate\Database\Eloquent\Collection|static[]
35 public function getAllRoles()
37 return $this->role->all();
41 * Get all the roles except for the provided one.
45 public function getAllRolesExcept(Role $role)
47 return $this->role->where('id', '!=', $role->id)->get();
51 * Get a role via its ID.
55 public function getRoleById($id)
57 return $this->role->findOrFail($id);
61 * Save a new role into the system.
62 * @param array $roleData
65 public function saveNewRole($roleData)
67 $role = $this->role->newInstance($roleData);
68 $role->name = str_replace(' ', '-', strtolower($roleData['display_name']));
69 // Prevent duplicate names
70 while ($this->role->where('name', '=', $role->name)->count() > 0) {
71 $role->name .= strtolower(str_random(2));
75 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
76 $this->assignRolePermissions($role, $permissions);
77 $this->permissionService->buildJointPermissionForRole($role);
82 * Updates an existing role.
83 * Ensure Admin role always have core permissions.
86 * @throws PermissionsException
88 public function updateRole($roleId, $roleData)
90 $role = $this->role->findOrFail($roleId);
92 $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
93 if ($role->system_name === 'admin') {
94 $permissions = array_merge($permissions, [
97 'restrictions-manage-all',
98 'restrictions-manage-own',
103 $this->assignRolePermissions($role, $permissions);
105 $role->fill($roleData);
107 $this->permissionService->buildJointPermissionForRole($role);
111 * Assign an list of permission names to an role.
113 * @param array $permissionNameArray
115 public function assignRolePermissions(Role $role, $permissionNameArray = [])
118 $permissionNameArray = array_values($permissionNameArray);
119 if ($permissionNameArray && count($permissionNameArray) > 0) {
120 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
122 $role->permissions()->sync($permissions);
126 * Delete a role from the system.
127 * Check it's not an admin role or set as default before deleting.
128 * If an migration Role ID is specified the users assign to the current role
129 * will be added to the role of the specified id.
131 * @param $migrateRoleId
132 * @throws PermissionsException
134 public function deleteRole($roleId, $migrateRoleId)
136 $role = $this->role->findOrFail($roleId);
138 // Prevent deleting admin role or default registration role.
139 if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
140 throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
141 } else if ($role->id == setting('registration-role')) {
142 throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
145 if ($migrateRoleId) {
146 $newRole = $this->role->find($migrateRoleId);
148 $users = $role->users->pluck('id')->toArray();
149 $newRole->users()->sync($users);
153 $this->permissionService->deleteJointPermissionsForRole($role);