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 has all 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 $this->assignRolePermissions($role, $permissions);
95 if ($role->system_name === 'admin') {
96 $permissions = $this->permission->all()->pluck('id')->toArray();
97 $role->permissions()->sync($permissions);
100 $role->fill($roleData);
102 $this->permissionService->buildJointPermissionForRole($role);
106 * Assign an list of permission names to an role.
108 * @param array $permissionNameArray
110 public function assignRolePermissions(Role $role, $permissionNameArray = [])
113 $permissionNameArray = array_values($permissionNameArray);
114 if ($permissionNameArray && count($permissionNameArray) > 0) {
115 $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
117 $role->permissions()->sync($permissions);
121 * Delete a role from the system.
122 * Check it's not an admin role or set as default before deleting.
123 * If an migration Role ID is specified the users assign to the current role
124 * will be added to the role of the specified id.
126 * @param $migrateRoleId
127 * @throws PermissionsException
129 public function deleteRole($roleId, $migrateRoleId)
131 $role = $this->role->findOrFail($roleId);
133 // Prevent deleting admin role or default registration role.
134 if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
135 throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
136 } else if ($role->id == setting('registration-role')) {
137 throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
140 if ($migrateRoleId) {
141 $newRole = $this->role->find($migrateRoleId);
143 $users = $role->users->pluck('id')->toArray();
144 $newRole->users()->sync($users);
148 $this->permissionService->deleteJointPermissionsForRole($role);