1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yokai\SecurityExtraBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
7
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Yann Eugoné <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Configuration implements ConfigurationInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritDoc |
16
|
|
|
*/ |
17
|
2 |
|
public function getConfigTreeBuilder() |
18
|
|
|
{ |
19
|
2 |
|
$tree = new TreeBuilder(); |
20
|
2 |
|
$root = $tree->root('yokai_security_extra'); |
21
|
|
|
|
22
|
|
|
$root |
23
|
2 |
|
->children() |
24
|
2 |
|
->append($this->getPermissionsNode()) |
25
|
2 |
|
->end() |
26
|
|
|
; |
27
|
|
|
|
28
|
2 |
|
return $tree; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return ArrayNodeDefinition |
33
|
|
|
*/ |
34
|
|
|
private function getPermissionsNode() |
35
|
|
|
{ |
36
|
2 |
|
$isString = function ($value) { |
37
|
2 |
|
return is_string($value); |
38
|
2 |
|
}; |
39
|
2 |
|
$isNotCollection = function ($value) { |
40
|
2 |
|
if (!is_array($value)) { |
41
|
1 |
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
return isset($value[0]) && is_string($value[0]) && isset($value[1]) && is_string($value[1]); |
45
|
2 |
|
}; |
46
|
2 |
|
$toArray = function ($value) { |
47
|
2 |
|
return [$value]; |
48
|
2 |
|
}; |
49
|
|
|
|
50
|
2 |
|
($node = $this->root('permissions')) |
51
|
2 |
|
->prototype('array') |
52
|
2 |
|
->children() |
53
|
2 |
|
->arrayNode('attributes') |
54
|
2 |
|
->info('Matching attribute(s). Empty means all attributes.') |
55
|
2 |
|
->prototype('scalar')->end() |
56
|
2 |
|
->beforeNormalization() |
57
|
2 |
|
->ifTrue($isString)->then($toArray) |
58
|
2 |
|
->end() |
59
|
2 |
|
->end() |
60
|
2 |
|
->arrayNode('subjects') |
61
|
2 |
|
->info('Matching subject(s) types. Can be either classes, interfaces or types. Empty means all subjects.') |
62
|
2 |
|
->prototype('scalar')->end() |
63
|
2 |
|
->beforeNormalization() |
64
|
2 |
|
->ifTrue($isString)->then($toArray) |
65
|
2 |
|
->end() |
66
|
2 |
|
->end() |
67
|
2 |
|
->arrayNode('roles') |
68
|
2 |
|
->info('Required role(s) for these attributes & subjects.') |
69
|
2 |
|
->prototype('scalar')->end() |
70
|
2 |
|
->beforeNormalization() |
71
|
2 |
|
->ifTrue($isString)->then($toArray) |
72
|
2 |
|
->end() |
73
|
2 |
|
->end() |
74
|
2 |
|
->arrayNode('callables') |
75
|
2 |
|
->info('Callables that will verify access.') |
76
|
2 |
|
->prototype('variable')->end() |
77
|
2 |
|
->beforeNormalization() |
78
|
2 |
|
->ifTrue($isNotCollection)->then($toArray) |
79
|
2 |
|
->end() |
80
|
2 |
|
->end() |
81
|
2 |
|
->end() |
82
|
2 |
|
->end() |
83
|
|
|
; |
84
|
|
|
|
85
|
2 |
|
return $node; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $name |
90
|
|
|
* |
91
|
|
|
* @return ArrayNodeDefinition |
92
|
|
|
*/ |
93
|
2 |
|
private function root($name) |
94
|
|
|
{ |
95
|
2 |
|
return (new TreeBuilder())->root($name); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|