BookStack/app/Http/Controllers/RoleController.php

134 lines
3.6 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\Exceptions\PermissionsException;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RoleController extends Controller
{
protected $permissionsRepo;
/**
* PermissionController constructor.
*/
public function __construct(PermissionsRepo $permissionsRepo)
{
$this->permissionsRepo = $permissionsRepo;
}
/**
* Show a listing of the roles in the system.
*/
public function list()
{
$this->checkPermission('user-roles-manage');
$roles = $this->permissionsRepo->getAllRoles();
2021-06-26 11:23:15 -04:00
return view('settings.roles.index', ['roles' => $roles]);
}
/**
2021-06-26 11:23:15 -04:00
* Show the form to create a new role.
*/
public function create()
{
$this->checkPermission('user-roles-manage');
2021-06-26 11:23:15 -04:00
return view('settings.roles.create');
}
/**
* Store a new role in the system.
*/
public function store(Request $request)
{
$this->checkPermission('user-roles-manage');
$this->validate($request, [
'display_name' => 'required|min:3|max:180',
2021-06-26 11:23:15 -04:00
'description' => 'max:180',
]);
$this->permissionsRepo->saveNewRole($request->all());
$this->showSuccessNotification(trans('settings.role_create_success'));
2021-06-26 11:23:15 -04:00
return redirect('/settings/roles');
}
/**
* Show the form for editing a user role.
2021-06-26 11:23:15 -04:00
*
* @throws PermissionsException
*/
public function edit(string $id)
{
$this->checkPermission('user-roles-manage');
$role = $this->permissionsRepo->getRoleById($id);
if ($role->hidden) {
throw new PermissionsException(trans('errors.role_cannot_be_edited'));
}
2021-06-26 11:23:15 -04:00
return view('settings.roles.edit', ['role' => $role]);
}
/**
* Updates a user role.
2021-06-26 11:23:15 -04:00
*
* @throws ValidationException
*/
public function update(Request $request, string $id)
{
$this->checkPermission('user-roles-manage');
$this->validate($request, [
'display_name' => 'required|min:3|max:180',
2021-06-26 11:23:15 -04:00
'description' => 'max:180',
]);
$this->permissionsRepo->updateRole($id, $request->all());
$this->showSuccessNotification(trans('settings.role_update_success'));
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
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'));
} catch (PermissionsException $e) {
$this->showErrorNotification($e->getMessage());
2021-06-26 11:23:15 -04:00
return redirect()->back();
}
$this->showSuccessNotification(trans('settings.role_delete_success'));
2021-06-26 11:23:15 -04:00
return redirect('/settings/roles');
}
}