2016-03-02 17:35:01 -05:00
|
|
|
<?php namespace BookStack\Repos;
|
|
|
|
|
|
|
|
|
|
|
|
use BookStack\Exceptions\PermissionsException;
|
2016-05-01 16:20:50 -04:00
|
|
|
use BookStack\RolePermission;
|
2016-03-02 17:35:01 -05:00
|
|
|
use BookStack\Role;
|
2016-05-01 16:20:50 -04:00
|
|
|
use BookStack\Services\PermissionService;
|
2016-03-02 17:35:01 -05:00
|
|
|
use Setting;
|
|
|
|
|
|
|
|
class PermissionsRepo
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $permission;
|
|
|
|
protected $role;
|
2016-05-01 16:20:50 -04:00
|
|
|
protected $permissionService;
|
2016-03-02 17:35:01 -05:00
|
|
|
|
2016-05-01 14:36:53 -04:00
|
|
|
protected $systemRoles = ['admin', 'public'];
|
|
|
|
|
2016-03-02 17:35:01 -05:00
|
|
|
/**
|
|
|
|
* PermissionsRepo constructor.
|
2016-05-01 16:20:50 -04:00
|
|
|
* @param RolePermission $permission
|
2016-04-24 11:54:20 -04:00
|
|
|
* @param Role $role
|
2016-05-01 16:20:50 -04:00
|
|
|
* @param PermissionService $permissionService
|
2016-03-02 17:35:01 -05:00
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
public function __construct(RolePermission $permission, Role $role, PermissionService $permissionService)
|
2016-03-02 17:35:01 -05:00
|
|
|
{
|
|
|
|
$this->permission = $permission;
|
|
|
|
$this->role = $role;
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->permissionService = $permissionService;
|
2016-03-02 17:35:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the user roles from the system.
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[]
|
|
|
|
*/
|
|
|
|
public function getAllRoles()
|
|
|
|
{
|
2016-09-29 07:43:46 -04:00
|
|
|
return $this->role->all();
|
2016-03-02 17:35:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the roles except for the provided one.
|
|
|
|
* @param Role $role
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getAllRolesExcept(Role $role)
|
|
|
|
{
|
2016-09-29 07:43:46 -04:00
|
|
|
return $this->role->where('id', '!=', $role->id)->get();
|
2016-03-02 17:35:01 -05: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) {
|
|
|
|
$role->name .= strtolower(str_random(2));
|
|
|
|
}
|
|
|
|
$role->save();
|
|
|
|
|
|
|
|
$permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
|
|
|
|
$this->assignRolePermissions($role, $permissions);
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->permissionService->buildJointPermissionForRole($role);
|
2016-03-02 17:35:01 -05:00
|
|
|
return $role;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an existing role.
|
|
|
|
* Ensure Admin role always has all permissions.
|
|
|
|
* @param $roleId
|
|
|
|
* @param $roleData
|
2016-05-01 14:36:53 -04:00
|
|
|
* @throws PermissionsException
|
2016-03-02 17:35:01 -05:00
|
|
|
*/
|
|
|
|
public function updateRole($roleId, $roleData)
|
|
|
|
{
|
|
|
|
$role = $this->role->findOrFail($roleId);
|
2016-05-01 14:36:53 -04:00
|
|
|
|
2016-03-02 17:35:01 -05:00
|
|
|
$permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
|
|
|
|
$this->assignRolePermissions($role, $permissions);
|
|
|
|
|
2017-01-22 07:16:02 -05:00
|
|
|
if ($role->system_name === 'admin') {
|
2016-03-02 17:35:01 -05:00
|
|
|
$permissions = $this->permission->all()->pluck('id')->toArray();
|
|
|
|
$role->permissions()->sync($permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
$role->fill($roleData);
|
|
|
|
$role->save();
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->permissionService->buildJointPermissionForRole($role);
|
2016-03-02 17:35:01 -05: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 07:09:09 -05:00
|
|
|
$permissionNameArray = array_values($permissionNameArray);
|
2016-03-02 17:35:01 -05: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-01 14:36:53 -04:00
|
|
|
if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
|
2016-12-04 11:51:39 -05:00
|
|
|
throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
|
2016-03-06 07:55:08 -05:00
|
|
|
} else if ($role->id == setting('registration-role')) {
|
2016-12-04 11:51:39 -05:00
|
|
|
throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
|
2016-03-02 17:35:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($migrateRoleId) {
|
|
|
|
$newRole = $this->role->find($migrateRoleId);
|
|
|
|
if ($newRole) {
|
|
|
|
$users = $role->users->pluck('id')->toArray();
|
|
|
|
$newRole->users()->sync($users);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->permissionService->deleteJointPermissionsForRole($role);
|
2016-03-02 17:35:01 -05:00
|
|
|
$role->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|