1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Twig\Extension; |
6
|
|
|
|
7
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfiguration; |
8
|
|
|
use LAG\AdminBundle\Security\Helper\SecurityHelper; |
9
|
|
|
use Twig\Extension\AbstractExtension; |
10
|
|
|
use Twig\TwigFunction; |
11
|
|
|
|
12
|
|
|
class AdminExtension extends AbstractExtension |
13
|
|
|
{ |
14
|
|
|
private bool $mediaEnabled; |
15
|
|
|
private ApplicationConfiguration $appConfig; |
16
|
|
|
private SecurityHelper $security; |
17
|
|
|
|
18
|
5 |
|
public function __construct( |
19
|
|
|
bool $mediaEnabled, |
20
|
|
|
ApplicationConfiguration $appConfig, |
21
|
|
|
SecurityHelper $security |
22
|
|
|
) { |
23
|
5 |
|
$this->appConfig = $appConfig; |
24
|
5 |
|
$this->security = $security; |
25
|
5 |
|
$this->mediaEnabled = $mediaEnabled; |
26
|
5 |
|
} |
27
|
|
|
|
28
|
1 |
|
public function getFunctions(): array |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
1 |
|
new TwigFunction('admin_config', [$this, 'getApplicationParameter']), |
32
|
1 |
|
new TwigFunction('admin_action_allowed', [$this, 'isAdminActionAllowed']), |
33
|
1 |
|
new TwigFunction('admin_media_enabled', [$this, 'isMediaBundleEnabled']), |
34
|
1 |
|
new TwigFunction('admin_is_translation_enabled', [$this, 'isTranslationEnabled']), |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function getApplicationParameter($name) |
39
|
|
|
{ |
40
|
|
|
return $this |
41
|
1 |
|
->appConfig |
42
|
1 |
|
->get($name) |
43
|
|
|
; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return true if the given action is allowed for the given Admin. |
48
|
|
|
*/ |
49
|
1 |
|
public function isAdminActionAllowed(string $adminName, string $actionName): bool |
50
|
|
|
{ |
51
|
1 |
|
return $this->security->isActionAllowed($adminName, $actionName); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return true if the media bundle is enabled. |
56
|
|
|
*/ |
57
|
1 |
|
public function isMediaBundleEnabled(): bool |
58
|
|
|
{ |
59
|
1 |
|
return $this->mediaEnabled; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isTranslationEnabled(): bool |
63
|
|
|
{ |
64
|
|
|
return $this->appConfig->isTranslationEnabled(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|