1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use Inertia\Inertia; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
|
9
|
|
|
use App\Actions\GetUserRoles; |
10
|
|
|
use App\Http\Controllers\Controller; |
11
|
|
|
use App\Http\Requests\User\UserRequest; |
12
|
|
|
|
13
|
|
|
use App\Models\User; |
14
|
|
|
use App\Models\UserRoles; |
15
|
|
|
use App\Models\UserSetting; |
16
|
|
|
use App\Models\UserSettingType; |
17
|
|
|
|
18
|
|
|
use App\Events\Admin\NewUserCreated; |
19
|
|
|
use App\Events\Admin\UserUpdatedEvent; |
20
|
|
|
use App\Events\Admin\UserDeactivatedEvent; |
21
|
|
|
|
22
|
|
|
class UserController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
142 |
|
* Display a listing of all active users |
26
|
|
|
*/ |
27
|
142 |
|
public function index() |
28
|
|
|
{ |
29
|
122 |
|
$this->authorize('create', User::class); |
30
|
102 |
|
|
31
|
142 |
|
return Inertia::render('Admin/User/Index', [ |
32
|
142 |
|
'users' => User::with('UserRoles')->get(), |
33
|
|
|
]); |
34
|
|
|
} |
35
|
2 |
|
|
36
|
|
|
/** |
37
|
2 |
|
* Show the new user form |
38
|
|
|
*/ |
39
|
2 |
|
public function create() |
40
|
2 |
|
{ |
41
|
|
|
$this->authorize('create', User::class); |
42
|
2 |
|
|
43
|
|
|
return Inertia::render('Admin/User/Create', [ |
44
|
2 |
|
'roles' => (new GetUserRoles)->run(/** @scrutinizer ignore-type */ Auth::user()), |
45
|
2 |
|
]); |
46
|
2 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Store a newly created user |
50
|
|
|
*/ |
51
|
12 |
|
public function store(UserRequest $request) |
52
|
|
|
{ |
53
|
12 |
|
$newUser = User::create($request->toArray()); |
54
|
|
|
|
55
|
12 |
|
// Add the users settings data |
56
|
|
|
$settings = UserSettingType::all(); |
57
|
12 |
|
foreach($settings as $setting) |
58
|
|
|
{ |
59
|
4 |
|
UserSetting::create([ |
60
|
4 |
|
'user_id' => $newUser->user_id, |
61
|
|
|
'setting_type_id' => $setting->setting_type_id, |
62
|
|
|
'value' => true, |
63
|
8 |
|
]); |
64
|
8 |
|
} |
65
|
8 |
|
|
66
|
8 |
|
event(new NewUserCreated($newUser)); |
67
|
8 |
|
return back()->with([ |
68
|
8 |
|
'message' => 'New User Created', |
69
|
|
|
'type' => 'success', |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
/** |
74
|
|
|
* Show form for editing an existing user |
75
|
8 |
|
*/ |
76
|
|
|
public function edit($id) |
77
|
8 |
|
{ |
78
|
8 |
|
$this->authorize('create', User::class); |
79
|
|
|
|
80
|
8 |
|
return Inertia::render('Admin/User/Edit', [ |
81
|
|
|
'user' => User::where('username', $id)->firstOrFail()->makeVisible(['user_id', 'role_id']), |
82
|
8 |
|
'roles' => UserRoles::all(), |
83
|
|
|
]); |
84
|
4 |
|
} |
85
|
4 |
|
|
86
|
|
|
/** |
87
|
8 |
|
* Update a user's information |
88
|
|
|
*/ |
89
|
2 |
|
public function update(UserRequest $request, $id) |
90
|
2 |
|
{ |
91
|
|
|
$user = User::findOrFail($id); |
92
|
|
|
|
93
|
|
|
if(Auth::user()->role_id > $user->role_id) |
94
|
8 |
|
{ |
95
|
8 |
|
return back()->with([ |
96
|
8 |
|
'message' => 'You cannot modify a user with higher permissions than you', |
97
|
|
|
'type' => 'danger' |
98
|
|
|
]); |
99
|
|
|
} |
100
|
|
|
|
101
|
8 |
|
$user->update($request->toArray()); |
102
|
8 |
|
|
103
|
8 |
|
event(new UserUpdatedEvent($user)); |
104
|
|
|
return redirect(route('admin.user.index'))->with([ |
105
|
|
|
'message' => 'User Details Updated', |
106
|
|
|
'type' => 'success' |
107
|
|
|
]); |
108
|
16 |
|
} |
109
|
|
|
|
110
|
16 |
|
/** |
111
|
|
|
* Deactivate a user |
112
|
|
|
*/ |
113
|
16 |
|
public function destroy($id) |
114
|
16 |
|
{ |
115
|
|
|
$user = User::where('username', $id)->firstOrFail(); |
116
|
|
|
$this->authorize('create', $user); |
117
|
|
|
$user->delete(); |
118
|
|
|
|
119
|
|
|
// if($user->role_id > Auth::user()->role_id) |
120
|
|
|
// { |
121
|
|
|
// TODO - User cannot deactivate a user with higher permissions than themselves |
122
|
2 |
|
// abort(403, 'You cannot deactivate someone with higher permissions than yourself'); |
123
|
2 |
|
// } |
124
|
2 |
|
|
125
|
2 |
|
event(new UserDeactivatedEvent($user)); |
126
|
2 |
|
return back()->with([ |
127
|
2 |
|
'message' => 'User has been deactivated', |
128
|
2 |
|
'type' => 'danger', |
129
|
|
|
]); |
130
|
2 |
|
} |
131
|
|
|
} |
132
|
|
|
|