|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use App\Http\Requests\AttachUsers; |
|
7
|
|
|
use App\Http\Requests\CreateRole; |
|
8
|
|
|
use App\Http\Requests\UpdateRole; |
|
9
|
|
|
use App\Permission; |
|
10
|
|
|
use App\Role; |
|
11
|
|
|
use App\User; |
|
12
|
|
|
use Illuminate\Support\Facades\Log; |
|
13
|
|
|
|
|
14
|
|
|
class RoleController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
public function displayRole(Role $role) |
|
17
|
|
|
{ |
|
18
|
|
|
return view('admin.iam.manage-role', [ |
|
19
|
|
|
'role' => $role, |
|
20
|
|
|
'users' => User::whereNotNull('email_verified_at')->orderBy('name', 'ASC')->get(), |
|
21
|
|
|
'permissions' => Permission::orderBy('name', 'ASC')->get() |
|
22
|
|
|
]); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function updateRole(Role $role, UpdateRole $request) |
|
26
|
|
|
{ |
|
27
|
|
|
$validated = $request->validated(); |
|
28
|
|
|
|
|
29
|
|
|
// Set the new submitted (direct) role properties |
|
30
|
|
|
$role->name = $validated['name']; |
|
31
|
|
|
$role->save(); |
|
32
|
|
|
|
|
33
|
|
|
// Detach all roles from the role |
|
34
|
|
|
$role->permissions()->detach(); |
|
35
|
|
|
|
|
36
|
|
|
// Attach only the given roles to the role |
|
37
|
|
|
Log::info('Attached permission-ids ' . implode('|', $validated['permissions']) . ' to role#' . $role->id); |
|
38
|
|
|
$role->permissions()->attach($validated['permissions']); |
|
39
|
|
|
|
|
40
|
|
|
// On successfull update redirect the browser to the role overview |
|
41
|
|
|
return redirect() |
|
42
|
|
|
->route('admin.iam.role.manage', $role) |
|
43
|
|
|
->with('status', 'Update successfull!'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function createRole(CreateRole $request) |
|
47
|
|
|
{ |
|
48
|
|
|
$validated = $request->validated(); |
|
49
|
|
|
$role = Role::create([ |
|
50
|
|
|
'name' => $validated['name'] |
|
51
|
|
|
]); |
|
52
|
|
|
// On successfull creation redirect the browser to the role overview |
|
53
|
|
|
return redirect() |
|
54
|
|
|
->route('admin.iam.role.manage', $role) |
|
55
|
|
|
->with('status', 'Update successfull!'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function deleteRole(Role $role) |
|
59
|
|
|
{ |
|
60
|
|
|
Log::info('Deleting role#' . $role->id . ' (' . $role->name . ') with attached user-ids ' . $role->users->implode('id', '|') . ' and attached permission-ids ' . $role->permissions->implode('id', '|')); |
|
61
|
|
|
|
|
62
|
|
|
$role->users()->detach(); |
|
63
|
|
|
$role->permissions()->detach(); |
|
64
|
|
|
$role->delete(); |
|
65
|
|
|
|
|
66
|
|
|
// On successfull deletion redirect the browser to the overview |
|
67
|
|
|
return redirect() |
|
68
|
|
|
->route('admin.iam.dashboard') |
|
69
|
|
|
->with('status', 'Deleted role successfull!'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function attachUsers(Role $role, AttachUsers $request) |
|
73
|
|
|
{ |
|
74
|
|
|
$validated = $request->validated(); |
|
75
|
|
|
|
|
76
|
|
|
Log::info('Attaching role#' . $role->id . ' to users with ids' . implode('|', $validated['id'])); |
|
77
|
|
|
$role->users()->attach($validated['id']); |
|
78
|
|
|
|
|
79
|
|
|
return redirect() |
|
80
|
|
|
->route('admin.iam.role.manage', $role) |
|
81
|
|
|
->with('status', 'Users added to role successfully!'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function detachUser(Role $role, User $user) |
|
85
|
|
|
{ |
|
86
|
|
|
Log::info('Detaching role#' . $role->id . ' from user#' . $user->id); |
|
87
|
|
|
$role->users()->detach($user); |
|
88
|
|
|
return redirect() |
|
89
|
|
|
->route('admin.iam.role.manage', $role) |
|
90
|
|
|
->with('status', 'User ' . $user->name . ' removed from role successfully!'); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|