|
1
|
|
|
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelApiHelper\Traits\JsonResponses; |
|
4
|
|
|
use Arcanedev\LaravelAuth\Services\UserImpersonator; |
|
5
|
|
|
use Arcanesoft\Auth\Http\Requests\Admin\Users\CreateUserRequest; |
|
6
|
|
|
use Arcanesoft\Auth\Http\Requests\Admin\Users\UpdateUserRequest; |
|
7
|
|
|
use Arcanesoft\Auth\Policies\UsersPolicy; |
|
8
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role; |
|
9
|
|
|
use Arcanesoft\Contracts\Auth\Models\User; |
|
10
|
|
|
use Log; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class UsersController |
|
14
|
|
|
* |
|
15
|
|
|
* @package Arcanesoft\Auth\Http\Controllers\Admin |
|
16
|
|
|
* @author ARCANEDEV <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class UsersController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
/* ----------------------------------------------------------------- |
|
21
|
|
|
| Traits |
|
22
|
|
|
| ----------------------------------------------------------------- |
|
23
|
|
|
*/ |
|
24
|
|
|
use JsonResponses; |
|
25
|
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
|
27
|
|
|
| Properties |
|
28
|
|
|
| ----------------------------------------------------------------- |
|
29
|
|
|
*/ |
|
30
|
|
|
/** |
|
31
|
|
|
* The user model. |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Arcanesoft\Contracts\Auth\Models\User |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $user; |
|
36
|
|
|
|
|
37
|
|
|
/* ----------------------------------------------------------------- |
|
38
|
|
|
| Constructor |
|
39
|
|
|
| ----------------------------------------------------------------- |
|
40
|
|
|
*/ |
|
41
|
|
|
/** |
|
42
|
|
|
* Instantiate the controller. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(User $user) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct(); |
|
49
|
|
|
|
|
50
|
|
|
$this->user = $user; |
|
51
|
|
|
|
|
52
|
|
|
$this->setCurrentPage('auth-users'); |
|
53
|
|
|
$this->addBreadcrumbRoute('Users', 'admin::auth.users.index'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/* ----------------------------------------------------------------- |
|
57
|
|
|
| Main Methods |
|
58
|
|
|
| ----------------------------------------------------------------- |
|
59
|
|
|
*/ |
|
60
|
|
|
/** |
|
61
|
|
|
* List the users. |
|
62
|
|
|
* |
|
63
|
|
|
* @param bool $trashed |
|
64
|
|
|
* |
|
65
|
|
|
* @return \Illuminate\View\View |
|
66
|
|
|
*/ |
|
67
|
|
|
public function index($trashed = false) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->authorize(UsersPolicy::PERMISSION_LIST); |
|
70
|
|
|
|
|
71
|
|
|
$users = $this->user->with('roles'); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$users = $trashed |
|
74
|
|
|
? $users->onlyTrashed()->paginate(30) |
|
75
|
|
|
: $users->paginate(30); |
|
76
|
|
|
|
|
77
|
|
|
$this->setTitle($title = 'List of users' . ($trashed ? ' - Trashed' : '')); |
|
78
|
|
|
$this->addBreadcrumb($title); |
|
79
|
|
|
|
|
80
|
|
|
return $this->view('admin.users.list', compact('trashed', 'users')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* List the trashed users. |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Illuminate\View\View |
|
87
|
|
|
*/ |
|
88
|
|
|
public function trashList() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->index(true); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* List the users by a role. |
|
95
|
|
|
* |
|
96
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role $role |
|
97
|
|
|
* @param bool $trashed |
|
98
|
|
|
* |
|
99
|
|
|
* @return \Illuminate\View\View |
|
100
|
|
|
*/ |
|
101
|
|
|
public function listByRole(Role $role, $trashed = false) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->authorize(UsersPolicy::PERMISSION_LIST); |
|
104
|
|
|
|
|
105
|
|
|
$users = $role->users()->with('roles')->paginate(30); |
|
106
|
|
|
|
|
107
|
|
|
$this->setTitle($title = "List of users - {$role->name} Role" . ($trashed ? ' - Trashed' : '')); |
|
|
|
|
|
|
108
|
|
|
$this->addBreadcrumb($title); |
|
109
|
|
|
|
|
110
|
|
|
return $this->view('admin.users.list', compact('trashed', 'users')); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Show the create a new user form. |
|
115
|
|
|
* |
|
116
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role $role |
|
117
|
|
|
* |
|
118
|
|
|
* @return \Illuminate\View\View |
|
119
|
|
|
*/ |
|
120
|
|
|
public function create(Role $role) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->authorize(UsersPolicy::PERMISSION_CREATE); |
|
123
|
|
|
|
|
124
|
|
|
$roles = $role->all(); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
$this->setTitle($title = 'Create a new user'); |
|
127
|
|
|
$this->addBreadcrumb($title); |
|
128
|
|
|
|
|
129
|
|
|
return $this->view('admin.users.create', compact('roles')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Store the new user. |
|
134
|
|
|
* |
|
135
|
|
|
* @param \Arcanesoft\Auth\Http\Requests\Admin\Users\CreateUserRequest $request |
|
136
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
137
|
|
|
* |
|
138
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
139
|
|
|
*/ |
|
140
|
|
|
public function store(CreateUserRequest $request, User $user) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->authorize(UsersPolicy::PERMISSION_CREATE); |
|
143
|
|
|
|
|
144
|
|
|
$data = $request->only([ |
|
145
|
|
|
'username', 'email', 'first_name', 'last_name', 'password' |
|
146
|
|
|
]); |
|
147
|
|
|
$user->fill($data); |
|
|
|
|
|
|
148
|
|
|
$user->is_active = true; |
|
|
|
|
|
|
149
|
|
|
$user->save(); |
|
150
|
|
|
$user->roles()->sync($request->get('roles')); |
|
151
|
|
|
|
|
152
|
|
|
$this->transNotification('created', ['name' => $user->full_name], $user->toArray()); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
return redirect()->route('admin::auth.users.index'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Show the user's details. |
|
159
|
|
|
* |
|
160
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
161
|
|
|
* |
|
162
|
|
|
* @return \Illuminate\View\View |
|
163
|
|
|
*/ |
|
164
|
|
|
public function show(User $user) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->authorize(UsersPolicy::PERMISSION_SHOW); |
|
167
|
|
|
|
|
168
|
|
|
$user->load(['roles', 'roles.permissions']); |
|
169
|
|
|
|
|
170
|
|
|
$this->setTitle($title = 'User details'); |
|
171
|
|
|
$this->addBreadcrumb($title); |
|
172
|
|
|
|
|
173
|
|
|
return $this->view('admin.users.show', compact('user')); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Show the edit the user form. |
|
178
|
|
|
* |
|
179
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
180
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role $role |
|
181
|
|
|
* |
|
182
|
|
|
* @return \Illuminate\View\View |
|
183
|
|
|
*/ |
|
184
|
|
|
public function edit(User $user, Role $role) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->authorize(UsersPolicy::PERMISSION_UPDATE); |
|
187
|
|
|
|
|
188
|
|
|
$user->load(['roles', 'roles.permissions']); |
|
189
|
|
|
$roles = $role->all(); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
$this->setTitle($title = 'Edit a user'); |
|
192
|
|
|
$this->addBreadcrumb($title); |
|
193
|
|
|
|
|
194
|
|
|
return $this->view('admin.users.edit', compact('user', 'roles')); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Update the user. |
|
199
|
|
|
* |
|
200
|
|
|
* @param \Arcanesoft\Auth\Http\Requests\Admin\Users\UpdateUserRequest $request |
|
201
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
202
|
|
|
* |
|
203
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
204
|
|
|
*/ |
|
205
|
|
|
public function update(UpdateUserRequest $request, User $user) |
|
206
|
|
|
{ |
|
207
|
|
|
$this->authorize(UsersPolicy::PERMISSION_UPDATE); |
|
208
|
|
|
|
|
209
|
|
|
$user->update($request->intersect(['username', 'email', 'password', 'first_name', 'last_name'])); |
|
|
|
|
|
|
210
|
|
|
$user->roles()->sync($request->get('roles')); |
|
211
|
|
|
|
|
212
|
|
|
$this->transNotification('updated', ['name' => $user->full_name], $user->toArray()); |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
return redirect()->route('admin::auth.users.show', [$user->hashed_id]); |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Activate/Disable a user. |
|
219
|
|
|
* |
|
220
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
221
|
|
|
* |
|
222
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
223
|
|
|
*/ |
|
224
|
|
|
public function activate(User $user) |
|
225
|
|
|
{ |
|
226
|
|
|
$this->authorize(UsersPolicy::PERMISSION_UPDATE); |
|
227
|
|
|
|
|
228
|
|
|
try { |
|
229
|
|
|
($active = $user->isActive()) ? $user->deactivate() : $user->activate(); |
|
230
|
|
|
|
|
231
|
|
|
return $this->jsonResponseSuccess( |
|
232
|
|
|
$this->transNotification($active ? 'disabled' : 'enabled', ['name' => $user->full_name], $user->toArray()) |
|
|
|
|
|
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
catch (\Exception $e) { |
|
236
|
|
|
return $this->jsonResponseError($e->getMessage(), 500); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Restore the trashed user. |
|
242
|
|
|
* |
|
243
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
244
|
|
|
* |
|
245
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
246
|
|
|
*/ |
|
247
|
|
|
public function restore(User $user) |
|
248
|
|
|
{ |
|
249
|
|
|
$this->authorize(UsersPolicy::PERMISSION_UPDATE); |
|
250
|
|
|
|
|
251
|
|
|
try { |
|
252
|
|
|
$user->restore(); |
|
253
|
|
|
|
|
254
|
|
|
return $this->jsonResponseSuccess( |
|
255
|
|
|
$this->transNotification('restored', ['name' => $user->full_name], $user->toArray()) |
|
|
|
|
|
|
256
|
|
|
); |
|
257
|
|
|
} |
|
258
|
|
|
catch (\Exception $e) { |
|
259
|
|
|
return $this->jsonResponseError($e->getMessage(), 500); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Delete a user. |
|
265
|
|
|
* |
|
266
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
267
|
|
|
* |
|
268
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
269
|
|
|
*/ |
|
270
|
|
|
public function delete(User $user) |
|
271
|
|
|
{ |
|
272
|
|
|
$this->authorize(UsersPolicy::PERMISSION_DELETE); |
|
273
|
|
|
|
|
274
|
|
|
try { |
|
275
|
|
|
($trashed = $user->trashed()) ? $user->forceDelete() : $user->delete(); |
|
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
return $this->jsonResponseSuccess( |
|
278
|
|
|
$this->transNotification($trashed ? 'deleted' : 'trashed', ['name' => $user->full_name], $user->toArray()) |
|
|
|
|
|
|
279
|
|
|
); |
|
280
|
|
|
} |
|
281
|
|
|
catch(\Exception $e) { |
|
282
|
|
|
return $this->jsonResponseError($e->getMessage(), 500); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Impersonate a user. |
|
288
|
|
|
* |
|
289
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User $user |
|
290
|
|
|
* |
|
291
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
292
|
|
|
*/ |
|
293
|
|
|
public function impersonate(User $user) |
|
294
|
|
|
{ |
|
295
|
|
|
if (UserImpersonator::start($user)) |
|
296
|
|
|
return redirect()->to('/'); |
|
297
|
|
|
|
|
298
|
|
|
$this->notifyDanger('Impersonate disabled for this user.', 'Impersonation failed'); |
|
299
|
|
|
|
|
300
|
|
|
return redirect()->back(); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/* ----------------------------------------------------------------- |
|
304
|
|
|
| Other Functions |
|
305
|
|
|
| ----------------------------------------------------------------- |
|
306
|
|
|
*/ |
|
307
|
|
|
/** |
|
308
|
|
|
* Notify with translation. |
|
309
|
|
|
* |
|
310
|
|
|
* @param string $action |
|
311
|
|
|
* @param array $replace |
|
312
|
|
|
* @param array $context |
|
313
|
|
|
* |
|
314
|
|
|
* @return string |
|
315
|
|
|
*/ |
|
316
|
|
|
protected function transNotification($action, array $replace = [], array $context = []) |
|
317
|
|
|
{ |
|
318
|
|
|
$title = trans("auth::users.messages.{$action}.title"); |
|
319
|
|
|
$message = trans("auth::users.messages.{$action}.message", $replace); |
|
320
|
|
|
|
|
321
|
|
|
Log::info($message, $context); |
|
322
|
|
|
$this->notifySuccess($message, $title); |
|
323
|
|
|
|
|
324
|
|
|
return $message; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.