1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\ManagementBundle\Validator; |
20
|
|
|
|
21
|
|
|
use Assert\Assertion; |
22
|
|
|
use Assert\AssertionFailedException; |
23
|
|
|
use Assert\InvalidArgumentException as AssertionException; |
24
|
|
|
use InvalidArgumentException as CoreInvalidArgumentException; |
25
|
|
|
use Surfnet\Stepup\Helper\JsonHelper; |
26
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Assert as StepupAssert; |
27
|
|
|
use Symfony\Component\Validator\Constraint; |
28
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
29
|
|
|
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder; |
30
|
|
|
use TypeError; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Once the Assert 2.0 library has been built this should be converted to the lazy assertions so we can report |
34
|
|
|
* all errors at once. |
35
|
|
|
*/ |
|
|
|
|
36
|
|
|
class ConfigurationStructureValidator extends ConstraintValidator |
37
|
|
|
{ |
38
|
|
|
public function __construct( |
39
|
|
|
private readonly GatewayConfigurationValidator $gatewayConfigurationValidator, |
40
|
|
|
private readonly EmailTemplatesConfigurationValidator $emailTemplatesConfigurationValidator, |
41
|
|
|
) { |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function validate(mixed $value, Constraint $constraint): void |
45
|
|
|
{ |
46
|
|
|
/** @var ConstraintViolationBuilder|false $violation */ |
|
|
|
|
47
|
|
|
$violation = false; |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$decoded = $this->decodeJson($value); |
51
|
|
|
$this->validateRoot($decoded); |
52
|
|
|
} catch (AssertionException $exception) { |
53
|
|
|
// method is not in the interface yet, but the old method is deprecated. |
54
|
|
|
$violation = $this->context->buildViolation($exception->getMessage()); |
55
|
|
|
$violation->atPath($exception->getPropertyPath()); |
|
|
|
|
56
|
|
|
} catch (CoreInvalidArgumentException|TypeError $exception) { |
57
|
|
|
$violation = $this->context->buildViolation($exception->getMessage()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($violation) { |
61
|
|
|
// ensure we have a sensible path. |
62
|
|
|
$violation->addViolation(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function decodeJson(string $rawValue): mixed |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return JsonHelper::decode($rawValue); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function validateRoot(array $configuration): void |
72
|
|
|
{ |
73
|
|
|
$acceptedProperties = ['gateway', 'sraa', 'email_templates']; |
74
|
|
|
StepupAssert::keysMatch( |
75
|
|
|
$configuration, |
76
|
|
|
$acceptedProperties, |
77
|
|
|
sprintf("Expected only properties '%s'", implode(',', $acceptedProperties)), |
78
|
|
|
'(root)', |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->validateGatewayConfiguration($configuration, 'gateway'); |
82
|
|
|
$this->validateSraaConfiguration($configuration, 'sraa'); |
83
|
|
|
$this->validateEmailTemplatesConfiguration($configuration, 'email_templates'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function validateGatewayConfiguration(array $configuration, string $propertyPath): void |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
Assertion::isArray($configuration['gateway'], 'Property "gateway" must have an object as value', $propertyPath); |
89
|
|
|
|
90
|
|
|
$this->gatewayConfigurationValidator->validate($configuration['gateway'], $propertyPath); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function validateSraaConfiguration(array $configuration, string $propertyPath): void |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
Assertion::isArray( |
96
|
|
|
$configuration['sraa'], |
97
|
|
|
'Property sraa must have an array of name_ids (string) as value', |
98
|
|
|
$propertyPath, |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
foreach ($configuration['sraa'] as $index => $value) { |
102
|
|
|
Assertion::string( |
103
|
|
|
$value, |
104
|
|
|
'value must be a string (the name_id of the SRAA)', |
105
|
|
|
$propertyPath . '[' . $index . ']', |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
|
|
|
|
111
|
|
|
* @param array<string, mixed> $configuration |
|
|
|
|
112
|
|
|
* @throws AssertionFailedException |
|
|
|
|
113
|
|
|
*/ |
|
|
|
|
114
|
|
|
private function validateEmailTemplatesConfiguration(array $configuration, string $propertyPath): void |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
Assertion::isArray( |
117
|
|
|
$configuration['email_templates'], |
118
|
|
|
'Property "email_templates" must have an object as value', |
119
|
|
|
$propertyPath, |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$this->emailTemplatesConfigurationValidator->validate($configuration['email_templates'], $propertyPath); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|