|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\SecurityExtraBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
8
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
9
|
|
|
use Yokai\SecurityExtraBundle\Callback\HasRoles; |
|
10
|
|
|
use Yokai\SecurityExtraBundle\Voter\CallableCollectionVoter; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Yann Eugoné <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class YokaiSecurityExtraExtension extends Extension |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @inheritdoc |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function load(array $configs, ContainerBuilder $container) |
|
21
|
|
|
{ |
|
22
|
2 |
|
$config = $this->processConfiguration(new Configuration(), $configs); |
|
23
|
|
|
|
|
24
|
2 |
|
foreach ($config['permissions'] as $permission) { |
|
25
|
2 |
|
$callables = $this->buildCallables($container, $permission['roles'], $permission['callables']); |
|
26
|
|
|
|
|
27
|
2 |
|
$voterDefinition = (new Definition(CallableCollectionVoter::class)) |
|
28
|
2 |
|
->setArguments([$permission['attributes'], $permission['subjects'], $callables]) |
|
29
|
2 |
|
->addTag('security.voter') |
|
30
|
2 |
|
->setPublic(false) |
|
31
|
|
|
; |
|
32
|
|
|
|
|
33
|
2 |
|
$container->setDefinition($this->uniqueVoterId(), $voterDefinition); |
|
34
|
|
|
} |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ContainerBuilder $container |
|
39
|
|
|
* @param array $roles |
|
40
|
|
|
* @param array $callables |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
2 |
|
private function buildCallables(ContainerBuilder $container, array $roles, array $callables) |
|
45
|
|
|
{ |
|
46
|
2 |
|
$return = []; |
|
47
|
|
|
|
|
48
|
2 |
|
if (count($roles) > 0) { |
|
49
|
1 |
|
$callbackDefinition = (new Definition(HasRoles::class)) |
|
50
|
1 |
|
->setArguments([new Reference('security.access.decision_manager'), $roles]) |
|
51
|
1 |
|
->setPublic(false) |
|
52
|
|
|
; |
|
53
|
|
|
|
|
54
|
1 |
|
$container->setDefinition($callbackId = $this->uniqueCallbackId(), $callbackDefinition); |
|
55
|
|
|
|
|
56
|
1 |
|
$return[] = new Reference($callbackId); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
foreach ($callables as $callable) { |
|
60
|
2 |
|
if (is_array($callable) && isset($callable[0])) { |
|
61
|
2 |
|
$callable[0] = $this->replaceWithReference($callable[0]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
if (is_string($callable)) { |
|
65
|
1 |
|
$callable = $this->replaceWithReference($callable); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
$return[] = $callable; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
return $return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
2 |
|
private function uniqueVoterId() |
|
78
|
|
|
{ |
|
79
|
2 |
|
return uniqid('app.security.voter.configurable.', true); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
1 |
|
private function uniqueCallbackId() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return uniqid('app.security.voter.callback.', true); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $string |
|
92
|
|
|
* |
|
93
|
|
|
* @return string|Reference |
|
94
|
|
|
*/ |
|
95
|
2 |
|
private function replaceWithReference(string $string) |
|
96
|
|
|
{ |
|
97
|
2 |
|
if (strpos($string, '@') !== 0) { |
|
98
|
1 |
|
return $string; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
return new Reference(substr($string, 1)); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|