]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/RoleController.php
fee31ffbfe297a8806a9b9d12a8c310c3325418a
[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
34         return view('settings.roles.index', ['roles' => $roles]);
35     }
36
37     /**
38      * Show the form to create a new role.
39      */
40     public function create(Request $request)
41     {
42         $this->checkPermission('user-roles-manage');
43
44         /** @var ?Role $role */
45         $role = null;
46         if ($request->has('copy_from')) {
47             $role = Role::query()->find($request->get('copy_from'));
48         }
49
50         if ($role) {
51             $role->display_name .= ' (' . trans('common.copy') . ')';
52         }
53
54         $this->setPageTitle(trans('settings.role_create'));
55
56         return view('settings.roles.create', ['role' => $role]);
57     }
58
59     /**
60      * Store a new role in the system.
61      */
62     public function store(Request $request)
63     {
64         $this->checkPermission('user-roles-manage');
65         $this->validate($request, [
66             'display_name' => ['required', 'min:3', 'max:180'],
67             'description'  => ['max:180'],
68         ]);
69
70         $this->permissionsRepo->saveNewRole($request->all());
71         $this->showSuccessNotification(trans('settings.role_create_success'));
72
73         return redirect('/settings/roles');
74     }
75
76     /**
77      * Show the form for editing a user role.
78      *
79      * @throws PermissionsException
80      */
81     public function edit(string $id)
82     {
83         $this->checkPermission('user-roles-manage');
84         $role = $this->permissionsRepo->getRoleById($id);
85         if ($role->hidden) {
86             throw new PermissionsException(trans('errors.role_cannot_be_edited'));
87         }
88
89         $this->setPageTitle(trans('settings.role_edit'));
90
91         return view('settings.roles.edit', ['role' => $role]);
92     }
93
94     /**
95      * Updates a user role.
96      *
97      * @throws ValidationException
98      */
99     public function update(Request $request, string $id)
100     {
101         $this->checkPermission('user-roles-manage');
102         $this->validate($request, [
103             'display_name' => ['required', 'min:3', 'max:180'],
104             'description'  => ['max:180'],
105         ]);
106
107         $this->permissionsRepo->updateRole($id, $request->all());
108         $this->showSuccessNotification(trans('settings.role_update_success'));
109
110         return redirect('/settings/roles');
111     }
112
113     /**
114      * Show the view to delete a role.
115      * Offers the chance to migrate users.
116      */
117     public function showDelete(string $id)
118     {
119         $this->checkPermission('user-roles-manage');
120         $role = $this->permissionsRepo->getRoleById($id);
121         $roles = $this->permissionsRepo->getAllRolesExcept($role);
122         $blankRole = $role->newInstance(['display_name' => trans('settings.role_delete_no_migration')]);
123         $roles->prepend($blankRole);
124
125         $this->setPageTitle(trans('settings.role_delete'));
126
127         return view('settings.roles.delete', ['role' => $role, 'roles' => $roles]);
128     }
129
130     /**
131      * Delete a role from the system,
132      * Migrate from a previous role if set.
133      *
134      * @throws Exception
135      */
136     public function delete(Request $request, string $id)
137     {
138         $this->checkPermission('user-roles-manage');
139
140         try {
141             $this->permissionsRepo->deleteRole($id, $request->get('migrate_role_id'));
142         } catch (PermissionsException $e) {
143             $this->showErrorNotification($e->getMessage());
144
145             return redirect()->back();
146         }
147
148         $this->showSuccessNotification(trans('settings.role_delete_success'));
149
150         return redirect('/settings/roles');
151     }
152 }