1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Http\Controllers\Backend; |
17
|
|
|
|
18
|
|
|
use Illuminate\Http\Request; |
19
|
|
|
use Rinvex\Fort\Models\User; |
20
|
|
|
use Rinvex\Fort\Contracts\UserRepositoryContract; |
21
|
|
|
use Rinvex\Fort\Http\Controllers\AuthorizedController; |
22
|
|
|
use Rinvex\Fort\Http\Requests\Backend\UserStoreRequest; |
23
|
|
|
use Rinvex\Fort\Http\Requests\Backend\UserUpdateRequest; |
24
|
|
|
|
25
|
|
|
class UsersController extends AuthorizedController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected $resource = 'users'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The user repository instance. |
34
|
|
|
* |
35
|
|
|
* @var \Rinvex\Fort\Contracts\UserRepositoryContract |
36
|
|
|
*/ |
37
|
|
|
protected $userRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new users controller instance. |
41
|
|
|
*/ |
42
|
|
|
public function __construct(UserRepositoryContract $userRepository) |
43
|
|
|
{ |
44
|
|
|
parent::__construct(); |
45
|
|
|
|
46
|
|
|
$this->userRepository = $userRepository; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Display a listing of the resource. |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
public function index() |
55
|
|
|
{ |
56
|
|
|
$users = $this->userRepository->paginate(config('rinvex.fort.backend.items_per_page')); |
57
|
|
|
|
58
|
|
|
return view('rinvex/fort::backend/users.index', compact('users')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Display the specified resource. |
63
|
|
|
* |
64
|
|
|
* @param \Rinvex\Fort\Models\User $user |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function show(User $user) |
69
|
|
|
{ |
70
|
|
|
$resources = app('rinvex.fort.ability')->findAll()->groupBy('resource'); |
71
|
|
|
$actions = ['list', 'view', 'create', 'update', 'delete', 'import', 'export']; |
72
|
|
|
$columns = ['resource', 'list', 'view', 'create', 'update', 'delete', 'import', 'export', 'other']; |
73
|
|
|
$userCountry = country($user->country); |
74
|
|
|
$country = ! empty($userCountry) ? $userCountry->getName().' '.$userCountry->getEmoji() : null; |
75
|
|
|
$phone = ! empty($userCountry) ? $userCountry->getCallingCode().$user->phone : null; |
76
|
|
|
|
77
|
|
|
return view('rinvex/fort::backend/users.show', compact('user', 'resources', 'actions', 'columns', 'country', 'phone')); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Show the form for creating a new resource. |
82
|
|
|
* |
83
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
84
|
|
|
*/ |
85
|
|
|
public function create() |
86
|
|
|
{ |
87
|
|
|
return $this->form('create', 'store', $this->userRepository->createModel()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Show the form for copying the given resource. |
92
|
|
|
* |
93
|
|
|
* @param \Rinvex\Fort\Models\User $user |
94
|
|
|
* |
95
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
public function copy(User $user) |
98
|
|
|
{ |
99
|
|
|
return $this->form('copy', 'store', $user); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Show the form for editing the given resource. |
104
|
|
|
* |
105
|
|
|
* @param \Rinvex\Fort\Models\User $user |
106
|
|
|
* |
107
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
108
|
|
|
*/ |
109
|
|
|
public function edit(User $user) |
110
|
|
|
{ |
111
|
|
|
return $this->form('edit', 'update', $user); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Store a newly created resource in storage. |
116
|
|
|
* |
117
|
|
|
* @param \Rinvex\Fort\Http\Requests\Backend\UserStoreRequest $request |
118
|
|
|
* |
119
|
|
|
* @return \Illuminate\Http\Response |
120
|
|
|
*/ |
121
|
|
|
public function store(UserStoreRequest $request) |
122
|
|
|
{ |
123
|
|
|
return $this->process($request); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Update the given resource in storage. |
128
|
|
|
* |
129
|
|
|
* @param \Rinvex\Fort\Http\Requests\Backend\UserUpdateRequest $request |
130
|
|
|
* @param \Rinvex\Fort\Models\User $user |
131
|
|
|
* |
132
|
|
|
* @return \Illuminate\Http\Response |
133
|
|
|
*/ |
134
|
|
|
public function update(UserUpdateRequest $request, User $user) |
135
|
|
|
{ |
136
|
|
|
return $this->process($request, $user); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Delete the given resource from storage. |
141
|
|
|
* |
142
|
|
|
* @param \Rinvex\Fort\Models\User $user |
143
|
|
|
* |
144
|
|
|
* @return \Illuminate\Http\Response |
145
|
|
|
*/ |
146
|
|
|
public function delete(User $user) |
147
|
|
|
{ |
148
|
|
|
$result = $this->userRepository->delete($user); |
149
|
|
|
|
150
|
|
|
return intend([ |
151
|
|
|
'route' => 'rinvex.fort.backend.users.index', |
152
|
|
|
'with' => ['rinvex.fort.alert.warning' => trans('rinvex/fort::backend/messages.user.deleted', ['userId' => $result->id])], |
|
|
|
|
153
|
|
|
]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Import the given resources into storage. |
158
|
|
|
* |
159
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
160
|
|
|
*/ |
161
|
|
|
public function import() |
162
|
|
|
{ |
163
|
|
|
// |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Export the given resources from storage. |
168
|
|
|
* |
169
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
170
|
|
|
*/ |
171
|
|
|
public function export() |
172
|
|
|
{ |
173
|
|
|
// |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Bulk control the given resources. |
178
|
|
|
* |
179
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
180
|
|
|
*/ |
181
|
|
|
public function bulk() |
182
|
|
|
{ |
183
|
|
|
// |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Show the form for create/edit/copy of the given resource. |
188
|
|
|
* |
189
|
|
|
* @param string $mode |
190
|
|
|
* @param string $action |
191
|
|
|
* @param \Rinvex\Fort\Models\User $user |
192
|
|
|
* |
193
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
194
|
|
|
*/ |
195
|
|
|
protected function form($mode, $action, User $user) |
196
|
|
|
{ |
197
|
|
|
$countries = array_map(function ($country) { |
198
|
|
|
return $country->getName(); |
199
|
|
|
}, Loader::countries()); |
200
|
|
|
|
201
|
|
|
$abilityList = app('rinvex.fort.ability')->findAll()->groupBy('resource')->map(function ($item) { |
202
|
|
|
return $item->pluck('title', 'id'); |
203
|
|
|
})->toArray(); |
204
|
|
|
|
205
|
|
|
$roleList = app('rinvex.fort.role')->findAll()->pluck('title', 'id')->toArray(); |
206
|
|
|
|
207
|
|
|
return view('rinvex/fort::backend/users.form', compact('user', 'abilityList', 'roleList', 'countries', 'mode', 'action')); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Process the form for store/update of the given resource. |
212
|
|
|
* |
213
|
|
|
* @param \Illuminate\Http\Request $request |
214
|
|
|
* @param \Rinvex\Fort\Models\User $user |
|
|
|
|
215
|
|
|
* |
216
|
|
|
* @return \Illuminate\Http\Response |
217
|
|
|
*/ |
218
|
|
|
protected function process(Request $request, User $user = null) |
219
|
|
|
{ |
220
|
|
|
// Prepare required input fields |
221
|
|
|
$input = $request->except(['_method', '_token', 'id']); |
222
|
|
|
$roles = $request->user($this->getGuard())->can('assign-roles') ? ['roles' => array_pull($input, 'roleList')] : []; |
|
|
|
|
223
|
|
|
$abilities = $request->user($this->getGuard())->can('grant-abilities') ? ['abilities' => array_pull($input, 'abilityList')] : []; |
|
|
|
|
224
|
|
|
|
225
|
|
|
// Store data into the entity |
226
|
|
|
$result = $this->userRepository->store($user, $input + $roles + $abilities); |
227
|
|
|
|
228
|
|
|
// Repository `store` method returns false if no attributes |
229
|
|
|
// updated, happens save button clicked without chaning anything |
230
|
|
|
$message = ! is_null($user) |
231
|
|
|
? ($result === false |
232
|
|
|
? ['rinvex.fort.alert.warning' => trans('rinvex/fort::backend/messages.user.nothing_updated', ['userId' => $user->id])] |
|
|
|
|
233
|
|
|
: ['rinvex.fort.alert.success' => trans('rinvex/fort::backend/messages.user.updated', ['userId' => $result->id])]) |
|
|
|
|
234
|
|
|
: ['rinvex.fort.alert.success' => trans('rinvex/fort::backend/messages.user.created', ['userId' => $result->id])]; |
|
|
|
|
235
|
|
|
|
236
|
|
|
return intend([ |
237
|
|
|
'route' => 'rinvex.fort.backend.users.index', |
238
|
|
|
'with' => $message, |
239
|
|
|
]); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
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.