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