2018-09-25 19:30:50 +08:00
|
|
|
<?php namespace BookStack\Auth\Permissions;
|
2016-03-03 06:35:01 +08:00
|
|
|
|
2018-09-25 19:30:50 +08:00
|
|
|
use BookStack\Auth\Permissions;
|
|
|
|
use BookStack\Auth\Role;
|
2018-09-25 23:58:03 +08:00
|
|
|
use BookStack\Exceptions\PermissionsException;
|
2019-09-14 06:58:40 +08:00
|
|
|
use Illuminate\Support\Str;
|
2016-03-03 06:35:01 +08:00
|
|
|
|
|
|
|
class PermissionsRepo
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $permission;
|
|
|
|
protected $role;
|
2016-05-02 04:20:50 +08:00
|
|
|
protected $permissionService;
|
2016-03-03 06:35:01 +08:00
|
|
|
|
2016-05-02 02:36:53 +08:00
|
|
|
protected $systemRoles = ['admin', 'public'];
|
|
|
|
|
2016-03-03 06:35:01 +08:00
|
|
|
/**
|
|
|
|
* PermissionsRepo constructor.
|
2016-05-02 04:20:50 +08:00
|
|
|
* @param RolePermission $permission
|
2016-04-24 23:54:20 +08:00
|
|
|
* @param Role $role
|
2018-09-25 19:30:50 +08:00
|
|
|
* @param \BookStack\Auth\Permissions\PermissionService $permissionService
|
2016-03-03 06:35:01 +08:00
|
|
|
*/
|
2018-09-25 19:30:50 +08:00
|
|
|
public function __construct(RolePermission $permission, Role $role, Permissions\PermissionService $permissionService)
|
2016-03-03 06:35:01 +08:00
|
|
|
{
|
|
|
|
$this->permission = $permission;
|
|
|
|
$this->role = $role;
|
2016-05-02 04:20:50 +08:00
|
|
|
$this->permissionService = $permissionService;
|
2016-03-03 06:35:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the user roles from the system.
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[]
|
|
|
|
*/
|
|
|
|
public function getAllRoles()
|
|
|
|
{
|
2016-09-29 19:43:46 +08:00
|
|
|
return $this->role->all();
|
2016-03-03 06:35:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the roles except for the provided one.
|
|
|
|
* @param Role $role
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getAllRolesExcept(Role $role)
|
|
|
|
{
|
2016-09-29 19:43:46 +08:00
|
|
|
return $this->role->where('id', '!=', $role->id)->get();
|
2016-03-03 06:35:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a role via its ID.
|
|
|
|
* @param $id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getRoleById($id)
|
|
|
|
{
|
|
|
|
return $this->role->findOrFail($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a new role into the system.
|
|
|
|
* @param array $roleData
|
|
|
|
* @return Role
|
|
|
|
*/
|
|
|
|
public function saveNewRole($roleData)
|
|
|
|
{
|
|
|
|
$role = $this->role->newInstance($roleData);
|
|
|
|
$role->name = str_replace(' ', '-', strtolower($roleData['display_name']));
|
|
|
|
// Prevent duplicate names
|
|
|
|
while ($this->role->where('name', '=', $role->name)->count() > 0) {
|
2019-09-14 06:58:40 +08:00
|
|
|
$role->name .= strtolower(Str::random(2));
|
2016-03-03 06:35:01 +08:00
|
|
|
}
|
|
|
|
$role->save();
|
|
|
|
|
|
|
|
$permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
|
|
|
|
$this->assignRolePermissions($role, $permissions);
|
2016-05-02 04:20:50 +08:00
|
|
|
$this->permissionService->buildJointPermissionForRole($role);
|
2016-03-03 06:35:01 +08:00
|
|
|
return $role;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an existing role.
|
2018-09-21 02:48:08 +08:00
|
|
|
* Ensure Admin role always have core permissions.
|
2016-03-03 06:35:01 +08:00
|
|
|
* @param $roleId
|
|
|
|
* @param $roleData
|
2016-05-02 02:36:53 +08:00
|
|
|
* @throws PermissionsException
|
2016-03-03 06:35:01 +08:00
|
|
|
*/
|
|
|
|
public function updateRole($roleId, $roleData)
|
|
|
|
{
|
|
|
|
$role = $this->role->findOrFail($roleId);
|
2016-05-02 02:36:53 +08:00
|
|
|
|
2016-03-03 06:35:01 +08:00
|
|
|
$permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
|
2017-01-22 20:16:02 +08:00
|
|
|
if ($role->system_name === 'admin') {
|
2018-09-21 02:48:08 +08:00
|
|
|
$permissions = array_merge($permissions, [
|
|
|
|
'users-manage',
|
|
|
|
'user-roles-manage',
|
|
|
|
'restrictions-manage-all',
|
|
|
|
'restrictions-manage-own',
|
|
|
|
'settings-manage',
|
|
|
|
]);
|
2016-03-03 06:35:01 +08:00
|
|
|
}
|
|
|
|
|
2018-09-21 02:48:08 +08:00
|
|
|
$this->assignRolePermissions($role, $permissions);
|
|
|
|
|
2016-03-03 06:35:01 +08:00
|
|
|
$role->fill($roleData);
|
|
|
|
$role->save();
|
2016-05-02 04:20:50 +08:00
|
|
|
$this->permissionService->buildJointPermissionForRole($role);
|
2016-03-03 06:35:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign an list of permission names to an role.
|
|
|
|
* @param Role $role
|
|
|
|
* @param array $permissionNameArray
|
|
|
|
*/
|
|
|
|
public function assignRolePermissions(Role $role, $permissionNameArray = [])
|
|
|
|
{
|
|
|
|
$permissions = [];
|
2016-03-05 20:09:09 +08:00
|
|
|
$permissionNameArray = array_values($permissionNameArray);
|
2016-03-03 06:35:01 +08:00
|
|
|
if ($permissionNameArray && count($permissionNameArray) > 0) {
|
|
|
|
$permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
|
|
|
|
}
|
|
|
|
$role->permissions()->sync($permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a role from the system.
|
|
|
|
* Check it's not an admin role or set as default before deleting.
|
|
|
|
* If an migration Role ID is specified the users assign to the current role
|
|
|
|
* will be added to the role of the specified id.
|
|
|
|
* @param $roleId
|
|
|
|
* @param $migrateRoleId
|
|
|
|
* @throws PermissionsException
|
|
|
|
*/
|
|
|
|
public function deleteRole($roleId, $migrateRoleId)
|
|
|
|
{
|
|
|
|
$role = $this->role->findOrFail($roleId);
|
|
|
|
|
|
|
|
// Prevent deleting admin role or default registration role.
|
2016-05-02 02:36:53 +08:00
|
|
|
if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
|
2016-12-05 00:51:39 +08:00
|
|
|
throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
|
2019-09-16 06:28:23 +08:00
|
|
|
} else if ($role->id === intval(setting('registration-role'))) {
|
2016-12-05 00:51:39 +08:00
|
|
|
throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
|
2016-03-03 06:35:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($migrateRoleId) {
|
|
|
|
$newRole = $this->role->find($migrateRoleId);
|
|
|
|
if ($newRole) {
|
|
|
|
$users = $role->users->pluck('id')->toArray();
|
|
|
|
$newRole->users()->sync($users);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 04:20:50 +08:00
|
|
|
$this->permissionService->deleteJointPermissionsForRole($role);
|
2016-03-03 06:35:01 +08:00
|
|
|
$role->delete();
|
|
|
|
}
|
2018-01-29 00:58:52 +08:00
|
|
|
}
|