3 namespace BookStack\Users\Controllers;
5 use BookStack\Exceptions\PermissionsException;
6 use BookStack\Http\Controller;
7 use BookStack\Permissions\Permission;
8 use BookStack\Permissions\PermissionsRepo;
9 use BookStack\Users\Models\Role;
10 use BookStack\Users\Queries\RolesAllPaginatedAndSorted;
11 use BookStack\Util\SimpleListOptions;
13 use Illuminate\Http\Request;
15 class RoleController extends Controller
17 public function __construct(
18 protected PermissionsRepo $permissionsRepo
23 * Show a listing of the roles in the system.
25 public function index(Request $request)
27 $this->checkPermission(Permission::UserRolesManage);
29 $listOptions = SimpleListOptions::fromRequest($request, 'roles')->withSortOptions([
30 'display_name' => trans('common.sort_name'),
31 'users_count' => trans('settings.roles_assigned_users'),
32 'permissions_count' => trans('settings.roles_permissions_provided'),
33 'created_at' => trans('common.sort_created_at'),
34 'updated_at' => trans('common.sort_updated_at'),
37 $roles = (new RolesAllPaginatedAndSorted())->run(20, $listOptions);
38 $roles->appends($listOptions->getPaginationAppends());
40 $this->setPageTitle(trans('settings.roles'));
42 return view('settings.roles.index', [
44 'listOptions' => $listOptions,
49 * Show the form to create a new role.
51 public function create(Request $request)
53 $this->checkPermission(Permission::UserRolesManage);
55 /** @var ?Role $role */
57 if ($request->has('copy_from')) {
58 $role = Role::query()->find($request->get('copy_from'));
62 $role->display_name .= ' (' . trans('common.copy') . ')';
65 $this->setPageTitle(trans('settings.role_create'));
67 return view('settings.roles.create', ['role' => $role]);
71 * Store a new role in the system.
73 public function store(Request $request)
75 $this->checkPermission(Permission::UserRolesManage);
76 $data = $this->validate($request, [
77 'display_name' => ['required', 'min:3', 'max:180'],
78 'description' => ['max:180'],
79 'external_auth_id' => ['string', 'max:180'],
80 'permissions' => ['array'],
81 'mfa_enforced' => ['string'],
84 $data['permissions'] = array_keys($data['permissions'] ?? []);
85 $data['mfa_enforced'] = ($data['mfa_enforced'] ?? 'false') === 'true';
86 $this->permissionsRepo->saveNewRole($data);
88 return redirect('/settings/roles');
92 * Show the form for editing a user role.
94 public function edit(string $id)
96 $this->checkPermission(Permission::UserRolesManage);
97 $role = $this->permissionsRepo->getRoleById($id);
99 $this->setPageTitle(trans('settings.role_edit'));
101 return view('settings.roles.edit', ['role' => $role]);
105 * Updates a user role.
107 public function update(Request $request, string $id)
109 $this->checkPermission(Permission::UserRolesManage);
110 $data = $this->validate($request, [
111 'display_name' => ['required', 'min:3', 'max:180'],
112 'description' => ['max:180'],
113 'external_auth_id' => ['string', 'max:180'],
114 'permissions' => ['array'],
115 'mfa_enforced' => ['string'],
118 $data['permissions'] = array_keys($data['permissions'] ?? []);
119 $data['mfa_enforced'] = ($data['mfa_enforced'] ?? 'false') === 'true';
120 $this->permissionsRepo->updateRole($id, $data);
122 return redirect('/settings/roles');
126 * Show the view to delete a role.
127 * Offers the chance to migrate users.
129 public function showDelete(string $id)
131 $this->checkPermission(Permission::UserRolesManage);
132 $role = $this->permissionsRepo->getRoleById($id);
133 $roles = $this->permissionsRepo->getAllRolesExcept($role);
134 $blankRole = $role->newInstance(['display_name' => trans('settings.role_delete_no_migration')]);
135 $roles->prepend($blankRole);
137 $this->setPageTitle(trans('settings.role_delete'));
139 return view('settings.roles.delete', ['role' => $role, 'roles' => $roles]);
143 * Delete a role from the system,
144 * Migrate from a previous role if set.
148 public function delete(Request $request, string $id)
150 $this->checkPermission(Permission::UserRolesManage);
153 $migrateRoleId = intval($request->get('migrate_role_id') ?: "0");
154 $this->permissionsRepo->deleteRole($id, $migrateRoleId);
155 } catch (PermissionsException $e) {
156 $this->showErrorNotification($e->getMessage());
158 return redirect("/settings/roles/delete/{$id}");
161 return redirect('/settings/roles');