1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\Events\Admin\UserRoleCreatedEvent; |
6
|
|
|
use App\Events\Admin\UserRoleDeletedEvent; |
7
|
|
|
use App\Events\Admin\UserRoleUpdatedEvent; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use App\Http\Requests\User\UserRoleRequest; |
10
|
|
|
use App\Models\User; |
11
|
|
|
use App\Models\UserRolePermissions; |
12
|
|
|
use App\Models\UserRolePermissionTypes; |
13
|
|
|
use App\Models\UserRoles; |
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
use Illuminate\Support\Facades\Auth; |
16
|
|
|
use Illuminate\Support\Facades\Log; |
17
|
|
|
use Inertia\Inertia; |
18
|
|
|
|
19
|
|
|
class UserRolesController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Show the existing User Roles |
23
|
|
|
*/ |
24
|
|
|
public function index() |
25
|
|
|
{ |
26
|
|
|
$this->authorize('viewAny', UserRoles::class); |
27
|
|
|
|
28
|
|
|
return Inertia::render('Admin/Roles/Index', [ |
29
|
|
|
'roles' => UserRoles::all()->makeVisible('allow_edit'), |
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Show the form to create a new Role |
35
|
|
|
*/ |
36
|
|
|
public function create() |
37
|
|
|
{ |
38
|
|
|
$this->authorize('create', UserRoles::class); |
39
|
|
|
|
40
|
|
|
return Inertia::render('Admin/Roles/Create', [ |
41
|
|
|
'permissions' => UserRolePermissionTypes::all(), |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create the new User Role |
47
|
|
|
*/ |
48
|
|
|
public function store(UserRoleRequest $request) |
49
|
|
|
{ |
50
|
|
|
// Create the new role |
51
|
|
|
$newRole = UserRoles::create($request->only(['name', 'description'])); |
52
|
|
|
|
53
|
|
|
// Insert the permissions for the role |
54
|
|
|
foreach($request->user_role_permissions as $perm) |
55
|
|
|
{ |
56
|
|
|
UserRolePermissions::create([ |
57
|
|
|
'role_id' => $newRole->role_id, |
58
|
|
|
'perm_type_id' => $perm['perm_type_id'], |
59
|
|
|
'allow' => isset($perm['allow']) ? $perm['allow'] : false, |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
event(new UserRoleCreatedEvent($newRole)); |
64
|
|
|
return redirect(route('admin.user-roles.index'))->with([ |
65
|
|
|
'message' => 'New role created', |
66
|
|
|
'type' => 'success', |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Show the form for editing the User Role |
72
|
|
|
*/ |
73
|
|
|
public function edit($id) |
74
|
|
|
{ |
75
|
|
|
$role = UserRoles::with('UserRolePermissions.UserRolePermissionTypes')->where('role_id', $id)->firstOrFail(); |
76
|
|
|
$this->authorize('update', $role); |
77
|
|
|
|
78
|
|
|
return Inertia::render('Admin/Roles/Edit', [ |
79
|
|
|
'role_data' => $role, |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Update the user Role |
85
|
|
|
*/ |
86
|
|
|
public function update(UserRoleRequest $request, $id) |
87
|
|
|
{ |
88
|
|
|
// Block a user from trying to update one of the default roles |
89
|
|
|
if($id <= 4) |
90
|
|
|
{ |
91
|
|
|
report('User '.$request->user()->username.' is trying to modify a default role'); |
92
|
|
|
abort(403, 'You cannot modify a default User Role'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Update the role details |
96
|
|
|
$role = UserRoles::find($id); |
97
|
|
|
$role->update($request->only(['name', 'description'])); |
98
|
|
|
|
99
|
|
|
// Update the role permissions |
100
|
|
|
foreach($request->user_role_permissions as $perm) |
101
|
|
|
{ |
102
|
|
|
UserRolePermissions::where(['role_id' => $perm['role_id'], 'perm_type_id' => $perm['perm_type_id']])->update([ |
103
|
|
|
'allow' => $perm['allow'] |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
event(new UserRoleUpdatedEvent($role)); |
108
|
|
|
return redirect(route('admin.user-roles.index'))->with([ |
109
|
|
|
'message' => 'Role Updated', |
110
|
|
|
'type' => 'success' |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Remove a User Role |
116
|
|
|
*/ |
117
|
|
|
public function destroy($id) |
118
|
|
|
{ |
119
|
|
|
$role = UserRoles::findOrFail($id); |
120
|
|
|
$this->authorize('forceDelete', $role); |
121
|
|
|
|
122
|
|
|
// verify this is not a default role |
123
|
|
|
$role = UserRoles::find($id); |
124
|
|
|
if(!$role->allow_edit) |
125
|
|
|
{ |
126
|
|
|
report('User '.Auth::user()->full_name.' is trying to delete one of the default User Roles'); |
|
|
|
|
127
|
|
|
return back()->with([ |
128
|
|
|
'message' => 'You cannot delete one of the default User Roles', |
129
|
|
|
'type' => 'danger' |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Verify that it is not in use |
134
|
|
|
$inUse = User::where('role_id', $id)->count(); |
135
|
|
|
if($inUse) |
136
|
|
|
{ |
137
|
|
|
report('User '.Auth::user()->username.' is trying to delete a role that is stil in use. Details'); |
|
|
|
|
138
|
|
|
return back()->with([ |
139
|
|
|
'message' => 'This User Role is in use. Please remove all users from this role before deleting', |
140
|
|
|
'type' => 'danger' |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$role->delete(); |
145
|
|
|
|
146
|
|
|
event(new UserRoleDeletedEvent($role)); |
|
|
|
|
147
|
|
|
return redirect(route('admin.user-roles.index')); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|