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