SecurityExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 69
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFunctions() 0 6 1
B isOneGranted() 0 22 6
A getName() 0 4 1
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
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead

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.

Loading history...
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')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

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.

Loading history...
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