1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers\Admin\User; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\RedirectResponse; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Support\Facades\Hash; |
8
|
|
|
use Illuminate\View\View; |
9
|
|
|
use Ultraware\Roles\Models\Role; |
10
|
|
|
use Xetaravel\Http\Controllers\Admin\Controller; |
11
|
|
|
use Xetaravel\Models\Repositories\UserRepository; |
12
|
|
|
use Xetaravel\Models\Repositories\AccountRepository; |
13
|
|
|
use Xetaravel\Models\User; |
14
|
|
|
use Xetaravel\Models\Validators\UserValidator; |
15
|
|
|
|
16
|
|
|
class UserController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Show the search page. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\View\View |
22
|
|
|
*/ |
23
|
|
|
public function index(): View |
24
|
|
|
{ |
25
|
|
|
$latestUsers = User::with(['roles']) |
26
|
|
|
->limit(5) |
27
|
|
|
->latest() |
28
|
|
|
->get(); |
29
|
|
|
|
30
|
|
|
$breadcrumbs = $this->breadcrumbs->addCrumb('Manage Users', route('admin.user.user.index')); |
|
|
|
|
31
|
|
|
|
32
|
|
|
return view('Admin::User.user.index', compact('latestUsers', 'breadcrumbs')); |
33
|
|
|
} |
34
|
|
|
/** |
35
|
|
|
* Search users related to the type. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Http\Request $request |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\View\View |
40
|
|
|
*/ |
41
|
|
|
public function search(Request $request): View |
42
|
|
|
{ |
43
|
|
|
$query = User::with(['roles'])->select(); |
44
|
|
|
$search = str_replace('%', '\\%', trim($request->input('search'))); |
45
|
|
|
$type = trim($request->input('type')); |
46
|
|
|
|
47
|
|
|
switch ($type) { |
48
|
|
|
case 'username': |
49
|
|
|
$query->where('username', 'like', '%' . $search . '%'); |
50
|
|
|
break; |
51
|
|
|
|
52
|
|
|
case 'email': |
53
|
|
|
$query->where('email', 'like', '%' . $search . '%'); |
54
|
|
|
break; |
55
|
|
|
|
56
|
|
|
case 'register_ip': |
57
|
|
|
$query->where('register_ip', 'like', '%' . $search . '%'); |
58
|
|
|
break; |
59
|
|
|
|
60
|
|
|
case 'last_login_ip': |
61
|
|
|
$query->where('last_login_ip', 'like', '%' . $search . '%'); |
62
|
|
|
break; |
63
|
|
|
|
64
|
|
|
default: |
65
|
|
|
$query->where('username', 'like', '%' . $search . '%'); |
66
|
|
|
$type = 'username'; |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
$users = $query |
70
|
|
|
->paginate(10) |
71
|
|
|
->appends($request->except('page')); |
72
|
|
|
|
73
|
|
|
$breadcrumbs = $this->breadcrumbs |
74
|
|
|
->addCrumb('Manage Users', route('admin.user.user.index')) |
75
|
|
|
->addCrumb('Search an user', route('admin.user.user.search')); |
76
|
|
|
|
77
|
|
|
return view('Admin::User.user.search', compact('users', 'breadcrumbs', 'type', 'search')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Show the update form. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Http\Request $request |
84
|
|
|
* @param string $slug The slug of the user. |
85
|
|
|
* @param int $id The id of the user. |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
public function showUpdateForm(Request $request, string $slug, int $id) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$user = User::findOrFail($id); |
92
|
|
|
|
93
|
|
|
$roles = Role::pluck('name', 'id'); |
94
|
|
|
$attributes = Role::pluck('id')->toArray(); |
95
|
|
|
|
96
|
|
|
$optionsAttributes = []; |
97
|
|
|
foreach ($attributes as $attribute) { |
98
|
|
|
$optionsAttributes[$attribute] = [ |
99
|
|
|
'style' => Role::where('id', $attribute)->select('css')->first()->css |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$breadcrumbs = $this->breadcrumbs |
104
|
|
|
->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0') |
105
|
|
|
->addCrumb('Manage Users', route('admin.user.user.index')) |
106
|
|
|
->addCrumb( |
107
|
|
|
'Edit ' . e($user->username), |
108
|
|
|
route('admin.user.user.update', $user->slug, $user->id) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return view('Admin::User.user.update', compact('user', 'roles', 'optionsAttributes', 'breadcrumbs')); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Handle an user update request for the application. |
116
|
|
|
* |
117
|
|
|
* @param \Illuminate\Http\Request $request |
118
|
|
|
* @param int $id The id of the user to update. |
119
|
|
|
* |
120
|
|
|
* @return \Illuminate\Http\RedirectResponse |
121
|
|
|
*/ |
122
|
|
|
public function update(Request $request, int $id): RedirectResponse |
123
|
|
|
{ |
124
|
|
|
$user = User::findOrFail($id); |
125
|
|
|
|
126
|
|
|
UserValidator::update($request->all(), $user->id)->validate(); |
127
|
|
|
UserRepository::update($request->all(), $user); |
128
|
|
|
AccountRepository::update($request->get('account'), $user->id); |
129
|
|
|
|
130
|
|
|
$user->roles()->sync($request->get('roles')); |
131
|
|
|
|
132
|
|
|
return redirect() |
133
|
|
|
->back() |
134
|
|
|
->with('success', 'This user has been updated successfully !'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Handle the delete request for the user. |
139
|
|
|
* |
140
|
|
|
* @param \Illuminate\Http\Request $request |
141
|
|
|
* @param int $id The id of the user to delete. |
142
|
|
|
* |
143
|
|
|
* @return \Illuminate\Http\RedirectResponse |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function delete(Request $request, int $id): RedirectResponse |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$user = User::findOrFail($id); |
148
|
|
|
|
149
|
|
|
if (!Hash::check($request->input('password'), Auth::user()->password)) { |
|
|
|
|
150
|
|
|
return redirect() |
151
|
|
|
->back() |
152
|
|
|
->with('danger', 'Your Password does not match !'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($user->delete()) { |
156
|
|
|
return redirect() |
157
|
|
|
->route('admin.user.user.index') |
158
|
|
|
->with('success', 'This user has been deleted successfully !'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return redirect() |
162
|
|
|
->route('admin.user.user.index') |
163
|
|
|
->with('danger', 'An error occurred while deleting this user !'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Delete the avatar for the specified user. |
168
|
|
|
* |
169
|
|
|
* @param int $id The id of the user. |
170
|
|
|
* |
171
|
|
|
* @return \Illuminate\Http\RedirectResponse |
172
|
|
|
*/ |
173
|
|
|
public function deleteAvatar(int $id): RedirectResponse |
174
|
|
|
{ |
175
|
|
|
$user = User::findOrFail($id); |
176
|
|
|
|
177
|
|
|
$user->clearMediaCollection('avatar'); |
178
|
|
|
$user->addMedia(resource_path('assets/images/avatar.png')) |
179
|
|
|
->preservingOriginal() |
180
|
|
|
->setName(substr(md5($user->username), 0, 10)) |
181
|
|
|
->setFileName(substr(md5($user->username), 0, 10) . '.png') |
182
|
|
|
->toMediaCollection('avatar'); |
183
|
|
|
|
184
|
|
|
return redirect() |
185
|
|
|
->back() |
186
|
|
|
->with('success', 'The avatar has been deleted successfully !'); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: