Issues (404)

Branch: dev

app/Http/Middleware/CheckIfAdmin.php (9 issues)

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
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
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
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
32
     *
33
     * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
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 ignore-call  annotation

38
            return response(/** @scrutinizer ignore-call */ trans('backpack::base.unauthorized'), 401);
Loading history...
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 ignore-call  annotation

38
            return /** @scrutinizer ignore-call */ response(trans('backpack::base.unauthorized'), 401);
Loading history...
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 ignore-call  annotation

40
            return redirect()->guest(/** @scrutinizer ignore-call */ backpack_url('login'));
Loading history...
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 ignore-call  annotation

40
            return /** @scrutinizer ignore-call */ redirect()->guest(backpack_url('login'));
Loading history...
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 ignore-call  annotation

54
        if (/** @scrutinizer ignore-call */ backpack_auth()->guest()) {
Loading history...
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 ignore-call  annotation

58
        if (!$this->checkIfUserIsAdmin(/** @scrutinizer ignore-call */ backpack_user())) {
Loading history...
59 1
            return $this->respondToUnauthorizedRequest($request);
60
        }
61
62 5
        return $next($request);
63
    }
64
}
65