11
11
use Drupal \Core \StringTranslation \StringTranslationTrait ;
12
12
use Drupal \Core \StringTranslation \TranslatableMarkup ;
13
13
use Drupal \Core \Url ;
14
+ use Drupal \os2forms_digital_post \Helper \CertificateLocatorHelper ;
14
15
use Drupal \os2forms_digital_post \Helper \Settings ;
15
16
use Symfony \Component \DependencyInjection \ContainerInterface ;
16
17
@@ -34,6 +35,7 @@ public function __construct(
34
35
ConfigFactoryInterface $ config_factory ,
35
36
EntityTypeManagerInterface $ entityTypeManager ,
36
37
private readonly Settings $ settings ,
38
+ private readonly CertificateLocatorHelper $ certificateLocatorHelper ,
37
39
) {
38
40
parent ::__construct ($ config_factory );
39
41
$ this ->queueStorage = $ entityTypeManager ->getStorage ('advancedqueue_queue ' );
@@ -49,6 +51,7 @@ public static function create(ContainerInterface $container) {
49
51
$ container ->get ('config.factory ' ),
50
52
$ container ->get ('entity_type.manager ' ),
51
53
$ container ->get (Settings::class),
54
+ $ container ->get (CertificateLocatorHelper::class),
52
55
);
53
56
}
54
57
@@ -134,17 +137,127 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
134
137
'#type ' => 'fieldset ' ,
135
138
'#title ' => $ this ->t ('Certificate ' ),
136
139
'#tree ' => TRUE ,
140
+ ];
141
+
142
+ $ form [Settings::CERTIFICATE ][Settings::CERTIFICATE_PROVIDER ] = [
143
+ '#type ' => 'select ' ,
144
+ '#title ' => $ this ->t ('Provider ' ),
145
+ '#options ' => [
146
+ Settings::PROVIDER_TYPE_FORM => $ this ->t ('Form ' ),
147
+ Settings::PROVIDER_TYPE_KEY => $ this ->t ('Key ' ),
148
+ ],
149
+ '#default_value ' => $ this ->settings ->getEditableValue ([Settings::CERTIFICATE , Settings::CERTIFICATE_PROVIDER ]) ?? Settings::PROVIDER_TYPE_FORM ,
150
+ '#description ' => $ this ->t ('Specifies which provider to use ' ),
151
+ ];
152
+
153
+ $ form [Settings::CERTIFICATE ][CertificateLocatorHelper::LOCATOR_TYPE ] = [
154
+ '#type ' => 'select ' ,
155
+ '#title ' => $ this ->t ('Certificate locator type ' ),
156
+ '#options ' => [
157
+ CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT => $ this ->t ('Azure key vault ' ),
158
+ CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM => $ this ->t ('File system ' ),
159
+ ],
160
+ '#default_value ' => $ this ->settings ->getEditableValue ([
161
+ Settings::CERTIFICATE ,
162
+ CertificateLocatorHelper::LOCATOR_TYPE ,
163
+ ]) ?? NULL ,
164
+ '#states ' => [
165
+ 'visible ' => [':input[name="certificate[certificate_provider]"] ' => ['value ' => Settings::PROVIDER_TYPE_FORM ]],
166
+ ],
167
+ '#description ' => $ this ->t ('Specifies which locator to use ' ),
168
+ ];
137
169
138
- Settings::KEY => [
139
- '#type ' => 'key_select ' ,
140
- '#key_filters ' => [
141
- 'type ' => 'os2web_key_certificate ' ,
170
+ $ form [Settings::CERTIFICATE ][CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT ] = [
171
+ '#type ' => 'fieldset ' ,
172
+ '#title ' => $ this ->t ('Azure key vault ' ),
173
+ '#states ' => [
174
+ 'visible ' => [
175
+ ':input[name="certificate[certificate_provider]"] ' => ['value ' => Settings::PROVIDER_TYPE_FORM ],
176
+ ':input[name="certificate[locator_type]"] ' => ['value ' => CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT ],
177
+ ],
178
+ ],
179
+ ];
180
+
181
+ $ settings = [
182
+ CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_TENANT_ID => ['title ' => $ this ->t ('Tenant id ' )],
183
+ CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_APPLICATION_ID => ['title ' => $ this ->t ('Application id ' )],
184
+ CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_CLIENT_SECRET => ['title ' => $ this ->t ('Client secret ' )],
185
+ CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_NAME => ['title ' => $ this ->t ('Name ' )],
186
+ CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_SECRET => ['title ' => $ this ->t ('Secret ' )],
187
+ CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_VERSION => ['title ' => $ this ->t ('Version ' )],
188
+ ];
189
+
190
+ foreach ($ settings as $ key => $ info ) {
191
+ $ form [Settings::CERTIFICATE ][CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT ][$ key ] = [
192
+ '#type ' => 'textfield ' ,
193
+ '#title ' => $ info ['title ' ],
194
+ '#default_value ' => $ this ->settings ->getEditableValue ([
195
+ Settings::CERTIFICATE ,
196
+ CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT ,
197
+ $ key ,
198
+ ]) ?? NULL ,
199
+ '#states ' => [
200
+ 'required ' => [
201
+ ':input[name="certificate[certificate_provider]"] ' => ['value ' => Settings::PROVIDER_TYPE_FORM ],
202
+ ':input[name="certificate[locator_type]"] ' => ['value ' => CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT ],
203
+ ],
204
+ ],
205
+ ];
206
+ }
207
+
208
+ $ form [Settings::CERTIFICATE ][CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM ] = [
209
+ '#type ' => 'fieldset ' ,
210
+ '#title ' => $ this ->t ('File system ' ),
211
+ '#states ' => [
212
+ 'visible ' => [
213
+ ':input[name="certificate[certificate_provider]"] ' => ['value ' => Settings::PROVIDER_TYPE_FORM ],
214
+ ':input[name="certificate[locator_type]"] ' => ['value ' => CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM ],
215
+ ],
216
+ ],
217
+
218
+ CertificateLocatorHelper::LOCATOR_FILE_SYSTEM_PATH => [
219
+ '#type ' => 'textfield ' ,
220
+ '#title ' => $ this ->t ('Path ' ),
221
+ '#default_value ' => $ this ->settings ->getEditableValue ([
222
+ Settings::CERTIFICATE ,
223
+ CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM ,
224
+ CertificateLocatorHelper::LOCATOR_FILE_SYSTEM_PATH ,
225
+ ]) ?? NULL ,
226
+ '#states ' => [
227
+ 'required ' => [
228
+ ':input[name="certificate[certificate_provider]"] ' => ['value ' => Settings::PROVIDER_TYPE_FORM ],
229
+ ':input[name="certificate[locator_type]"] ' => ['value ' => CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM ],
230
+ ],
142
231
],
143
- '#key_description ' => FALSE ,
144
- '#title ' => $ this ->t ('Key ' ),
145
- '#default_value ' => $ this ->settings ->getEditableValue ([Settings::CERTIFICATE , Settings::KEY ]),
146
- '#required ' => TRUE ,
147
- '#description ' => $ this ->createDescription ([Settings::CERTIFICATE , Settings::KEY ]),
232
+ ],
233
+ ];
234
+
235
+ $ form [Settings::CERTIFICATE ][CertificateLocatorHelper::LOCATOR_PASSPHRASE ] = [
236
+ '#type ' => 'textfield ' ,
237
+ '#title ' => $ this ->t ('Passphrase ' ),
238
+ '#default_value ' => $ this ->settings ->getEditableValue ([
239
+ Settings::CERTIFICATE ,
240
+ CertificateLocatorHelper::LOCATOR_PASSPHRASE ,
241
+ ]) ?? '' ,
242
+ '#states ' => [
243
+ 'visible ' => [
244
+ ':input[name="certificate[certificate_provider]"] ' => ['value ' => Settings::PROVIDER_TYPE_FORM ],
245
+ ],
246
+ ],
247
+ ];
248
+
249
+ $ form [Settings::CERTIFICATE ][Settings::PROVIDER_TYPE_KEY ] = [
250
+ '#type ' => 'key_select ' ,
251
+ '#key_filters ' => [
252
+ 'type ' => 'os2web_key_certificate ' ,
253
+ ],
254
+ '#key_description ' => FALSE ,
255
+ '#title ' => $ this ->t ('Key ' ),
256
+ '#default_value ' => $ this ->settings ->getEditableValue ([Settings::CERTIFICATE , Settings::PROVIDER_TYPE_KEY ]),
257
+ '#required ' => TRUE ,
258
+ '#description ' => $ this ->createDescription ([Settings::CERTIFICATE , Settings::PROVIDER_TYPE_KEY ]),
259
+ '#states ' => [
260
+ 'visible ' => [':input[name="certificate[certificate_provider]"] ' => ['value ' => Settings::PROVIDER_TYPE_KEY ]],
148
261
],
149
262
];
150
263
@@ -176,15 +289,55 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
176
289
),
177
290
];
178
291
292
+ $ form ['actions ' ]['testCertificate ' ] = [
293
+ '#type ' => 'submit ' ,
294
+ '#name ' => 'testCertificate ' ,
295
+ '#value ' => $ this ->t ('Test certificate ' ),
296
+ '#states ' => [
297
+ 'visible ' => [':input[name="certificate[certificate_provider]"] ' => ['value ' => Settings::PROVIDER_TYPE_FORM ]],
298
+ ],
299
+ ];
300
+
179
301
return $ form ;
180
302
}
181
303
304
+ /**
305
+ * {@inheritdoc}
306
+ *
307
+ * @phpstan-param array<string, mixed> $form
308
+ */
309
+ public function validateForm (array &$ form , FormStateInterface $ form_state ): void {
310
+ $ triggeringElement = $ form_state ->getTriggeringElement ();
311
+ if ('testCertificate ' === ($ triggeringElement ['#name ' ] ?? NULL )) {
312
+ return ;
313
+ }
314
+
315
+ $ values = $ form_state ->getValues ();
316
+
317
+ if (Settings::PROVIDER_TYPE_FORM === $ values [Settings::CERTIFICATE ][Settings::CERTIFICATE_PROVIDER ]) {
318
+ if (CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM === $ values [Settings::CERTIFICATE ][CertificateLocatorHelper::LOCATOR_TYPE ]) {
319
+ $ path = $ values [Settings::CERTIFICATE ][CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM ][CertificateLocatorHelper::LOCATOR_FILE_SYSTEM_PATH ] ?? NULL ;
320
+ if (!file_exists ($ path )) {
321
+ $ form_state ->setErrorByName ('certificate][file_system][path ' , $ this ->t ('Invalid certificate path: %path ' , ['%path ' => $ path ]));
322
+ }
323
+ }
324
+ }
325
+
326
+ parent ::validateForm ($ form , $ form_state );
327
+ }
328
+
182
329
/**
183
330
* {@inheritdoc}
184
331
*
185
332
* @phpstan-param array<string, mixed> $form
186
333
*/
187
334
public function submitForm (array &$ form , FormStateInterface $ form_state ): void {
335
+ $ triggeringElement = $ form_state ->getTriggeringElement ();
336
+ if ('testCertificate ' === ($ triggeringElement ['#name ' ] ?? NULL )) {
337
+ $ this ->testCertificate ();
338
+ return ;
339
+ }
340
+
188
341
$ config = $ this ->config (Settings::CONFIG_NAME );
189
342
foreach ([
190
343
Settings::TEST_MODE ,
@@ -223,4 +376,20 @@ private function createDescription(string|array $key, ?TranslatableMarkup $descr
223
376
return (string ) $ description ;
224
377
}
225
378
379
+ /**
380
+ * Test certificate.
381
+ */
382
+ private function testCertificate (): void {
383
+ try {
384
+
385
+ $ certificateLocator = $ this ->certificateLocatorHelper ->getCertificateLocator ();
386
+ $ certificateLocator ->getCertificates ();
387
+ $ this ->messenger ()->addStatus ($ this ->t ('Certificate succesfully tested ' ));
388
+ }
389
+ catch (\Throwable $ throwable ) {
390
+ $ message = $ this ->t ('Error testing certificate: %message ' , ['%message ' => $ throwable ->getMessage ()]);
391
+ $ this ->messenger ()->addError ($ message );
392
+ }
393
+ }
394
+
226
395
}
0 commit comments