]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/RoleController.php
Added page titles to many missing app areas
[bookstack] / app / Http / Controllers / RoleController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Auth\Permissions\PermissionsRepo;
6 use BookStack\Auth\Role;
7 use BookStack\Exceptions\PermissionsException;
8 use Exception;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
11
12 class RoleController extends Controller
13 {
14     protected $permissionsRepo;
15
16     /**
17      * PermissionController constructor.
18      */
19     public function __construct(PermissionsRepo $permissionsRepo)
20     {
21         $this->permissionsRepo = $permissionsRepo;
22     }
23
24     /**
25      * Show a listing of the roles in the system.
26      */
27     public function index()
28     {
29         $this->checkPermission('user-roles-manage');
30         $roles = $this->permissionsRepo->getAllRoles();
31
32         $this->setPageTitle(trans('settings.roles'));
33         return view('settings.roles.index', ['roles' => $roles]);
34     }
35
36     /**
37      * Show the form to create a new role.
38      */
39     public function create(Request $request)
40     {
41         $this->checkPermission('user-roles-manage');
42
43         /** @var ?Role $role */
44         $role = null;
45         if ($request->has('copy_from')) {
46             $role = Role::query()->find($request->get('copy_from'));
47         }
48
49         if ($role) {
50             $role->display_name .= ' (' . trans('common.copy') . ')';
51         }
52
53         $this->setPageTitle(trans('settings.role_create'));
54         return view('settings.roles.create', ['role' => $role]);
55     }
56
57     /**
58      * Store a new role in the system.
59      */
60     public function store(Request $request)
61     {
62         $this->checkPermission('user-roles-manage');
63         $this->validate($request, [
64             'display_name' => ['required', 'min:3', 'max:180'],
65             'description'  => ['max:180'],
66         ]);
67
68         $this->permissionsRepo->saveNewRole($request->all());
69         $this->showSuccessNotification(trans('settings.role_create_success'));
70
71         return redirect('/settings/roles');
72     }
73
74     /**
75      * Show the form for editing a user role.
76      *
77      * @throws PermissionsException
78      */
79     public function edit(string $id)
80     {
81         $this->checkPermission('user-roles-manage');
82         $role = $this->permissionsRepo->getRoleById($id);
83         if ($role->hidden) {
84             throw new PermissionsException(trans('errors.role_cannot_be_edited'));
85         }
86
87         $this->setPageTitle(trans('settings.role_edit'));
88         return view('settings.roles.edit', ['role' => $role]);
89     }
90
91     /**
92      * Updates a user role.
93      *
94      * @throws ValidationException
95      */
96     public function update(Request $request, string $id)
97     {
98         $this->checkPermission('user-roles-manage');
99         $this->validate($request, [
100             'display_name' => ['required', 'min:3', 'max:180'],
101             'description'  => ['max:180'],
102         ]);
103
104         $this->permissionsRepo->updateRole($id, $request->all());
105         $this->showSuccessNotification(trans('settings.role_update_success'));
106
107         return redirect('/settings/roles');
108     }
109
110     /**
111      * Show the view to delete a role.
112      * Offers the chance to migrate users.
113      */
114     public function showDelete(string $id)
115     {
116         $this->checkPermission('user-roles-manage');
117         $role = $this->permissionsRepo->getRoleById($id);
118         $roles = $this->permissionsRepo->getAllRolesExcept($role);
119         $blankRole = $role->newInstance(['display_name' => trans('settings.role_delete_no_migration')]);
120         $roles->prepend($blankRole);
121
122         $this->setPageTitle(trans('settings.role_delete'));
123         return view('settings.roles.delete', ['role' => $role, 'roles' => $roles]);
124     }
125
126     /**
127      * Delete a role from the system,
128      * Migrate from a previous role if set.
129      *
130      * @throws Exception
131      */
132     public function delete(Request $request, string $id)
133     {
134         $this->checkPermission('user-roles-manage');
135
136         try {
137             $this->permissionsRepo->deleteRole($id, $request->get('migrate_role_id'));
138         } catch (PermissionsException $e) {
139             $this->showErrorNotification($e->getMessage());
140
141             return redirect()->back();
142         }
143
144         $this->showSuccessNotification(trans('settings.role_delete_success'));
145
146         return redirect('/settings/roles');
147     }
148 }