Cancelled
Push — test-branch ( 5c534a )
by Yonathan
06:17 queued 06:17
created

CheckIfAdmin::respondToUnauthorizedRequest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
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]
2 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...
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
20
     *
21
     * @return bool [description]
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
22
     */
23
    private function checkIfUserIsAdmin($user)
1 ignored issue
show
introduced by
Method \App\Http\Middleware\CheckIfAdmin::checkIfUserIsAdmin() does not have parameter type hint for its parameter $user but it should be possible to add it based on @param annotation "[type]".
Loading history...
introduced by
Method \App\Http\Middleware\CheckIfAdmin::checkIfUserIsAdmin() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
Coding Style introduced by
Type hint "[type]" missing for $user
Loading history...
24
    {
25
        return ($user->hasRole('admin'));
26
    }
27
28
    /**
29
     * Answer to unauthorized access request.
30
     *
31
     * @param [type] $request [description]
2 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...
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
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
    private function respondToUnauthorizedRequest($request)
1 ignored issue
show
introduced by
Method \App\Http\Middleware\CheckIfAdmin::respondToUnauthorizedRequest() does not have parameter type hint for its parameter $request but it should be possible to add it based on @param annotation "[type]".
Loading history...
introduced by
Method \App\Http\Middleware\CheckIfAdmin::respondToUnauthorizedRequest() does not have return type hint for its return value but it should be possible to add it based on @return annotation "[type]".
Loading history...
Coding Style introduced by
Type hint "[type]" missing for $request
Loading history...
36
    {
37
        if ($request->ajax() || $request->wantsJson()) {
38
            return response(trans('backpack::base.unauthorized'), 401);
39
        } else {
40
            return redirect()->guest(backpack_url('login'));
41
        }
42
    }
43
44
    /**
45
     * Handle an incoming request.
46
     *
47
     * @param \Illuminate\Http\Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
48
     * @param \Closure                 $next
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
49
     *
50
     * @return mixed
51
     */
52
    public function handle($request, Closure $next)
1 ignored issue
show
introduced by
Method \App\Http\Middleware\CheckIfAdmin::handle() does not have parameter type hint for its parameter $request but it should be possible to add it based on @param annotation "\Illuminate\Http\Request".
Loading history...
Coding Style introduced by
Type hint "\Illuminate\Http\Request" missing for $request
Loading history...
53
    {
54
        if (backpack_auth()->guest()) {
55
            return $this->respondToUnauthorizedRequest($request);
56
        }
57
58
        if (!$this->checkIfUserIsAdmin(backpack_user())) {
59
            return $this->respondToUnauthorizedRequest($request);
60
        }
61
62
        return $next($request);
63
    }
64
}
65