1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Auth\Permissions\PermissionsRepo;
4 use BookStack\Exceptions\PermissionsException;
6 use Illuminate\Http\Request;
7 use Illuminate\Validation\ValidationException;
9 class RoleController extends Controller
12 protected $permissionsRepo;
15 * PermissionController constructor.
17 public function __construct(PermissionsRepo $permissionsRepo)
19 $this->permissionsRepo = $permissionsRepo;
20 parent::__construct();
24 * Show a listing of the roles in the system.
26 public function list()
28 $this->checkPermission('user-roles-manage');
29 $roles = $this->permissionsRepo->getAllRoles();
30 return view('settings.roles.index', ['roles' => $roles]);
34 * Show the form to create a new role
36 public function create()
38 $this->checkPermission('user-roles-manage');
39 return view('settings.roles.create');
43 * Store a new role in the system.
45 public function store(Request $request)
47 $this->checkPermission('user-roles-manage');
48 $this->validate($request, [
49 'display_name' => 'required|min:3|max:180',
50 'description' => 'max:180'
53 $this->permissionsRepo->saveNewRole($request->all());
54 $this->showSuccessNotification(trans('settings.role_create_success'));
55 return redirect('/settings/roles');
59 * Show the form for editing a user role.
60 * @throws PermissionsException
62 public function edit(string $id)
64 $this->checkPermission('user-roles-manage');
65 $role = $this->permissionsRepo->getRoleById($id);
67 throw new PermissionsException(trans('errors.role_cannot_be_edited'));
69 return view('settings.roles.edit', ['role' => $role]);
73 * Updates a user role.
74 * @throws ValidationException
76 public function update(Request $request, string $id)
78 $this->checkPermission('user-roles-manage');
79 $this->validate($request, [
80 'display_name' => 'required|min:3|max:180',
81 'description' => 'max:180'
84 $this->permissionsRepo->updateRole($id, $request->all());
85 $this->showSuccessNotification(trans('settings.role_update_success'));
86 return redirect('/settings/roles');
90 * Show the view to delete a role.
91 * Offers the chance to migrate users.
93 public function showDelete(string $id)
95 $this->checkPermission('user-roles-manage');
96 $role = $this->permissionsRepo->getRoleById($id);
97 $roles = $this->permissionsRepo->getAllRolesExcept($role);
98 $blankRole = $role->newInstance(['display_name' => trans('settings.role_delete_no_migration')]);
99 $roles->prepend($blankRole);
100 return view('settings.roles.delete', ['role' => $role, 'roles' => $roles]);
104 * Delete a role from the system,
105 * Migrate from a previous role if set.
108 public function delete(Request $request, string $id)
110 $this->checkPermission('user-roles-manage');
113 $this->permissionsRepo->deleteRole($id, $request->get('migrate_role_id'));
114 } catch (PermissionsException $e) {
115 $this->showErrorNotification($e->getMessage());
116 return redirect()->back();
119 $this->showSuccessNotification(trans('settings.role_delete_success'));
120 return redirect('/settings/roles');