Completed
Push — master ( 56cad9...ac5209 )
by Cristian
01:53
created

CheckIfAdmin::checkIfUserIsAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\Base\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 introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
20
     *
21
     * @return bool [description]
22
     */
23
    private function checkIfUserIsAdmin($user)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        // return ($user->is_admin == 1);
26
        return true;
27
    }
28
29
    /**
30
     * Answer to unauthorized access request.
31
     *
32
     * @param [type] $request [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
33
     *
34
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
35
     */
36
    private function respondToUnauthorizedRequest($request)
37
    {
38
        if ($request->ajax() || $request->wantsJson()) {
39
            return response(trans('backpack::base.unauthorized'), 401);
40
        } else {
41
            return redirect()->guest(backpack_url('login'));
0 ignored issues
show
Bug introduced by
It seems like backpack_url('login') targeting backpack_url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Illuminate\Routing\Redirector::guest() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
        }
43
    }
44
45
    /**
46
     * Handle an incoming request.
47
     *
48
     * @param \Illuminate\Http\Request $request
49
     * @param \Closure                 $next
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