1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Serializer; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\ClassUtils; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Method; |
9
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Right; |
10
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata; |
11
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\RestMetadataFactory; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
14
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Philip Washington Sorst <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class RestDenormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface |
22
|
|
|
{ |
23
|
|
|
const DDR_REST_METHOD = 'ddrRestMethod'; |
24
|
|
|
const DDR_REST_ENTITY = 'ddrRestEntity'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RestMetadataFactory |
28
|
|
|
*/ |
29
|
|
|
private $metadataFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var PropertyAccessorInterface |
33
|
|
|
*/ |
34
|
|
|
private $propertyAccessor; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var AuthorizationCheckerInterface|null |
38
|
|
|
*/ |
39
|
|
|
private $authorizationChecker; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var EntityManagerInterface |
43
|
|
|
*/ |
44
|
|
|
private $entityManager; |
45
|
|
|
|
46
|
80 |
|
public function __construct( |
47
|
|
|
RestMetadataFactory $metadataFactory, |
48
|
|
|
PropertyAccessorInterface $propertyAccessor, |
49
|
|
|
EntityManagerInterface $entityManager |
50
|
|
|
) { |
51
|
80 |
|
$this->metadataFactory = $metadataFactory; |
52
|
80 |
|
$this->propertyAccessor = $propertyAccessor; |
53
|
80 |
|
$this->entityManager = $entityManager; |
54
|
80 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
24 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
60
|
|
|
{ |
61
|
24 |
|
$method = $context[self::DDR_REST_METHOD]; |
62
|
24 |
|
$entity = array_key_exists(self::DDR_REST_ENTITY, $context) ? $context[self::DDR_REST_ENTITY] : null; |
63
|
|
|
|
64
|
24 |
|
if (null === $entity) { |
65
|
14 |
|
$entity = new $class; |
66
|
|
|
} |
67
|
|
|
|
68
|
24 |
|
$this->updateObject($entity, $method, $data); |
69
|
|
|
|
70
|
24 |
|
return $entity; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
24 |
|
public function supportsDenormalization($data, $type, $format = null) |
77
|
|
|
{ |
78
|
24 |
|
if ('json' === $format) { |
79
|
24 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
24 |
|
public function hasCacheableSupportsMethod(): bool |
89
|
|
|
{ |
90
|
|
|
// TODO: change |
91
|
24 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param object $object Access by reference. |
96
|
|
|
* @param string $method |
97
|
|
|
* @param array $data |
98
|
|
|
*/ |
99
|
24 |
|
protected function updateObject(&$object, $method, $data) |
100
|
|
|
{ |
101
|
24 |
|
$classMetadata = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($object)); |
102
|
|
|
|
103
|
24 |
|
foreach ($data as $key => $value) { |
104
|
22 |
|
if (array_key_exists($key, $classMetadata->propertyMetadata)) { |
105
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
106
|
22 |
|
$propertyMetadata = $classMetadata->propertyMetadata[$key]; |
107
|
22 |
|
if ($this->isUpdateable($object, $method, $propertyMetadata)) { |
108
|
22 |
|
$this->updateProperty($object, $method, $propertyMetadata, $value); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
24 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param object $object Access by reference. |
116
|
|
|
* @param string $method |
117
|
|
|
* @param PropertyMetadata $propertyMetadata |
118
|
|
|
* @param mixed $value |
119
|
|
|
*/ |
120
|
22 |
|
protected function updateProperty(&$object, string $method, PropertyMetadata $propertyMetadata, $value) |
121
|
|
|
{ |
122
|
22 |
|
$byReference = $this->isUpdateableByReference($propertyMetadata, $method); |
123
|
22 |
|
if ($byReference) { |
124
|
4 |
|
$this->updateByReference($object, $propertyMetadata, $value); |
125
|
18 |
|
} elseif (array_key_exists($propertyMetadata->getType(), Type::getTypesMap())) { |
126
|
18 |
|
$convertedValue = $this->convert($propertyMetadata->getType(), $value); |
127
|
18 |
|
$this->propertyAccessor->setValue($object, $propertyMetadata->name, $convertedValue); |
128
|
|
|
} else { |
129
|
2 |
|
$this->updatePropertyObject($object, $method, $propertyMetadata, $value); |
130
|
|
|
} |
131
|
22 |
|
} |
132
|
|
|
|
133
|
4 |
|
private function updateByReference(&$object, PropertyMetadata $propertyMetadata, $value) |
134
|
|
|
{ |
135
|
4 |
|
if (null === $value) { |
136
|
|
|
$this->propertyAccessor->setValue($object, $propertyMetadata->name, null); |
137
|
|
|
} else { |
138
|
4 |
|
$type = $propertyMetadata->getType(); |
139
|
4 |
|
$classMetadata = $this->entityManager->getClassMetadata($type); |
140
|
4 |
|
$identifiers = $classMetadata->getIdentifier(); |
141
|
4 |
|
$id = []; |
142
|
4 |
|
foreach ($identifiers as $idName) { |
143
|
4 |
|
$id[$idName] = $value[$idName]; |
144
|
|
|
} |
145
|
4 |
|
$reference = $this->entityManager->getReference($type, $id); |
146
|
4 |
|
$this->propertyAccessor->setValue($object, $propertyMetadata->name, $reference); |
147
|
|
|
} |
148
|
4 |
|
} |
149
|
|
|
|
150
|
2 |
|
protected function updatePropertyObject(&$object, string $method, PropertyMetadata $propertyMetadata, $value) |
151
|
|
|
{ |
152
|
2 |
|
$propertyObject = $this->propertyAccessor->getValue($object, $propertyMetadata->name); |
153
|
2 |
|
if (null === $propertyObject) { |
154
|
|
|
$type = $propertyMetadata->getType(); |
155
|
|
|
$propertyObject = new $type; |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
$this->updateObject($propertyObject, $method, $value); |
159
|
2 |
|
$this->propertyAccessor->setValue($object, $propertyMetadata->name, $propertyObject); |
160
|
2 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $method |
164
|
|
|
* @param object $object |
165
|
|
|
* @param PropertyMetadata $propertyMetadata |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
22 |
|
protected function isUpdateable($object, string $method, PropertyMetadata $propertyMetadata): bool |
170
|
|
|
{ |
171
|
22 |
|
if ((Request::METHOD_PUT === $method || Request::METHOD_PATCH === $method) && $propertyMetadata->isPuttable()) { |
172
|
10 |
|
return $this->isGranted($object, $propertyMetadata->getPuttable()->right); |
173
|
|
|
} |
174
|
|
|
|
175
|
18 |
|
if (Request:: METHOD_POST === $method && $propertyMetadata->isPostable()) { |
176
|
12 |
|
return $this->isGranted($object, $propertyMetadata->getPostable()->right); |
177
|
|
|
} |
178
|
|
|
|
179
|
12 |
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
22 |
|
private function isGranted($object, ?Right $right): bool |
183
|
|
|
{ |
184
|
22 |
|
if (null === $right) { |
185
|
22 |
|
return true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/* If no Security is enabled always deny access */ |
189
|
12 |
|
if (null === $this->authorizationChecker) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
12 |
|
$propertyPath = $right->propertyPath; |
194
|
12 |
|
if (null === $propertyPath) { |
195
|
12 |
|
return $this->authorizationChecker->isGranted($right->attributes); |
196
|
|
|
} else { |
197
|
|
|
$subject = $this->resolveSubject($object, $propertyPath); |
198
|
|
|
|
199
|
|
|
return $this->authorizationChecker->isGranted($right->attributes, $subject); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
private function resolveSubject($entity, $propertyPath) |
204
|
|
|
{ |
205
|
|
|
if ('this' === $propertyPath) { |
206
|
|
|
return $entity; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this->propertyAccessor->getValue($entity, $propertyPath); |
210
|
|
|
} |
211
|
|
|
|
212
|
18 |
|
private function convert(?string $type, $value) |
213
|
|
|
{ |
214
|
18 |
|
if (null === $value) { |
215
|
|
|
return $value; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
switch ($type) { |
219
|
18 |
|
case 'datetime': |
220
|
18 |
|
case 'date': |
221
|
18 |
|
case 'time': |
222
|
2 |
|
return new \DateTime($value); |
223
|
|
|
default: |
224
|
18 |
|
return $value; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
22 |
|
private function isUpdateableByReference(PropertyMetadata $propertyMetadata, string $method) |
229
|
|
|
{ |
230
|
|
|
if ( |
231
|
22 |
|
Method::PUT === $method |
232
|
22 |
|
&& null !== $propertyMetadata->getPuttable() && true === $propertyMetadata->getPuttable()->byReference |
233
|
|
|
) { |
234
|
2 |
|
return true; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if ( |
238
|
20 |
|
Method::POST === $method |
239
|
20 |
|
&& null !== $propertyMetadata->getPostable() && true === $propertyMetadata->getPostable()->byReference |
240
|
|
|
) { |
241
|
2 |
|
return true; |
242
|
|
|
} |
243
|
|
|
|
244
|
18 |
|
return false; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param AuthorizationCheckerInterface $authorizationChecker |
249
|
|
|
*/ |
250
|
76 |
|
public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker) |
251
|
|
|
{ |
252
|
76 |
|
$this->authorizationChecker = $authorizationChecker; |
253
|
76 |
|
} |
254
|
|
|
} |
255
|
|
|
|