Completed
Push — 3.0 ( dd376e...c11e1c )
by Ian
35:16
created

PermissionMiddleware::handle()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 29
ccs 0
cts 21
cp 0
rs 8.439
cc 6
eloc 14
nc 13
nop 3
crap 42
1
<?php
2
3
/**
4
 * @package   Dashboard
5
 * @author    Ian Olson <[email protected]>
6
 * @license   MIT
7
 * @copyright 2015, Laraflock
8
 * @link      https://github.com/laraflock
9
 */
10
11
namespace Laraflock\Dashboard\Middleware;
12
13
use Closure;
14
use Illuminate\Http\Request;
15
use Laracasts\Flash\Flash;
16
use Laraflock\Dashboard\Repositories\Auth\AuthRepositoryInterface as Auth;
17
18
class PermissionMiddleware
19
{
20
    /**
21
     * Auth interface.
22
     *
23
     * @var Auth
24
     */
25
    protected $auth;
26
27
    /**
28
     * The constructor.
29
     *
30
     * @param Auth $auth
31
     */
32
    public function __construct(Auth $auth)
33
    {
34
        $this->auth = $auth;
35
    }
36
37
    /**
38
     * Check if user has permission.
39
     *
40
     * @param Request      $request
41
     * @param Closure      $next
42
     * @param string|array $permissions
43
     *
44
     * @return \Illuminate\Http\RedirectResponse
45
     */
46
    public function handle(Request $request, Closure $next, $permissions)
47
    {
48
        $accessDenied = true;
49
50
        if (!$user = $this->auth->getActiveUser()) {
51
            Flash::error(trans('dashboard::dashboard.flash.access_denied'));
52
53
            return redirect()->back();
54
        }
55
56
        if (!is_array($permissions)) {
57
            $permissions = [$permissions];
58
        }
59
60
        foreach ($permissions as $permission) {
61
62
            if ($user->hasAccess($permission)) {
63
                $accessDenied = false;
64
            }
65
        }
66
67
        if ($accessDenied) {
68
            Flash::error(trans('dashboard::dashboard.flash.access_denied'));
69
70
            return redirect()->back();
71
        }
72
73
        return $next($request);
74
    }
75
}