|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\PermissionsModule\Twig\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
class PermissionsExtension extends \Twig_Extension |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ContainerInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $container; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(ContainerInterface $container = null) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->container = $container; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return ContainerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getContainer() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->container; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns a list of functions to add to the existing list. |
|
38
|
|
|
* |
|
39
|
|
|
* @return array An array of functions |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getFunctions() |
|
42
|
|
|
{ |
|
43
|
|
|
$functions = [ |
|
44
|
|
|
new \Twig_SimpleFunction('hasPermission', [$this, 'hasPermission']), |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
return $functions; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $component |
|
52
|
|
|
* @param string $instance |
|
53
|
|
|
* @param string $level |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function hasPermission($component, $instance, $level) |
|
57
|
|
|
{ |
|
58
|
|
|
if (empty($component) || empty($instance) || empty($level)) { |
|
59
|
|
|
$translator = $this->container->get('translator.default'); |
|
60
|
|
|
throw new \InvalidArgumentException($translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$result = $this->container->get('zikula_permissions_module.api.permission')->hasPermission($component, $instance, constant($level)); |
|
64
|
|
|
|
|
65
|
|
|
return (bool) $result; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|