2016-03-02 17:35:01 -05:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2016-02-26 18:44:02 -05:00
|
|
|
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Auth\Permissions\PermissionsRepo;
|
2018-09-25 11:58:03 -04:00
|
|
|
use BookStack\Exceptions\PermissionsException;
|
2016-02-26 18:44:02 -05:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class PermissionController extends Controller
|
|
|
|
{
|
|
|
|
|
2016-03-02 17:35:01 -05:00
|
|
|
protected $permissionsRepo;
|
2016-02-26 18:44:02 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PermissionController constructor.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Auth\Permissions\PermissionsRepo $permissionsRepo
|
2016-02-26 18:44:02 -05:00
|
|
|
*/
|
2016-03-02 17:35:01 -05:00
|
|
|
public function __construct(PermissionsRepo $permissionsRepo)
|
2016-02-26 18:44:02 -05:00
|
|
|
{
|
2016-03-02 17:35:01 -05:00
|
|
|
$this->permissionsRepo = $permissionsRepo;
|
2016-02-26 18:44:02 -05:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a listing of the roles in the system.
|
|
|
|
*/
|
|
|
|
public function listRoles()
|
|
|
|
{
|
2016-02-27 14:24:42 -05:00
|
|
|
$this->checkPermission('user-roles-manage');
|
2016-03-02 17:35:01 -05:00
|
|
|
$roles = $this->permissionsRepo->getAllRoles();
|
2019-02-03 12:34:15 -05:00
|
|
|
return view('settings.roles.index', ['roles' => $roles]);
|
2016-02-26 18:44:02 -05:00
|
|
|
}
|
|
|
|
|
2016-02-27 14:24:42 -05:00
|
|
|
/**
|
|
|
|
* Show the form to create a new role
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function createRole()
|
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
2019-02-03 12:34:15 -05:00
|
|
|
return view('settings.roles.create');
|
2016-02-27 14:24:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new role in the system.
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
|
|
|
public function storeRole(Request $request)
|
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
|
|
|
$this->validate($request, [
|
|
|
|
'display_name' => 'required|min:3|max:200',
|
|
|
|
'description' => 'max:250'
|
|
|
|
]);
|
|
|
|
|
2016-03-02 17:35:01 -05:00
|
|
|
$this->permissionsRepo->saveNewRole($request->all());
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->showSuccessNotification(trans('settings.role_create_success'));
|
2016-02-27 14:24:42 -05:00
|
|
|
return redirect('/settings/roles');
|
|
|
|
}
|
|
|
|
|
2016-02-26 18:44:02 -05:00
|
|
|
/**
|
|
|
|
* Show the form for editing a user role.
|
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2016-05-01 14:36:53 -04:00
|
|
|
* @throws PermissionsException
|
2016-02-26 18:44:02 -05:00
|
|
|
*/
|
|
|
|
public function editRole($id)
|
|
|
|
{
|
2016-02-27 14:24:42 -05:00
|
|
|
$this->checkPermission('user-roles-manage');
|
2016-03-02 17:35:01 -05:00
|
|
|
$role = $this->permissionsRepo->getRoleById($id);
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($role->hidden) {
|
|
|
|
throw new PermissionsException(trans('errors.role_cannot_be_edited'));
|
|
|
|
}
|
2019-02-03 12:34:15 -05:00
|
|
|
return view('settings.roles.edit', ['role' => $role]);
|
2016-02-26 18:44:02 -05:00
|
|
|
}
|
2016-02-27 14:24:42 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a user role.
|
|
|
|
* @param Request $request
|
2019-09-15 13:53:30 -04:00
|
|
|
* @param $id
|
2016-02-27 14:24:42 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2018-07-15 14:34:42 -04:00
|
|
|
* @throws PermissionsException
|
2019-09-15 13:53:30 -04:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2016-02-27 14:24:42 -05:00
|
|
|
*/
|
2019-09-15 13:53:30 -04:00
|
|
|
public function updateRole(Request $request, $id)
|
2016-02-27 14:24:42 -05:00
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
|
|
|
$this->validate($request, [
|
|
|
|
'display_name' => 'required|min:3|max:200',
|
|
|
|
'description' => 'max:250'
|
|
|
|
]);
|
|
|
|
|
2016-03-02 17:35:01 -05:00
|
|
|
$this->permissionsRepo->updateRole($id, $request->all());
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->showSuccessNotification(trans('settings.role_update_success'));
|
2016-02-27 14:24:42 -05:00
|
|
|
return redirect('/settings/roles');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the view to delete a role.
|
|
|
|
* Offers the chance to migrate users.
|
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function showDeleteRole($id)
|
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
2016-03-02 17:35:01 -05:00
|
|
|
$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')]);
|
2016-02-27 14:24:42 -05:00
|
|
|
$roles->prepend($blankRole);
|
2019-02-03 12:34:15 -05:00
|
|
|
return view('settings.roles.delete', ['role' => $role, 'roles' => $roles]);
|
2016-02-27 14:24:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a role from the system,
|
|
|
|
* Migrate from a previous role if set.
|
|
|
|
* @param Request $request
|
2019-09-15 13:53:30 -04:00
|
|
|
* @param $id
|
2016-02-27 14:24:42 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
2019-09-15 13:53:30 -04:00
|
|
|
public function deleteRole(Request $request, $id)
|
2016-02-27 14:24:42 -05:00
|
|
|
{
|
|
|
|
$this->checkPermission('user-roles-manage');
|
|
|
|
|
2016-03-02 17:35:01 -05:00
|
|
|
try {
|
|
|
|
$this->permissionsRepo->deleteRole($id, $request->get('migrate_role_id'));
|
|
|
|
} catch (PermissionsException $e) {
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->showErrorNotification($e->getMessage());
|
2016-02-27 14:24:42 -05:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->showSuccessNotification(trans('settings.role_delete_success'));
|
2016-02-27 14:24:42 -05:00
|
|
|
return redirect('/settings/roles');
|
|
|
|
}
|
2016-02-26 18:44:02 -05:00
|
|
|
}
|