-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathLdapObjectSubscriber.php
More file actions
283 lines (250 loc) · 8.33 KB
/
LdapObjectSubscriber.php
File metadata and controls
283 lines (250 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* This file is part of the LdapToolsBundle package.
*
* (c) Chad Sikorra <Chad.Sikorra@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace LdapTools\Bundle\LdapToolsBundle\Doctrine\Subscriber;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\ObjectManager;
use LdapTools\Bundle\LdapToolsBundle\Annotation\LdapObject as LdapObjectAnnotation;
use Doctrine\ORM\Event\LifecycleEventArgs;
use LdapTools\LdapManager;
use LdapTools\Object\LdapObject;
use LdapTools\Object\LdapObjectCollection;
/**
* Doctrine Lifecycle Events to load/save LDAP objects to an entity property.
*
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
class LdapObjectSubscriber implements EventSubscriber
{
/**
* The annotation class to check for.
*/
const ANNOTATION = 'LdapTools\Bundle\LdapToolsBundle\Annotation\LdapObject';
/**
* @var Reader
*/
protected $reader;
/**
* @var LdapManager
*/
protected $ldap;
/**
* @param Reader $reader
* @param LdapManager $ldap
*/
public function __construct(Reader $reader, LdapManager $ldap)
{
$this->reader = $reader;
$this->ldap = $ldap;
}
/**
* @return array
*/
public function getSubscribedEvents()
{
return [
'prePersist',
'preUpdate',
'postLoad',
];
}
/**
* @param LifecycleEventArgs $args
*/
public function preUpdate(LifecycleEventArgs $args)
{
$this->transformValueForDb($args);
}
/**
* @param LifecycleEventArgs $args
*/
public function prePersist(LifecycleEventArgs $args)
{
$this->transformValueForDb($args);
}
/**
* @param LifecycleEventArgs $args
*/
public function postLoad(LifecycleEventArgs $args)
{
$entity = $this->getObjectFromLifeCycleArgs($args);
$om = $this->getOmFromLifeCycleArgs($args);
$properties = $this->getLdapObjectAnnotationProperties($entity, $om);
foreach ($properties as $info) {
if ($info['property']->getValue($entity)) {
$this->setLdapObjectForProperty($info['property'], $info['annotation'], $entity);
}
}
}
/**
* Handles transforming the value as it currently is to the value the DB expects.
*
* @param LifecycleEventArgs $args
*/
protected function transformValueForDb(LifecycleEventArgs $args)
{
$entity = $this->getObjectFromLifeCycleArgs($args);
$om = $this->getOmFromLifeCycleArgs($args);
$properties = $this->getLdapObjectAnnotationProperties($entity, $om);
foreach ($properties as $info) {
if ($info['property']->getValue($entity)) {
$this->setLdapValueForProperty($info['property'], $info['annotation'], $entity);
}
}
}
/**
* Get all the properties from the entity that have a LdapObject annotation defined.
*
* @param object $entity
* @param ObjectManager $om
* @return array
*/
protected function getLdapObjectAnnotationProperties($entity, $om)
{
$properties = $om->getClassMetadata(get_class($entity))->getReflectionProperties();
$ldapObjectProps = [];
foreach ($properties as $prop) {
$annotation = $this->reader->getPropertyAnnotation($prop, self::ANNOTATION);
if (!empty($annotation)) {
$ldapObjectProps[] = ['annotation' => $annotation, 'property' => $prop];
}
}
return $ldapObjectProps;
}
/**
* Based on an array of IDs for LDAP objects, set the property to either a LdapObject for LdapObjectCollection.
*
* @param \ReflectionProperty $property
* @param LdapObjectAnnotation $annotation
* @param $entity
*/
protected function setLdapObjectForProperty(\ReflectionProperty $property, LdapObjectAnnotation $annotation, $entity)
{
if (empty($property->getValue($entity))) {
return;
}
$domain = $this->ldap->getDomainContext();
$switchDomain = $annotation->domain ?: null;
if ($switchDomain) {
$this->ldap->switchDomain($annotation->domain);
}
$results = $this->queryLdapForObjects($property, $annotation, $entity);
$property->setValue($entity, $results);
if ($switchDomain) {
$this->ldap->switchDomain($domain);
}
}
/**
* Get the specified ID for each LDAP object and save it to an array.
*
* @param \ReflectionProperty $property
* @param LdapObjectAnnotation $annotation
* @param $entity
*/
protected function setLdapValueForProperty(\ReflectionProperty $property, LdapObjectAnnotation $annotation, $entity)
{
$value = $property->getValue($entity);
if ($value instanceof LdapObject) {
$ldapValues = $value->get($annotation->id);
} elseif ($value instanceof LdapObjectCollection) {
$ldapValues = [];
foreach ($value->toArray() as $ldapObject) {
$ldapValues[] = $ldapObject->get($annotation->id);
}
} else {
throw new \InvalidArgumentException(sprintf(
'Class "%s" is not valid. Expected a LdapObject or LdapObjectCollection',
get_class($value)
));
}
$property->setValue($entity, $ldapValues);
}
/**
* @param LdapObjectAnnotation $annotation
* @return array
*/
protected function getLdapAttributesToSelect(LdapObjectAnnotation $annotation)
{
$attributes = $annotation->attributes;
if (empty($attributes)) {
$schemaFactory = $this->ldap->getSchemaFactory();
$schema = $schemaFactory->get(
$this->ldap->getConnection()->getConfig()->getSchemaName(),
$annotation->type
);
$attributes = $schema->getAttributesToSelect();
}
if (!in_array($annotation->id, $attributes)) {
$attributes[] = $annotation->id;
}
return $attributes;
}
/**
* @param \ReflectionProperty $property
* @param LdapObjectAnnotation $annotation
* @param $entity
* @return LdapObject|LdapObjectCollection|null
*/
protected function queryLdapForObjects(\ReflectionProperty $property, LdapObjectAnnotation $annotation, $entity)
{
$query = $this->ldap->buildLdapQuery()
->select($this->getLdapAttributesToSelect($annotation))
->from($annotation->type);
$values = $property->getValue($entity);
// A single LdapObject type...
if (is_string($values) && !empty($values)) {
$query->where([$annotation->id => $values]);
// A LdapObjectCollection type...
} elseif (is_array($values) && !empty($values)) {
foreach ($values as $value) {
$query->orWhere([$annotation->id => $value]);
}
// A currently null/empty value?
} else {
return null;
}
if ($annotation->collection) {
$results = $query->getLdapQuery()->getResult();
} else {
$results = $query->getLdapQuery()->getOneOrNullResult();
}
return $results;
}
/**
* Avoid calling deprecated methods if possible.
*
* @param LifecycleEventArgs $args
* @return object
*/
protected function getObjectFromLifeCycleArgs(LifecycleEventArgs $args)
{
$rc = new \ReflectionClass('Doctrine\ORM\Event\LifecycleEventArgs');
if ($rc->hasMethod('getObject')) {
return $args->getObject();
} else {
return $args->getEntity();
}
}
/**
* Avoid calling deprecated methods if possible.
*
* @param LifecycleEventArgs $args
* @return \Doctrine\Common\Persistence\ObjectManager|\Doctrine\ORM\EntityManager
*/
protected function getOmFromLifeCycleArgs(LifecycleEventArgs $args)
{
$rc = new \ReflectionClass('Doctrine\ORM\Event\LifecycleEventArgs');
if ($rc->hasMethod('getObjectManager')) {
return $args->getObjectManager();
} else {
return $args->getEntityManager();
}
}
}