2016-03-03 06:35:01 +08:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2016-02-27 07:44:02 +08:00
|
|
|
|
2018-09-25 19:30:50 +08:00
|
|
|
use BookStack\Auth\Permissions\PermissionsRepo;
|
2018-09-25 23:58:03 +08:00
|
|
|
use BookStack\Exceptions\PermissionsException;
|
2020-08-04 21:55:01 +08:00
|
|
|
use Exception;
|
2016-02-27 07:44:02 +08:00
|
|
|
use Illuminate\Http\Request;
|
2020-08-04 21:55:01 +08:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2016-02-27 07:44:02 +08:00
|
|
|
|
2020-11-21 02:53:01 +08:00
|
|
|
class RoleController extends Controller
|
2016-02-27 07:44:02 +08:00
|
|
|
{
|
|
|
|
|
2016-03-03 06:35:01 +08:00
|
|
|
protected $permissionsRepo;
|
2016-02-27 07:44:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PermissionController constructor.
|
|
|
|
*/
|
2016-03-03 06:35:01 +08:00
|
|
|
public function __construct(PermissionsRepo $permissionsRepo)
|
2016-02-27 07:44:02 +08:00
|
|
|
{
|
2016-03-03 06:35:01 +08:00
|
|
|
$this->permissionsRepo = $permissionsRepo;
|
2016-02-27 07:44:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a listing of the roles in the system.
|
|
|
|
*/
|
2020-11-21 02:53:01 +08:00
|
|
|
public function list()
|
2016-02-27 07:44:02 +08:00
|
|
|
{
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkPermission('user-roles-manage');
|
2016-03-03 06:35:01 +08:00
|
|
|
$roles = $this->permissionsRepo->getAllRoles();
|
2019-02-04 01:34:15 +08:00
|
|
|
return view('settings.roles.index', ['roles' => $roles]);
|
2016-02-27 07:44:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 03:24:42 +08:00
|
|
|
/**
|
|
|
|
* Show the form to create a new role
|
|
|
|
*/
|
2020-11-21 02:53:01 +08:00
|
|
|
public function create()
|
2016-02-28 03:24:42 +08:00
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
2019-02-04 01:34:15 +08:00
|
|
|
return view('settings.roles.create');
|
2016-02-28 03:24:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new role in the system.
|
|
|
|
*/
|
2020-11-21 02:53:01 +08:00
|
|
|
public function store(Request $request)
|
2016-02-28 03:24:42 +08:00
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
|
|
|
$this->validate($request, [
|
2020-08-04 21:55:01 +08:00
|
|
|
'display_name' => 'required|min:3|max:180',
|
|
|
|
'description' => 'max:180'
|
2016-02-28 03:24:42 +08:00
|
|
|
]);
|
|
|
|
|
2016-03-03 06:35:01 +08:00
|
|
|
$this->permissionsRepo->saveNewRole($request->all());
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->showSuccessNotification(trans('settings.role_create_success'));
|
2016-02-28 03:24:42 +08:00
|
|
|
return redirect('/settings/roles');
|
|
|
|
}
|
|
|
|
|
2016-02-27 07:44:02 +08:00
|
|
|
/**
|
|
|
|
* Show the form for editing a user role.
|
2016-05-02 02:36:53 +08:00
|
|
|
* @throws PermissionsException
|
2016-02-27 07:44:02 +08:00
|
|
|
*/
|
2020-11-21 02:53:01 +08:00
|
|
|
public function edit(string $id)
|
2016-02-27 07:44:02 +08:00
|
|
|
{
|
2016-02-28 03:24:42 +08:00
|
|
|
$this->checkPermission('user-roles-manage');
|
2016-03-03 06:35:01 +08:00
|
|
|
$role = $this->permissionsRepo->getRoleById($id);
|
2018-01-29 00:58:52 +08:00
|
|
|
if ($role->hidden) {
|
|
|
|
throw new PermissionsException(trans('errors.role_cannot_be_edited'));
|
|
|
|
}
|
2019-02-04 01:34:15 +08:00
|
|
|
return view('settings.roles.edit', ['role' => $role]);
|
2016-02-27 07:44:02 +08:00
|
|
|
}
|
2016-02-28 03:24:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a user role.
|
2020-08-04 21:55:01 +08:00
|
|
|
* @throws ValidationException
|
2016-02-28 03:24:42 +08:00
|
|
|
*/
|
2020-11-21 02:53:01 +08:00
|
|
|
public function update(Request $request, string $id)
|
2016-02-28 03:24:42 +08:00
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
|
|
|
$this->validate($request, [
|
2020-08-04 21:55:01 +08:00
|
|
|
'display_name' => 'required|min:3|max:180',
|
|
|
|
'description' => 'max:180'
|
2016-02-28 03:24:42 +08:00
|
|
|
]);
|
|
|
|
|
2016-03-03 06:35:01 +08:00
|
|
|
$this->permissionsRepo->updateRole($id, $request->all());
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->showSuccessNotification(trans('settings.role_update_success'));
|
2016-02-28 03:24:42 +08:00
|
|
|
return redirect('/settings/roles');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the view to delete a role.
|
|
|
|
* Offers the chance to migrate users.
|
|
|
|
*/
|
2020-11-21 02:53:01 +08:00
|
|
|
public function showDelete(string $id)
|
2016-02-28 03:24:42 +08:00
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
2016-03-03 06:35:01 +08:00
|
|
|
$role = $this->permissionsRepo->getRoleById($id);
|
|
|
|
$roles = $this->permissionsRepo->getAllRolesExcept($role);
|
2016-12-05 00:51:39 +08:00
|
|
|
$blankRole = $role->newInstance(['display_name' => trans('settings.role_delete_no_migration')]);
|
2016-02-28 03:24:42 +08:00
|
|
|
$roles->prepend($blankRole);
|
2019-02-04 01:34:15 +08:00
|
|
|
return view('settings.roles.delete', ['role' => $role, 'roles' => $roles]);
|
2016-02-28 03:24:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a role from the system,
|
|
|
|
* Migrate from a previous role if set.
|
2020-08-04 21:55:01 +08:00
|
|
|
* @throws Exception
|
2016-02-28 03:24:42 +08:00
|
|
|
*/
|
2020-11-21 02:53:01 +08:00
|
|
|
public function delete(Request $request, string $id)
|
2016-02-28 03:24:42 +08:00
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
|
|
|
|
2016-03-03 06:35:01 +08:00
|
|
|
try {
|
|
|
|
$this->permissionsRepo->deleteRole($id, $request->get('migrate_role_id'));
|
|
|
|
} catch (PermissionsException $e) {
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->showErrorNotification($e->getMessage());
|
2016-02-28 03:24:42 +08:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
$this->showSuccessNotification(trans('settings.role_delete_success'));
|
2016-02-28 03:24:42 +08:00
|
|
|
return redirect('/settings/roles');
|
|
|
|
}
|
2016-02-27 07:44:02 +08:00
|
|
|
}
|