1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Fort\Http\Controllers\Adminarea; |
6
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Spatie\MediaLibrary\Models\Media; |
9
|
|
|
use Rinvex\Fort\Contracts\UserContract; |
10
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
11
|
|
|
use Cortex\Foundation\DataTables\LogsDataTable; |
12
|
|
|
use Cortex\Fort\DataTables\Adminarea\UsersDataTable; |
13
|
|
|
use Cortex\Foundation\DataTables\ActivitiesDataTable; |
14
|
|
|
use Cortex\Fort\Http\Requests\Adminarea\UserFormRequest; |
15
|
|
|
use Cortex\Foundation\Http\Controllers\AuthorizedController; |
16
|
|
|
|
17
|
|
|
class UsersController extends AuthorizedController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
protected $resource = 'users'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Display a listing of the resource. |
26
|
|
|
* |
27
|
|
|
* @param \Cortex\Fort\DataTables\Adminarea\UsersDataTable $usersDataTable |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
30
|
|
|
*/ |
31
|
|
|
public function index(UsersDataTable $usersDataTable) |
32
|
|
|
{ |
33
|
|
|
return $usersDataTable->with([ |
34
|
|
|
'id' => 'cortex-users', |
35
|
|
|
'phrase' => trans('cortex/fort::common.users'), |
36
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get a listing of the resource logs. |
41
|
|
|
* |
42
|
|
|
* @param \Rinvex\Fort\Contracts\UserContract $user |
43
|
|
|
* |
44
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
45
|
|
|
*/ |
46
|
|
|
public function logs(UserContract $user) |
47
|
|
|
{ |
48
|
|
|
return request()->ajax() && request()->wantsJson() |
49
|
|
|
? app(LogsDataTable::class)->with(['resource' => $user])->ajax() |
50
|
|
|
: intend(['url' => route('adminarea.users.edit', ['user' => $user]).'#logs-tab']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get a listing of the resource activities. |
55
|
|
|
* |
56
|
|
|
* @param \Rinvex\Fort\Contracts\UserContract $user |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
59
|
|
|
*/ |
60
|
|
|
public function activities(UserContract $user) |
61
|
|
|
{ |
62
|
|
|
return request()->ajax() && request()->wantsJson() |
63
|
|
|
? app(ActivitiesDataTable::class)->with(['resource' => $user])->ajax() |
64
|
|
|
: intend(['url' => route('adminarea.users.edit', ['user' => $user]).'#activities-tab']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Show the form for create/update of the given resource. |
69
|
|
|
* |
70
|
|
|
* @param \Illuminate\Http\Request $request |
71
|
|
|
* @param \Rinvex\Fort\Contracts\UserContract $user |
72
|
|
|
* |
73
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
74
|
|
|
*/ |
75
|
|
|
public function form(Request $request, UserContract $user) |
76
|
|
|
{ |
77
|
|
|
$countries = countries(); |
78
|
|
|
$authUser = $request->user($this->getGuard()); |
79
|
|
|
$languages = collect(languages())->pluck('name', 'iso_639_1'); |
80
|
|
|
$genders = ['m' => trans('cortex/fort::common.male'), 'f' => trans('cortex/fort::common.female')]; |
81
|
|
|
|
82
|
|
|
$roles = $authUser->isSuperadmin() |
83
|
|
|
? app('rinvex.fort.role')->all()->pluck('name', 'id')->toArray() |
84
|
|
|
: $authUser->roles->pluck('name', 'id')->toArray(); |
85
|
|
|
|
86
|
|
|
$abilities = $authUser->isSuperadmin() |
87
|
|
|
? app('rinvex.fort.ability')->all()->groupBy('resource')->map->pluck('name', 'id')->toArray() |
88
|
|
|
: $authUser->allAbilities->groupBy('resource')->map->pluck('name', 'id')->toArray(); |
89
|
|
|
|
90
|
|
|
$logs = app(LogsDataTable::class)->with(['id' => 'logs-table'])->html()->minifiedAjax(route('adminarea.users.logs', ['user' => $user])); |
|
|
|
|
91
|
|
|
$activities = app(ActivitiesDataTable::class)->with(['id' => 'activities-table'])->html()->minifiedAjax(route('adminarea.users.activities', ['user' => $user])); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return view('cortex/fort::adminarea.pages.user', compact('user', 'abilities', 'roles', 'countries', 'languages', 'genders', 'logs', 'activities')); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Store a newly created resource in storage. |
98
|
|
|
* |
99
|
|
|
* @param \Cortex\Fort\Http\Requests\Adminarea\UserFormRequest $request |
100
|
|
|
* |
101
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
102
|
|
|
*/ |
103
|
|
|
public function store(UserFormRequest $request) |
104
|
|
|
{ |
105
|
|
|
return $this->process($request, app('rinvex.fort.user')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Update the given resource in storage. |
110
|
|
|
* |
111
|
|
|
* @param \Cortex\Fort\Http\Requests\Adminarea\UserFormRequest $request |
112
|
|
|
* @param \Rinvex\Fort\Contracts\UserContract $user |
113
|
|
|
* |
114
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
public function update(UserFormRequest $request, UserContract $user) |
117
|
|
|
{ |
118
|
|
|
return $this->process($request, $user); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Process the form for store/update of the given resource. |
123
|
|
|
* |
124
|
|
|
* @param \Illuminate\Foundation\Http\FormRequest $request |
125
|
|
|
* @param \Rinvex\Fort\Contracts\UserContract $user |
126
|
|
|
* |
127
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
128
|
|
|
*/ |
129
|
|
|
protected function process(FormRequest $request, UserContract $user) |
130
|
|
|
{ |
131
|
|
|
// Prepare required input fields |
132
|
|
|
$data = $request->validated(); |
133
|
|
|
|
134
|
|
|
! $request->hasFile('profile_picture') |
135
|
|
|
|| $user->addMediaFromRequest('profile_picture') |
|
|
|
|
136
|
|
|
->sanitizingFileName(function ($fileName) { |
137
|
|
|
return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION); |
138
|
|
|
}) |
139
|
|
|
->toMediaCollection('profile_picture', config('cortex.fort.media.disk')); |
140
|
|
|
|
141
|
|
|
! $request->hasFile('cover_photo') |
142
|
|
|
|| $user->addMediaFromRequest('cover_photo') |
|
|
|
|
143
|
|
|
->sanitizingFileName(function ($fileName) { |
144
|
|
|
return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION); |
145
|
|
|
}) |
146
|
|
|
->toMediaCollection('cover_photo', config('cortex.fort.media.disk')); |
147
|
|
|
|
148
|
|
|
// Save user |
149
|
|
|
$user->fill($data)->save(); |
150
|
|
|
|
151
|
|
|
return intend([ |
152
|
|
|
'url' => route('adminarea.users.index'), |
153
|
|
|
'with' => ['success' => trans('cortex/fort::messages.user.saved', ['username' => $user->username])], |
|
|
|
|
154
|
|
|
]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Delete the given resource from storage. |
159
|
|
|
* |
160
|
|
|
* @param \Rinvex\Fort\Contracts\UserContract $user |
161
|
|
|
* |
162
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
163
|
|
|
*/ |
164
|
|
|
public function delete(UserContract $user) |
165
|
|
|
{ |
166
|
|
|
$user->delete(); |
167
|
|
|
|
168
|
|
|
return intend([ |
169
|
|
|
'url' => route('adminarea.users.index'), |
170
|
|
|
'with' => ['warning' => trans('cortex/fort::messages.user.deleted', ['username' => $user->username])], |
|
|
|
|
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Delete the given resource from storage. |
176
|
|
|
* |
177
|
|
|
* @param \Rinvex\Fort\Contracts\UserContract $user |
178
|
|
|
* @param \Spatie\MediaLibrary\Models\Media $media |
179
|
|
|
* |
180
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
181
|
|
|
*/ |
182
|
|
|
public function deleteMedia(UserContract $user, Media $media) |
183
|
|
|
{ |
184
|
|
|
$user->media()->where('id', $media->id)->first()->delete(); |
|
|
|
|
185
|
|
|
|
186
|
|
|
return intend([ |
187
|
|
|
'url' => route('adminarea.users.edit', ['user' => $user]), |
188
|
|
|
'with' => ['warning' => trans('cortex/fort::messages.user.media_deleted')], |
189
|
|
|
]); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.