1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Menu\Provider; |
6
|
|
|
|
7
|
|
|
use Knp\Menu\ItemInterface; |
8
|
|
|
use Knp\Menu\Provider\MenuProviderInterface; |
9
|
|
|
use LAG\AdminBundle\Factory\Configuration\ConfigurationFactoryInterface; |
10
|
|
|
use LAG\AdminBundle\Menu\Factory\MenuFactoryInterface; |
11
|
|
|
use Symfony\Component\Security\Core\Security; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create a new KNP menu using the MenuConfiguration class to validate provided options. |
15
|
|
|
*/ |
16
|
|
|
class MenuProvider implements MenuProviderInterface |
17
|
|
|
{ |
18
|
|
|
private array $menuConfigurations; |
19
|
|
|
private MenuFactoryInterface $menuFactory; |
20
|
|
|
private ConfigurationFactoryInterface $configurationFactory; |
21
|
|
|
private Security $security; |
22
|
|
|
|
23
|
1 |
|
public function __construct( |
24
|
|
|
array $menuConfigurations, |
25
|
|
|
MenuFactoryInterface $menuFactory, |
26
|
|
|
ConfigurationFactoryInterface $configurationFactory, |
27
|
|
|
Security $security |
28
|
|
|
) { |
29
|
1 |
|
$this->configurationFactory = $configurationFactory; |
30
|
1 |
|
$this->menuConfigurations = $menuConfigurations; |
31
|
1 |
|
$this->security = $security; |
32
|
1 |
|
$this->menuFactory = $menuFactory; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
1 |
|
public function get(string $name, array $options = []): ItemInterface |
36
|
|
|
{ |
37
|
1 |
|
return $this->menuFactory->create($name, $options); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function has(string $name, array $options = []): bool |
41
|
|
|
{ |
42
|
|
|
if (!\array_key_exists($name, $this->menuConfigurations)) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
$configuration = $this->configurationFactory->createMenuConfiguration($name, $options); |
46
|
|
|
|
47
|
|
|
if (!$configuration->hasPermissions()) { |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($configuration->getPermissions() as $permission) { |
52
|
|
|
if (!$this->security->isGranted($permission, $this->security->getUser())) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|