1 | <?php |
||||
2 | |||||
3 | namespace App\Http\Middleware; |
||||
4 | |||||
5 | use Closure; |
||||
6 | use Illuminate\Http\Request; |
||||
7 | |||||
8 | class CheckIfAdmin |
||||
9 | { |
||||
10 | /** |
||||
11 | * Checked that the logged in user is an administrator. |
||||
12 | * |
||||
13 | * -------------- |
||||
14 | * VERY IMPORTANT |
||||
15 | * -------------- |
||||
16 | * If you have both regular users and admins inside the same table, |
||||
17 | * change the contents of this method to check that the logged in user |
||||
18 | * is an admin, and not a regular user. |
||||
19 | * |
||||
20 | * @param [type] $user [description] |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
21 | * |
||||
22 | * @return bool [description] |
||||
23 | */ |
||||
24 | private function checkIfUserIsAdmin($user) |
||||
0 ignored issues
–
show
The parameter
$user is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
25 | { |
||||
26 | // return ($user->is_admin == 1); |
||||
27 | return true; |
||||
28 | } |
||||
29 | |||||
30 | /** |
||||
31 | * Answer to unauthorized access request. |
||||
32 | * |
||||
33 | * @param [type] $request [description] |
||||
0 ignored issues
–
show
|
|||||
34 | * |
||||
35 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
36 | */ |
||||
37 | private function respondToUnauthorizedRequest($request) |
||||
38 | { |
||||
39 | if ($request->ajax() || $request->wantsJson()) { |
||||
40 | return response(trans('backpack::base.unauthorized'), 401); |
||||
41 | } else { |
||||
42 | return redirect()->guest(backpack_url('login')); |
||||
43 | } |
||||
44 | } |
||||
45 | |||||
46 | /** |
||||
47 | * Handle an incoming request. |
||||
48 | * |
||||
49 | * @param Request $request |
||||
50 | * |
||||
51 | * @return mixed |
||||
52 | */ |
||||
53 | public function handle($request, Closure $next) |
||||
54 | { |
||||
55 | if (backpack_auth()->guest()) { |
||||
56 | return $this->respondToUnauthorizedRequest($request); |
||||
57 | } |
||||
58 | |||||
59 | if (! $this->checkIfUserIsAdmin(backpack_user())) { |
||||
60 | return $this->respondToUnauthorizedRequest($request); |
||||
61 | } |
||||
62 | |||||
63 | return $next($request); |
||||
64 | } |
||||
65 | } |
||||
66 |