|
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 |
|
@@ -134,17 +135,127 @@ public function buildForm(array $form, FormStateInterface $form_state): array { |
134 | 135 | '#type' => 'fieldset', |
135 | 136 | '#title' => $this->t('Certificate'), |
136 | 137 | '#tree' => TRUE, |
| 138 | + ]; |
| 139 | + |
| 140 | + $form[Settings::CERTIFICATE][Settings::CERTIFICATE_PROVIDER] = [ |
| 141 | + '#type' => 'select', |
| 142 | + '#title' => $this->t('Provider'), |
| 143 | + '#options' => [ |
| 144 | + Settings::PROVIDER_TYPE_FORM => $this->t('Form'), |
| 145 | + Settings::PROVIDER_TYPE_KEY => $this->t('Key'), |
| 146 | + ], |
| 147 | + '#default_value' => $this->settings->getEditableValue([Settings::CERTIFICATE, Settings::CERTIFICATE_PROVIDER]) ?? Settings::PROVIDER_TYPE_FORM, |
| 148 | + '#description' => $this->t('Specifies which provider to use'), |
| 149 | + ]; |
| 150 | + |
| 151 | + $form[Settings::CERTIFICATE][CertificateLocatorHelper::LOCATOR_TYPE] = [ |
| 152 | + '#type' => 'select', |
| 153 | + '#title' => $this->t('Certificate locator type'), |
| 154 | + '#options' => [ |
| 155 | + CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT => $this->t('Azure key vault'), |
| 156 | + CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM => $this->t('File system'), |
| 157 | + ], |
| 158 | + '#default_value' => $this->settings->getEditableValue([ |
| 159 | + Settings::CERTIFICATE, |
| 160 | + CertificateLocatorHelper::LOCATOR_TYPE, |
| 161 | + ]) ?? NULL, |
| 162 | + '#states' => [ |
| 163 | + 'visible' => [':input[name="certificate[certificate_provider]"]' => ['value' => Settings::PROVIDER_TYPE_FORM]], |
| 164 | + ], |
| 165 | + '#description' => $this->t('Specifies which locator to use'), |
| 166 | + ]; |
137 | 167 |
|
138 | | - Settings::KEY => [ |
139 | | - '#type' => 'key_select', |
140 | | - '#key_filters' => [ |
141 | | - 'type' => 'os2web_key_certificate', |
| 168 | + $form[Settings::CERTIFICATE][CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT] = [ |
| 169 | + '#type' => 'fieldset', |
| 170 | + '#title' => $this->t('Azure key vault'), |
| 171 | + '#states' => [ |
| 172 | + 'visible' => [ |
| 173 | + ':input[name="certificate[certificate_provider]"]' => ['value' => Settings::PROVIDER_TYPE_FORM], |
| 174 | + ':input[name="certificate[locator_type]"]' => ['value' => CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT], |
| 175 | + ], |
| 176 | + ], |
| 177 | + ]; |
| 178 | + |
| 179 | + $settings = [ |
| 180 | + CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_TENANT_ID => ['title' => $this->t('Tenant id')], |
| 181 | + CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_APPLICATION_ID => ['title' => $this->t('Application id')], |
| 182 | + CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_CLIENT_SECRET => ['title' => $this->t('Client secret')], |
| 183 | + CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_NAME => ['title' => $this->t('Name')], |
| 184 | + CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_SECRET => ['title' => $this->t('Secret')], |
| 185 | + CertificateLocatorHelper::LOCATOR_AZURE_KEY_VAULT_VERSION => ['title' => $this->t('Version')], |
| 186 | + ]; |
| 187 | + |
| 188 | + foreach ($settings as $key => $info) { |
| 189 | + $form[Settings::CERTIFICATE][CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT][$key] = [ |
| 190 | + '#type' => 'textfield', |
| 191 | + '#title' => $info['title'], |
| 192 | + '#default_value' => $this->settings->getEditableValue([ |
| 193 | + Settings::CERTIFICATE, |
| 194 | + CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT, |
| 195 | + $key, |
| 196 | + ]) ?? NULL, |
| 197 | + '#states' => [ |
| 198 | + 'required' => [ |
| 199 | + ':input[name="certificate[certificate_provider]"]' => ['value' => Settings::PROVIDER_TYPE_FORM], |
| 200 | + ':input[name="certificate[locator_type]"]' => ['value' => CertificateLocatorHelper::LOCATOR_TYPE_AZURE_KEY_VAULT], |
| 201 | + ], |
| 202 | + ], |
| 203 | + ]; |
| 204 | + } |
| 205 | + |
| 206 | + $form[Settings::CERTIFICATE][CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM] = [ |
| 207 | + '#type' => 'fieldset', |
| 208 | + '#title' => $this->t('File system'), |
| 209 | + '#states' => [ |
| 210 | + 'visible' => [ |
| 211 | + ':input[name="certificate[certificate_provider]"]' => ['value' => Settings::PROVIDER_TYPE_FORM], |
| 212 | + ':input[name="certificate[locator_type]"]' => ['value' => CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM], |
142 | 213 | ], |
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]), |
| 214 | + ], |
| 215 | + |
| 216 | + CertificateLocatorHelper::LOCATOR_FILE_SYSTEM_PATH => [ |
| 217 | + '#type' => 'textfield', |
| 218 | + '#title' => $this->t('Path'), |
| 219 | + '#default_value' => $this->settings->getEditableValue([ |
| 220 | + Settings::CERTIFICATE, |
| 221 | + CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM, |
| 222 | + CertificateLocatorHelper::LOCATOR_FILE_SYSTEM_PATH, |
| 223 | + ]) ?? NULL, |
| 224 | + '#states' => [ |
| 225 | + 'required' => [ |
| 226 | + ':input[name="certificate[certificate_provider]"]' => ['value' => Settings::PROVIDER_TYPE_FORM], |
| 227 | + ':input[name="certificate[locator_type]"]' => ['value' => CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM], |
| 228 | + ], |
| 229 | + ], |
| 230 | + ], |
| 231 | + ]; |
| 232 | + |
| 233 | + $form[Settings::CERTIFICATE][CertificateLocatorHelper::LOCATOR_PASSPHRASE] = [ |
| 234 | + '#type' => 'textfield', |
| 235 | + '#title' => $this->t('Passphrase'), |
| 236 | + '#default_value' => $this->settings->getEditableValue([ |
| 237 | + Settings::CERTIFICATE, |
| 238 | + CertificateLocatorHelper::LOCATOR_PASSPHRASE, |
| 239 | + ]) ?? '', |
| 240 | + '#states' => [ |
| 241 | + 'visible' => [ |
| 242 | + ':input[name="certificate[certificate_provider]"]' => ['value' => Settings::PROVIDER_TYPE_FORM], |
| 243 | + ], |
| 244 | + ], |
| 245 | + ]; |
| 246 | + |
| 247 | + $form[Settings::CERTIFICATE][Settings::PROVIDER_TYPE_KEY] = [ |
| 248 | + '#type' => 'key_select', |
| 249 | + '#key_filters' => [ |
| 250 | + 'type' => 'os2web_key_certificate', |
| 251 | + ], |
| 252 | + '#key_description' => FALSE, |
| 253 | + '#title' => $this->t('Key'), |
| 254 | + '#default_value' => $this->settings->getEditableValue([Settings::CERTIFICATE, Settings::PROVIDER_TYPE_KEY]), |
| 255 | + '#description' => $this->createDescription([Settings::CERTIFICATE, Settings::PROVIDER_TYPE_KEY]), |
| 256 | + '#states' => [ |
| 257 | + 'visible' => [':input[name="certificate[certificate_provider]"]' => ['value' => Settings::PROVIDER_TYPE_KEY]], |
| 258 | + 'required' => [':input[name="certificate[certificate_provider]"]' => ['value' => Settings::PROVIDER_TYPE_KEY]], |
148 | 259 | ], |
149 | 260 | ]; |
150 | 261 |
|
@@ -179,6 +290,26 @@ public function buildForm(array $form, FormStateInterface $form_state): array { |
179 | 290 | return $form; |
180 | 291 | } |
181 | 292 |
|
| 293 | + /** |
| 294 | + * {@inheritdoc} |
| 295 | + * |
| 296 | + * @phpstan-param array<string, mixed> $form |
| 297 | + */ |
| 298 | + public function validateForm(array &$form, FormStateInterface $form_state): void { |
| 299 | + $values = $form_state->getValues(); |
| 300 | + |
| 301 | + if (Settings::PROVIDER_TYPE_FORM === $values[Settings::CERTIFICATE][Settings::CERTIFICATE_PROVIDER]) { |
| 302 | + if (CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM === $values[Settings::CERTIFICATE][CertificateLocatorHelper::LOCATOR_TYPE]) { |
| 303 | + $path = $values[Settings::CERTIFICATE][CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM][CertificateLocatorHelper::LOCATOR_FILE_SYSTEM_PATH] ?? NULL; |
| 304 | + if (!file_exists($path)) { |
| 305 | + $form_state->setErrorByName('certificate][file_system][path', $this->t('Invalid certificate path: %path', ['%path' => $path])); |
| 306 | + } |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + parent::validateForm($form, $form_state); |
| 311 | + } |
| 312 | + |
182 | 313 | /** |
183 | 314 | * {@inheritdoc} |
184 | 315 | * |
|
0 commit comments