|
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
|
|
|
public function __construct( |
|
24
|
|
|
array $menuConfigurations, |
|
25
|
|
|
MenuFactoryInterface $menuFactory, |
|
26
|
|
|
ConfigurationFactoryInterface $configurationFactory, |
|
27
|
2 |
|
Security $security |
|
28
|
|
|
) { |
|
29
|
|
|
$this->configurationFactory = $configurationFactory; |
|
30
|
|
|
$this->menuConfigurations = $menuConfigurations; |
|
31
|
|
|
$this->security = $security; |
|
32
|
|
|
$this->menuFactory = $menuFactory; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
public function get(string $name, array $options = []): ItemInterface |
|
36
|
2 |
|
{ |
|
37
|
2 |
|
return $this->menuFactory->create($name, $options); |
|
38
|
2 |
|
} |
|
39
|
2 |
|
|
|
40
|
2 |
|
public function has(string $name, array $options = []): bool |
|
41
|
2 |
|
{ |
|
42
|
|
|
if (!\array_key_exists($name, $this->menuConfigurations)) { |
|
43
|
1 |
|
return false; |
|
44
|
|
|
} |
|
45
|
1 |
|
$configuration = $this->configurationFactory->createMenuConfiguration($name, $options); |
|
46
|
1 |
|
|
|
47
|
1 |
|
if (!$configuration->hasPermissions()) { |
|
48
|
1 |
|
return true; |
|
49
|
|
|
} |
|
50
|
1 |
|
|
|
51
|
|
|
foreach ($configuration->getPermissions() as $permission) { |
|
52
|
1 |
|
if (!$this->security->isGranted($permission, $this->security->getUser())) { |
|
53
|
1 |
|
return false; |
|
54
|
1 |
|
} |
|
55
|
|
|
} |
|
56
|
1 |
|
|
|
57
|
1 |
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|