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