BookStack/app/Http/Controllers/RoleController.php

170 lines
5.0 KiB
PHP
Raw Normal View History

2021-06-26 11:23:15 -04:00
<?php
namespace BookStack\Http\Controllers;
use BookStack\Auth\Permissions\PermissionsRepo;
use BookStack\Auth\Queries\RolesAllPaginatedAndSorted;
use BookStack\Auth\Role;
use BookStack\Exceptions\PermissionsException;
use BookStack\Util\SimpleListOptions;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RoleController extends Controller
{
protected PermissionsRepo $permissionsRepo;
public function __construct(PermissionsRepo $permissionsRepo)
{
$this->permissionsRepo = $permissionsRepo;
}
/**
* Show a listing of the roles in the system.
*/
public function index(Request $request)
{
$this->checkPermission('user-roles-manage');
$listOptions = SimpleListOptions::fromRequest($request, 'roles')->withSortOptions([
'display_name' => trans('common.sort_name'),
'users_count' => trans('settings.roles_assigned_users'),
'permissions_count' => trans('settings.roles_permissions_provided'),
'created_at' => trans('common.sort_created_at'),
'updated_at' => trans('common.sort_updated_at'),
]);
$roles = (new RolesAllPaginatedAndSorted())->run(20, $listOptions);
$roles->appends($listOptions->getPaginationAppends());
2021-06-26 11:23:15 -04:00
$this->setPageTitle(trans('settings.roles'));
2022-01-06 07:18:11 -05:00
return view('settings.roles.index', [
'roles' => $roles,
'listOptions' => $listOptions,
]);
}
/**
2021-06-26 11:23:15 -04:00
* Show the form to create a new role.
*/
public function create(Request $request)
{
$this->checkPermission('user-roles-manage');
2021-06-26 11:23:15 -04:00
/** @var ?Role $role */
$role = null;
if ($request->has('copy_from')) {
$role = Role::query()->find($request->get('copy_from'));
}
if ($role) {
$role->display_name .= ' (' . trans('common.copy') . ')';
}
$this->setPageTitle(trans('settings.role_create'));
2022-01-06 07:18:11 -05:00
return view('settings.roles.create', ['role' => $role]);
}
/**
* Store a new role in the system.
*/
public function store(Request $request)
{
$this->checkPermission('user-roles-manage');
$data = $this->validate($request, [
'display_name' => ['required', 'min:3', 'max:180'],
'description' => ['max:180'],
'external_auth_id' => ['string'],
'permissions' => ['array'],
'mfa_enforced' => ['string'],
]);
$data['permissions'] = array_keys($data['permissions'] ?? []);
$data['mfa_enforced'] = ($data['mfa_enforced'] ?? 'false') === 'true';
$this->permissionsRepo->saveNewRole($data);
2021-06-26 11:23:15 -04:00
return redirect('/settings/roles');
}
/**
* Show the form for editing a user role.
*/
public function edit(string $id)
{
$this->checkPermission('user-roles-manage');
$role = $this->permissionsRepo->getRoleById($id);
2021-06-26 11:23:15 -04:00
$this->setPageTitle(trans('settings.role_edit'));
2022-01-06 07:18:11 -05:00
return view('settings.roles.edit', ['role' => $role]);
}
/**
* Updates a user role.
*/
public function update(Request $request, string $id)
{
$this->checkPermission('user-roles-manage');
$data = $this->validate($request, [
'display_name' => ['required', 'min:3', 'max:180'],
'description' => ['max:180'],
'external_auth_id' => ['string'],
'permissions' => ['array'],
'mfa_enforced' => ['string'],
]);
if (isset($data['permissions'])) {
$data['permissions'] = array_keys($data['permissions']);
}
if (isset($data['mfa_enforced'])) {
$data['mfa_enforced'] = $data['mfa_enforced'] === 'true';
}
$this->permissionsRepo->updateRole($id, $data);
2021-06-26 11:23:15 -04:00
return redirect('/settings/roles');
}
/**
* Show the view to delete a role.
* Offers the chance to migrate users.
*/
public function showDelete(string $id)
{
$this->checkPermission('user-roles-manage');
$role = $this->permissionsRepo->getRoleById($id);
$roles = $this->permissionsRepo->getAllRolesExcept($role);
2016-12-04 11:51:39 -05:00
$blankRole = $role->newInstance(['display_name' => trans('settings.role_delete_no_migration')]);
$roles->prepend($blankRole);
2021-06-26 11:23:15 -04:00
$this->setPageTitle(trans('settings.role_delete'));
2022-01-06 07:18:11 -05:00
return view('settings.roles.delete', ['role' => $role, 'roles' => $roles]);
}
/**
* Delete a role from the system,
* Migrate from a previous role if set.
2021-06-26 11:23:15 -04:00
*
* @throws Exception
*/
public function delete(Request $request, string $id)
{
$this->checkPermission('user-roles-manage');
try {
$this->permissionsRepo->deleteRole($id, $request->get('migrate_role_id', 0));
} catch (PermissionsException $e) {
$this->showErrorNotification($e->getMessage());
2021-06-26 11:23:15 -04:00
return redirect()->back();
}
return redirect('/settings/roles');
}
}