|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use App\Association; |
|
7
|
|
|
use App\Club; |
|
8
|
|
|
use App\Country; |
|
9
|
|
|
use App\Federation; |
|
10
|
|
|
use App\Grade; |
|
11
|
|
|
use App\Http\Requests\UserRequest; |
|
12
|
|
|
use App\Notifications\AccountCreated; |
|
13
|
|
|
use App\Role; |
|
14
|
|
|
use App\User; |
|
15
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
|
16
|
|
|
use Illuminate\Database\QueryException; |
|
17
|
|
|
use Illuminate\Http\Request; |
|
18
|
|
|
use Illuminate\Support\Facades\Auth; |
|
19
|
|
|
use Illuminate\Support\Facades\Lang; |
|
20
|
|
|
use Illuminate\Support\Facades\Response; |
|
21
|
|
|
use Illuminate\Support\Facades\View; |
|
22
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
|
23
|
|
|
use URL; |
|
24
|
|
|
|
|
25
|
|
|
class UserController extends Controller |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Display a listing of the resource. |
|
29
|
|
|
* |
|
30
|
|
|
* @return View |
|
31
|
|
|
*/ |
|
32
|
|
|
public function index() |
|
33
|
|
|
{ |
|
34
|
|
|
$users = User::with('country', 'role', 'association', 'federation') |
|
35
|
|
|
->forUser(Auth::user()) |
|
36
|
|
|
->where('id', '>', 1) |
|
37
|
|
|
->paginate(config('constants.PAGINATION')); |
|
38
|
|
|
|
|
39
|
|
|
return view('users.index', compact('users')); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Show the form for creating a new resource. |
|
44
|
|
|
* @return View |
|
45
|
|
|
* @throws AuthorizationException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function create() |
|
48
|
|
|
{ |
|
49
|
|
|
|
|
50
|
|
|
$user = new User(); |
|
51
|
|
|
if (Auth::user()->cannot('create', $user)) { |
|
|
|
|
|
|
52
|
|
|
throw new AuthorizationException(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$roles = Role::grantedRoles(Auth::user()->role_id)->pluck('name', 'id'); |
|
|
|
|
|
|
56
|
|
|
$grades = Grade::getAllPlucked(); |
|
57
|
|
|
$countries = Country::getAllPlucked(); |
|
58
|
|
|
$federations = Federation::fillSelect(); |
|
59
|
|
|
$associations = Association::forUser(auth()->user()); |
|
|
|
|
|
|
60
|
|
|
$clubs = Club::fillSelect(Auth::user()->federation_id, Auth::user()->association_id); |
|
|
|
|
|
|
61
|
|
|
return view('users.form', compact('user', 'grades', 'countries', 'roles', 'federations', 'associations', 'clubs')); // |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Store a newly created resource in storage. |
|
66
|
|
|
* |
|
67
|
|
|
* @param UserRequest $userForm |
|
68
|
|
|
* @return \Illuminate\Http\RedirectResponse|Response |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function store(UserRequest $userForm) |
|
71
|
|
|
{ |
|
72
|
|
|
try { |
|
73
|
1 |
|
$userForm->store(); |
|
74
|
1 |
|
$user = User::where('email', $userForm->email)->first(); |
|
|
|
|
|
|
75
|
1 |
|
$user->notify(new AccountCreated($user)); |
|
76
|
1 |
|
flash()->success(trans('msg.user_create_successful')); |
|
|
|
|
|
|
77
|
|
|
} catch (QueryException $e) { |
|
78
|
|
|
flash()->error(trans('msg.user_already_exists')); |
|
|
|
|
|
|
79
|
|
|
return redirect()->back()->withInput(); |
|
80
|
|
|
} |
|
81
|
1 |
|
return redirect(route('users.index')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Display the specified resource. |
|
86
|
|
|
* |
|
87
|
|
|
* @param User $user |
|
88
|
|
|
* @return View |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function show(User $user) |
|
91
|
|
|
{ |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
1 |
|
return view('users.show', compact('user')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Show the form for editing the specified resource. |
|
99
|
|
|
* |
|
100
|
|
|
* @param User $user |
|
101
|
|
|
* @return View |
|
102
|
|
|
* @throws AuthorizationException |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function edit(User $user) |
|
105
|
|
|
{ |
|
106
|
2 |
|
if (Auth::user()->cannot('edit', $user)) { |
|
|
|
|
|
|
107
|
2 |
|
throw new AuthorizationException(); |
|
108
|
|
|
} |
|
109
|
1 |
|
$roles = Role::grantedRoles(Auth::user()->role_id)->pluck('name', 'id'); |
|
|
|
|
|
|
110
|
1 |
|
$grades = Grade::orderBy('order')->pluck('name', 'id'); |
|
111
|
1 |
|
$countries = Country::pluck('name', 'id'); |
|
112
|
1 |
|
$federations = Federation::fillSelect(); |
|
113
|
1 |
|
$associations = Association::forUser(auth()->user()); |
|
|
|
|
|
|
114
|
1 |
|
$clubs = Club::fillSelect(Auth::user()->federation_id, Auth::user()->association_id); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
1 |
|
return view('users.form', compact('user', 'grades', 'countries', 'roles', 'federations', 'associations', 'clubs')); // |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Update the specified resource in storage. |
|
121
|
|
|
* |
|
122
|
|
|
* @param UserRequest $userForm |
|
123
|
|
|
* @param User $user |
|
124
|
|
|
* @return Response |
|
125
|
|
|
*/ |
|
126
|
4 |
|
public function update(UserRequest $userForm, User $user) |
|
127
|
|
|
{ |
|
128
|
4 |
|
if ($userForm->update($user)) { |
|
129
|
4 |
|
flash()->success(trans('msg.user_update_successful')); |
|
|
|
|
|
|
130
|
|
|
} else |
|
131
|
|
|
flash()->error(Lang::get('msg.user_update_error')); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
4 |
|
if ($user->id == Auth::user()->id) { |
|
|
|
|
|
|
134
|
3 |
|
return redirect(URL::action('UserController@edit', Auth::user()->slug)); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
return redirect(route('users.index')); |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
public function export() |
|
143
|
|
|
{ |
|
144
|
|
|
|
|
145
|
|
|
Excel::create(trans_choice('core.user', 2), function ($excel) { |
|
|
|
|
|
|
146
|
|
|
$appName = (app()->environment() == 'local' ? getenv('APP_NAME') : config('app.name')); |
|
147
|
|
|
|
|
148
|
|
|
// Set the title |
|
149
|
|
|
$excel->setTitle(trans_choice('core.user', 2)); |
|
150
|
|
|
|
|
151
|
|
|
// Chain the setters |
|
152
|
|
|
$excel->setCreator($appName) |
|
153
|
|
|
->setCompany($appName); |
|
154
|
|
|
|
|
155
|
|
|
// Call them separately |
|
156
|
|
|
$excel->setDescription('A list of users'); |
|
157
|
|
|
$excel->sheet(trans_choice('core.user', 2), function ($sheet) { |
|
158
|
|
|
//TODO Here we should join grade, role, country to print name not FK |
|
159
|
|
|
$users = User::all(); |
|
160
|
|
|
// $users = User::with(['grade', 'role'])->get(); |
|
161
|
|
|
$sheet->fromArray($users); |
|
162
|
|
|
}); |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
})->export('xls'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
public function getMyTournaments(Request $request) |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
1 |
|
$tournaments = Auth::user()->myTournaments()->with('owner') |
|
|
|
|
|
|
171
|
1 |
|
->orderBy('created_at', 'desc') |
|
172
|
1 |
|
->paginate(config('constants.PAGINATION'));; |
|
173
|
|
|
|
|
174
|
1 |
|
$title = trans('core.tournaments_registered'); |
|
175
|
|
|
|
|
176
|
1 |
|
return view('users.tournaments', compact('tournaments', 'title')); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Remove the specified resource from storage. |
|
181
|
|
|
* |
|
182
|
|
|
* @param User $user |
|
183
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
184
|
|
|
* @throws \Exception |
|
185
|
|
|
*/ |
|
186
|
1 |
|
public function destroy(User $user) |
|
187
|
|
|
{ |
|
188
|
|
|
|
|
189
|
1 |
|
if ($user->delete()) { |
|
190
|
1 |
|
return Response::json(['msg' => trans('msg.user_delete_successful'), 'status' => 'success']); |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
return Response::json(['msg' => trans('msg.user_delete_error'), 'status' => 'error']); |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param $userSlug |
|
198
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
199
|
|
|
*/ |
|
200
|
|
|
public function restore($userSlug) |
|
201
|
|
|
|
|
202
|
|
|
{ |
|
203
|
|
|
$user = User::withTrashed()->whereSlug($userSlug)->first(); |
|
204
|
|
|
if ($user->restore()) { |
|
205
|
|
|
return Response::json(['msg' => trans('msg.user_restore_successful'), 'status' => 'success']); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
return Response::json(['msg' => trans('msg.user_restore_successful'), 'status' => 'error']); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function myFederations(User $user) |
|
212
|
|
|
{ |
|
213
|
|
|
return Federation::fillSelectForVueJs($user); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function myAssociations(User $user, $federationId) |
|
217
|
|
|
{ |
|
218
|
|
|
return Association::fillSelectForVuejs($user, $federationId); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function myClubs(User $user, $federationId, $associationId) |
|
222
|
|
|
{ |
|
223
|
|
|
return Club::fillSelectForVueJs($user, $federationId, $associationId); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: