Completed
Push — master ( 08e3df...e0592e )
by wen
03:47
created

Authorize::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Auth\Access\AuthorizationException;
7
use Route;
8
use Illuminate\Contracts\Auth\Factory as Auth;
9
10
class Authorize
11
{
12
    const DELIMITER = '|';
13
14
    protected $auth;
15
16
    /**
17
     * Create a new middleware instance.
18
     *
19
     * @param  \Illuminate\Contracts\Auth\Factory $auth
20
     */
21
    public function __construct(Auth $auth)
22
    {
23
        $this->auth = $auth;
24
    }
25
26
    /**
27
     * @param \Illuminate\Http\Request $request
28
     * @param Closure                  $next
29
     * @param string                   $permissions
30
     *
31
     * @return mixed
32
     * @throws \Illuminate\Auth\Access\AuthorizationException
33
     */
34
    public function handle($request, Closure $next, $permissions = '')
35
    {
36
        if (empty($permissions)) {
37
            $permissions = [Route::currentRouteName()];
38
        }
39
40
        if (!is_array($permissions)) {
41
            $permissions = explode(self::DELIMITER, $permissions);
42
        }
43
44
        if ($this->auth->guest() || !$request->user()->can($permissions)) {
0 ignored issues
show
Bug introduced by
The method guest() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            throw new AuthorizationException();
46
        }
47
48
        return $next($request);
49
    }
50
}
51