Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

Permission::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Http\Middleware;
13
14
use Closure;
15
use Illuminate\Contracts\Auth\Guard;
16
use Illuminate\Http\Request;
17
use Tinyissue\Model\Project as ProjectModel;
18
19
/**
20
 * Permission is a Middleware class to for checking if current user has the permission to access the request.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 */
24
class Permission
25
{
26
    /**
27
     * The Guard implementation.
28
     *
29
     * @var Guard
30
     */
31
    protected $auth;
32
33
    /**
34
     * List of permissions that can be accessed by public users.
35
     *
36
     * @var array
37
     */
38
    protected $publicAccess = [
39
        'issue-view',
40
    ];
41
42
    /**
43
     * Create a new filter instance.
44
     *
45
     * @param Guard $auth
46
     */
47 43
    public function __construct(Guard $auth)
48
    {
49 43
        $this->auth = $auth;
50 43
    }
51
52
    /**
53
     * Handle an incoming request.
54
     *
55
     * @param Request  $request
56
     * @param \Closure $next
57
     *
58
     * @return mixed
59
     */
60 42
    public function handle(Request $request, Closure $next)
61
    {
62 42
        $permission = $this->getPermission($request);
63 42
        $user       = $this->auth->user();
64
        /** @var ProjectModel|null $project */
65 42
        $project = $request->route()->getParameter('project');
66
67
        // Check if user has the permission
68
        // & if the user can access the current context (e.g. is one of the project users)
69 42
        if (app('tinyissue.settings')->isPublicProjectsEnabled()
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
70 42
            && in_array($permission, $this->publicAccess)
71 42
            && $project instanceof ProjectModel && !$project->isPrivate()) {
72
            // Ignore we are ok to view issues in public project
73 42
        } elseif (!$this->auth->guest()
74 42
            && (!$user->permission($permission) || !$user->permissionInContext($request->route()->parameters()))) {
75 8
            abort(401);
76
        }
77
78 38
        return $next($request);
79
    }
80
81
    /**
82
     * Returns the permission defined in route action.
83
     *
84
     * @param Request $request
85
     *
86
     * @return mixed
87
     */
88 42
    protected function getPermission(Request $request)
89
    {
90 42
        $actions = $request->route()->getAction();
91
92 42
        return $actions['permission'];
93
    }
94
}
95