]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/RoleController.php
Merge branch 'master' of git://github.com/rondaa/BookStack into rondaa-master
[bookstack] / app / Http / Controllers / RoleController.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 RoleController 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     }
21
22     /**
23      * Show a listing of the roles in the system.
24      */
25     public function list()
26     {
27         $this->checkPermission('user-roles-manage');
28         $roles = $this->permissionsRepo->getAllRoles();
29         return view('settings.roles.index', ['roles' => $roles]);
30     }
31
32     /**
33      * Show the form to create a new role
34      */
35     public function create()
36     {
37         $this->checkPermission('user-roles-manage');
38         return view('settings.roles.create');
39     }
40
41     /**
42      * Store a new role in the system.
43      */
44     public function store(Request $request)
45     {
46         $this->checkPermission('user-roles-manage');
47         $this->validate($request, [
48             'display_name' => 'required|min:3|max:180',
49             'description' => 'max:180'
50         ]);
51
52         $this->permissionsRepo->saveNewRole($request->all());
53         $this->showSuccessNotification(trans('settings.role_create_success'));
54         return redirect('/settings/roles');
55     }
56
57     /**
58      * Show the form for editing a user role.
59      * @throws PermissionsException
60      */
61     public function edit(string $id)
62     {
63         $this->checkPermission('user-roles-manage');
64         $role = $this->permissionsRepo->getRoleById($id);
65         if ($role->hidden) {
66             throw new PermissionsException(trans('errors.role_cannot_be_edited'));
67         }
68         return view('settings.roles.edit', ['role' => $role]);
69     }
70
71     /**
72      * Updates a user role.
73      * @throws ValidationException
74      */
75     public function update(Request $request, string $id)
76     {
77         $this->checkPermission('user-roles-manage');
78         $this->validate($request, [
79             'display_name' => 'required|min:3|max:180',
80             'description' => 'max:180'
81         ]);
82
83         $this->permissionsRepo->updateRole($id, $request->all());
84         $this->showSuccessNotification(trans('settings.role_update_success'));
85         return redirect('/settings/roles');
86     }
87
88     /**
89      * Show the view to delete a role.
90      * Offers the chance to migrate users.
91      */
92     public function showDelete(string $id)
93     {
94         $this->checkPermission('user-roles-manage');
95         $role = $this->permissionsRepo->getRoleById($id);
96         $roles = $this->permissionsRepo->getAllRolesExcept($role);
97         $blankRole = $role->newInstance(['display_name' => trans('settings.role_delete_no_migration')]);
98         $roles->prepend($blankRole);
99         return view('settings.roles.delete', ['role' => $role, 'roles' => $roles]);
100     }
101
102     /**
103      * Delete a role from the system,
104      * Migrate from a previous role if set.
105      * @throws Exception
106      */
107     public function delete(Request $request, string $id)
108     {
109         $this->checkPermission('user-roles-manage');
110
111         try {
112             $this->permissionsRepo->deleteRole($id, $request->get('migrate_role_id'));
113         } catch (PermissionsException $e) {
114             $this->showErrorNotification($e->getMessage());
115             return redirect()->back();
116         }
117
118         $this->showSuccessNotification(trans('settings.role_delete_success'));
119         return redirect('/settings/roles');
120     }
121 }