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 Xetaio\Mentions\Parser\MentionParser; |
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\Role; |
15
|
|
|
use Xetaravel\Models\Validators\UserValidator; |
16
|
|
|
|
17
|
|
|
class UserController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Show the search page. |
21
|
|
|
* |
22
|
|
|
* @return \Illuminate\View\View |
23
|
|
|
*/ |
24
|
|
|
public function index(): View |
25
|
|
|
{ |
26
|
|
|
$breadcrumbs = $this->breadcrumbs->addCrumb( |
27
|
|
|
'<i class="fa-solid fa-users mr-2"></i> Manage Users', |
28
|
|
|
route('admin.user.user.index') |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
return view('Admin::User.user.index', compact('breadcrumbs')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Show the update form. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Http\Request $request |
38
|
|
|
* @param string $slug The slug of the user. |
39
|
|
|
* @param int $id The id of the user. |
40
|
|
|
* |
41
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
42
|
|
|
*/ |
43
|
|
|
public function showUpdateForm(Request $request, string $slug, int $id) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$user = User::findOrFail($id); |
46
|
|
|
|
47
|
|
|
$roles = Role::pluck('name', 'id'); |
48
|
|
|
$attributes = Role::pluck('id')->toArray(); |
49
|
|
|
|
50
|
|
|
$optionsAttributes = []; |
51
|
|
|
foreach ($attributes as $attribute) { |
52
|
|
|
$optionsAttributes[$attribute] = [ |
53
|
|
|
'style' => Role::where('id', $attribute)->select('css')->first()->css |
|
|
|
|
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$breadcrumbs = $this->breadcrumbs |
58
|
|
|
->setListElementClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0') |
59
|
|
|
->addCrumb('<i class="fa-solid fa-users mr-2"></i> Manage Users', route('admin.user.user.index')) |
60
|
|
|
->addCrumb( |
61
|
|
|
'<i class="fa-solid fa-pen-to-square mr-2"></i> Edit ' . e($user->username), |
62
|
|
|
route('admin.user.user.update', $user->slug, $user->id) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return view('Admin::User.user.update', compact('user', 'roles', 'optionsAttributes', 'breadcrumbs')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handle an user update request for the application. |
70
|
|
|
* |
71
|
|
|
* @param \Illuminate\Http\Request $request |
72
|
|
|
* @param int $id The id of the user to update. |
73
|
|
|
* |
74
|
|
|
* @return \Illuminate\Http\RedirectResponse |
75
|
|
|
*/ |
76
|
|
|
public function update(Request $request, int $id): RedirectResponse |
77
|
|
|
{ |
78
|
|
|
$user = User::findOrFail($id); |
79
|
|
|
|
80
|
|
|
UserValidator::update($request->all(), $user->id)->validate(); |
81
|
|
|
UserRepository::update($request->all(), $user); |
82
|
|
|
$account = AccountRepository::update($request->get('account'), $user->id); |
83
|
|
|
|
84
|
|
|
$parser = new MentionParser($account, ['mention' => false]); |
85
|
|
|
$signature = $parser->parse($account->signature); |
86
|
|
|
$biography = $parser->parse($account->biography); |
87
|
|
|
|
88
|
|
|
$account->signature = $signature; |
89
|
|
|
$account->biography = $biography; |
90
|
|
|
$account->save(); |
91
|
|
|
|
92
|
|
|
$user->roles()->sync($request->get('roles')); |
93
|
|
|
|
94
|
|
|
return redirect() |
95
|
|
|
->route('admin.user.user.index') |
96
|
|
|
->with('success', 'This user has been updated successfully !'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Handle the delete request for the user. |
101
|
|
|
* |
102
|
|
|
* @param \Illuminate\Http\Request $request |
103
|
|
|
* @param int $id The id of the user to delete. |
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Http\RedirectResponse |
106
|
|
|
*/ |
107
|
|
|
public function delete(Request $request, int $id): RedirectResponse |
108
|
|
|
{ |
109
|
|
|
$user = User::findOrFail($id); |
110
|
|
|
|
111
|
|
|
if (!Hash::check($request->input('password'), Auth::user()->password)) { |
112
|
|
|
return redirect() |
113
|
|
|
->back() |
114
|
|
|
->with('danger', 'Your Password does not match !'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($user->delete()) { |
118
|
|
|
return redirect() |
119
|
|
|
->route('admin.user.user.index') |
120
|
|
|
->with('success', 'This user has been deleted successfully !'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return redirect() |
124
|
|
|
->route('admin.user.user.index') |
125
|
|
|
->with('danger', 'An error occurred while deleting this user !'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Delete the avatar for the specified user. |
130
|
|
|
* |
131
|
|
|
* @param int $id The id of the user. |
132
|
|
|
* |
133
|
|
|
* @return \Illuminate\Http\RedirectResponse |
134
|
|
|
*/ |
135
|
|
|
public function deleteAvatar(int $id): RedirectResponse |
136
|
|
|
{ |
137
|
|
|
$user = User::findOrFail($id); |
138
|
|
|
|
139
|
|
|
$user->clearMediaCollection('avatar'); |
140
|
|
|
$user->addMedia(public_path('images/avatar.png')) |
141
|
|
|
->preservingOriginal() |
142
|
|
|
->setName(substr(md5($user->username), 0, 10)) |
143
|
|
|
->setFileName(substr(md5($user->username), 0, 10) . '.png') |
144
|
|
|
->toMediaCollection('avatar'); |
145
|
|
|
|
146
|
|
|
return redirect() |
147
|
|
|
->back() |
148
|
|
|
->with('success', 'The avatar has been deleted successfully !'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.