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