Passed
Pull Request — master (#1259)
by Diego
07:12 queued 02:18
created

GateFilter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 18
c 3
b 0
f 0
dl 0
loc 85
ccs 20
cts 20
cp 1
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isAllowed() 0 19 5
A transform() 0 13 4
A __construct() 0 3 1
A allItemsRestricted() 0 11 3
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu\Filters;
4
5
use Illuminate\Contracts\Auth\Access\Gate;
6
use JeroenNoten\LaravelAdminLte\Helpers\MenuItemHelper;
7
8
class GateFilter implements FilterInterface
9
{
10
    /**
11
     * The Laravel gate instance, used to check for permissions.
12
     *
13
     * @var Gate
14
     */
15
    protected $gate;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param  Gate  $gate
21
     */
22 53
    public function __construct(Gate $gate)
23
    {
24 53
        $this->gate = $gate;
25
    }
26
27
    /**
28
     * Transforms a menu item. Add the restricted property to a menu item
29
     * when situable.
30
     *
31
     * @param  array  $item  A menu item
32
     * @return array The transformed menu item
33
     */
34 53
    public function transform($item)
35
    {
36
        // Set a special attribute when item is not allowed. Items with this
37
        // attribute will be filtered out of the menu.
38
39 53
        $isWholeRestrictedSubmenu = MenuItemHelper::isSubmenu($item)
40 53
            && $this->allItemsRestricted($item['submenu']);
41
42 53
        if (! $this->isAllowed($item) || $isWholeRestrictedSubmenu) {
43 7
            $item['restricted'] = true;
44
        }
45
46 53
        return $item;
47
    }
48
49
    /**
50
     * Check if a menu item is allowed for the current user.
51
     *
52
     * @param  array  $item  A menu item
53
     * @return bool
54
     */
55 53
    protected function isAllowed($item)
56
    {
57
        // Check if there are any permission defined for the item.
58
59 53
        if (empty($item['can'])) {
60 49
            return true;
61
        }
62
63
        // Read the extra arguments (a db model instance can be used).
64
65 8
        $args = isset($item['model']) ? $item['model'] : [];
66
67
        // Check if the current user can perform the configured permissions.
68
69 8
        if (is_string($item['can']) || is_array($item['can'])) {
70 7
            return $this->gate->any($item['can'], $args);
71
        }
72
73 1
        return true;
74
    }
75
76
    /**
77
     * Check if a set of items are all restricted (or unallowed).
78
     *
79
     * @param  array  $items  An array with the menu items to check
80
     * @return bool
81
     */
82 18
    protected function allItemsRestricted($items)
83
    {
84
        // Check if every provided item is restricted.
85
86 18
        foreach ($items as $item) {
87 17
            if ($this->isAllowed($item)) {
88 17
                return false;
89
            }
90
        }
91
92 1
        return true;
93
    }
94
}
95