|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Admingenerator\GeneratorBundle\Twig\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @author Stéphane Escandell <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class SecurityExtension extends \Twig_Extension |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var AuthorizationCheckerInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $authorizationChecker; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var bool |
|
19
|
|
|
*/ |
|
20
|
|
|
private $useExpression; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param AuthorizationCheckerInterface $authorizationChecker |
|
24
|
|
|
* @param bool $useExpression |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(AuthorizationCheckerInterface $authorizationChecker, $useExpression = false) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->authorizationChecker = $authorizationChecker; |
|
29
|
|
|
$this->useExpression = $useExpression; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getFunctions() |
|
36
|
|
|
{ |
|
37
|
|
|
return array( |
|
38
|
|
|
'is_one_admingenerator_granted' => new \Twig_SimpleFunction('is_one_admingenerator_granted', array($this, 'isOneGranted')), |
|
|
|
|
|
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $credentials |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function isOneGranted(array $credentials, $object = null) |
|
47
|
|
|
{ |
|
48
|
|
|
if (empty($credentials)) { |
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
foreach ($credentials as $credential) { |
|
53
|
|
|
if ('AdmingenAllowed' == $credential) { |
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($this->useExpression) { |
|
58
|
|
|
$credential = new \JMS\SecurityExtraBundle\Security\Authorization\Expression\Expression($credential); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($this->authorizationChecker->isGranted($credential, $object)) { |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the name of the extension. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string The extension name |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getName() |
|
75
|
|
|
{ |
|
76
|
|
|
return 'admingenerator_security'; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.