1 | <?php |
||
25 | class UserController extends Controller |
||
26 | { |
||
27 | /** |
||
28 | * Display a listing of the resource. |
||
29 | * |
||
30 | * @return View |
||
31 | */ |
||
32 | public function index() |
||
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) |
|
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) |
|
140 | |||
141 | |||
142 | public function export() |
||
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: