1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the LdapToolsBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Chad Sikorra <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace LdapTools\Bundle\LdapToolsBundle\Form\Type; |
12
|
|
|
|
13
|
|
|
use LdapTools\Bundle\LdapToolsBundle\Form\ChoiceList\LdapObjectChoiceList; |
14
|
|
|
use LdapTools\Bundle\LdapToolsBundle\Form\ChoiceLoader\LdapObjectChoiceLoader; |
15
|
|
|
use LdapTools\Bundle\LdapToolsBundle\Form\ChoiceLoader\LegacyLdapChoiceLoader; |
16
|
|
|
use LdapTools\LdapManager; |
17
|
|
|
use Symfony\Component\Form\AbstractType; |
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
21
|
|
|
use Symfony\Component\OptionsResolver\Options; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Provides a form type that represents LDAP object. |
25
|
|
|
* |
26
|
|
|
* @author Chad Sikorra <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class LdapObjectType extends AbstractType |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var LdapManager |
32
|
|
|
*/ |
33
|
|
|
protected $ldap; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param LdapManager $ldap |
37
|
|
|
*/ |
38
|
|
|
public function __construct(LdapManager $ldap) |
39
|
|
|
{ |
40
|
|
|
$this->ldap = $ldap; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function configureOptions(OptionsResolver $resolver) |
47
|
|
|
{ |
48
|
|
|
$ldap = $this->ldap; |
49
|
|
|
|
50
|
|
|
$resolver->setDefaults([ |
51
|
|
|
'ldap_domain' => $this->ldap->getDomainContext(), // The LDAP domain context |
52
|
|
|
'ldap_attributes' => null, // The attributes to select |
53
|
|
|
'ldap_query_builder' => null, // A closure or a LdapQueryBuilder instance |
54
|
|
|
'choice_name' => 'name', |
55
|
|
|
'choice_value' => 'guid', |
56
|
|
|
'choices' => [], // A user supplied array of LDAP objects. |
57
|
|
|
'choice_loader' => function (Options $options) use ($ldap) { |
58
|
|
|
if (!interface_exists('\Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new LdapObjectChoiceLoader($ldap, |
63
|
|
|
$options['ldap_type'], |
64
|
|
|
$options['choice_name'], |
65
|
|
|
$options['choice_value'], |
66
|
|
|
$options['ldap_query_builder'] |
67
|
|
|
); |
68
|
|
|
}, |
69
|
|
|
'choice_list' => function (Options $options) use ($ldap) { |
70
|
|
|
// Always prefer the ChoiceLoader if it exists. Fall back to the ObjectChoiceList... |
71
|
|
|
if (interface_exists('\Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
$legacyChoiceLoader = new LegacyLdapChoiceLoader($ldap, $options['ldap_type'], $options['choice_name'], $options['choice_value'], $options['ldap_query_builder']); |
75
|
|
|
$preferred = isset($options['preferred_choices']) ? $options['preferred_choices'] : []; |
76
|
|
|
|
77
|
|
|
return new LdapObjectChoiceList( |
78
|
|
|
$legacyChoiceLoader->load(), |
79
|
|
|
$options['choice_name'], |
80
|
|
|
$preferred, |
81
|
|
|
null, |
82
|
|
|
$options['choice_value'] |
83
|
|
|
); |
84
|
|
|
}, |
85
|
|
|
]); |
86
|
|
|
$resolver->setRequired(['ldap_type']); |
87
|
|
|
$this->setAllowedTypes($resolver); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function setDefaultOptions(OptionsResolverInterface $resolver) |
94
|
|
|
{ |
95
|
|
|
$this->configureOptions($resolver); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getParent() |
102
|
|
|
{ |
103
|
|
|
// FQCN is needed instead of a simple name in Symfony 3+ ... |
104
|
|
|
$interface = new \ReflectionClass('\Symfony\Component\Form\FormTypeInterface'); |
105
|
|
|
|
106
|
|
|
if ($interface->hasMethod('getName')) { |
107
|
|
|
$parent = 'choice'; |
108
|
|
|
} else { |
109
|
|
|
$parent = ChoiceType::class; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $parent; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getName() |
119
|
|
|
{ |
120
|
|
|
return 'ldap_object'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* A rather ugly way of allowing compatibility with the resolver component for pre 2.6 versions. |
125
|
|
|
* |
126
|
|
|
* @param OptionsResolver $resolver |
127
|
|
|
*/ |
128
|
|
|
protected function setAllowedTypes(OptionsResolver $resolver) |
129
|
|
|
{ |
130
|
|
|
$allowed = ['ldap_query_builder', ['\Closure', 'LdapTools\Query\LdapQueryBuilder', 'null']]; |
131
|
|
|
|
132
|
|
|
$reflection = new \ReflectionClass(get_class($resolver)); |
133
|
|
|
$parameters = $reflection->getMethod('addAllowedTypes')->getParameters(); |
134
|
|
|
|
135
|
|
|
if ($parameters[0]->isArray()) { |
136
|
|
|
$resolver->setAllowedTypes([$allowed[0] => $allowed[1]]); |
|
|
|
|
137
|
|
|
} else { |
138
|
|
|
$resolver->setAllowedTypes(...$allowed); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks for function calls that miss required arguments.