AuthorizeModuleAccess::handle()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 3
Ratio 21.43 %

Importance

Changes 0
Metric Value
dl 3
loc 14
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 7
nc 3
nop 2
1
<?php namespace Anomaly\UsersModule\Http\Middleware;
2
3
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection;
4
use Anomaly\Streams\Platform\Support\Authorizer;
5
use Closure;
6
use Illuminate\Http\Request;
7
8
/**
9
 * Class AuthorizeModuleAccess
10
 *
11
 * @link          http://pyrocms.com/
12
 * @author        PyroCMS, Inc. <[email protected]>
13
 * @author        Ryan Thompson <[email protected]>
14
 */
15
class AuthorizeModuleAccess
16
{
17
18
    /**
19
     * The module collection.
20
     *
21
     * @var ModuleCollection
22
     */
23
    protected $modules;
24
25
    /**
26
     * The authorizer utility.
27
     *
28
     * @var Authorizer
29
     */
30
    protected $authorizer;
31
32
    /**
33
     * Create a new AuthorizeModuleAccess instance.
34
     *
35
     * @param ModuleCollection $modules
36
     * @param Authorizer       $authorizer
37
     */
38
    public function __construct(ModuleCollection $modules, Authorizer $authorizer)
39
    {
40
        $this->modules    = $modules;
41
        $this->authorizer = $authorizer;
42
    }
43
44
    /**
45
     * Check the authorization of module access.
46
     *
47
     * @param  Request  $request
48
     * @param  \Closure $next
49
     * @return mixed
50
     */
51
    public function handle(Request $request, Closure $next)
52
    {
53 View Code Duplication
        if ($request->segment(1) !== 'admin' || in_array($request->path(), ['admin/login', 'admin/logout'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            return $next($request);
55
        }
56
57
        $module = $this->modules->active();
58
59
        if ($module && !$this->authorizer->authorize($module->getNamespace('*'))) {
60
            abort(403);
61
        }
62
63
        return $next($request);
64
    }
65
}
66