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
|
|
|
public function getConfigTreeBuilder() |
18
|
|
|
{ |
19
|
|
|
$tree = new TreeBuilder(); |
20
|
|
|
$root = $tree->root('yokai_security_extra'); |
21
|
|
|
|
22
|
|
|
$root |
23
|
|
|
->children() |
24
|
|
|
->append($this->getPermissionsNode()) |
25
|
|
|
->end() |
26
|
|
|
; |
27
|
|
|
|
28
|
|
|
return $tree; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return ArrayNodeDefinition |
33
|
|
|
*/ |
34
|
|
|
private function getPermissionsNode() |
35
|
|
|
{ |
36
|
|
|
$isString = function ($value) { |
37
|
|
|
return is_string($value); |
38
|
|
|
}; |
39
|
|
|
$isNotCollection = function ($value) { |
40
|
|
|
if (!is_array($value)) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (isset($value[0]) && is_string($value[0]) && isset($value[1]) && is_string($value[1])) { |
|
|
|
|
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
}; |
50
|
|
|
$toArray = function ($value) { |
51
|
|
|
return [$value]; |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
($node = $this->root('permissions')) |
55
|
|
|
->prototype('array') |
56
|
|
|
->children() |
57
|
|
|
->arrayNode('attributes') |
58
|
|
|
->info('Matching attribute(s). Empty means all attributes.') |
59
|
|
|
->prototype('scalar')->end() |
60
|
|
|
->beforeNormalization() |
61
|
|
|
->ifTrue($isString)->then($toArray) |
62
|
|
|
->end() |
63
|
|
|
->end() |
64
|
|
|
->arrayNode('subjects') |
65
|
|
|
->info('Matching subject(s) types. Can be either classes, interfaces or types. Empty means all subjects.') |
66
|
|
|
->prototype('scalar')->end() |
67
|
|
|
->beforeNormalization() |
68
|
|
|
->ifTrue($isString)->then($toArray) |
69
|
|
|
->end() |
70
|
|
|
->end() |
71
|
|
|
->arrayNode('roles') |
72
|
|
|
->info('Required role(s) for these attributes & subjects.') |
73
|
|
|
->prototype('scalar')->end() |
74
|
|
|
->beforeNormalization() |
75
|
|
|
->ifTrue($isString)->then($toArray) |
76
|
|
|
->end() |
77
|
|
|
->end() |
78
|
|
|
->arrayNode('callables') |
79
|
|
|
->info('Callables that will verify access.') |
80
|
|
|
->prototype('variable')->end() |
81
|
|
|
->beforeNormalization() |
82
|
|
|
->ifTrue($isNotCollection)->then($toArray) |
83
|
|
|
->end() |
84
|
|
|
->end() |
85
|
|
|
->end() |
86
|
|
|
->end() |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
return $node; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* |
95
|
|
|
* @return ArrayNodeDefinition |
96
|
|
|
*/ |
97
|
|
|
private function root($name) |
98
|
|
|
{ |
99
|
|
|
return (new TreeBuilder())->root($name); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|