|
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
|
|
|
|