|
6 | 6 | use Drupal\Core\Messenger\MessengerInterface; |
7 | 7 | use Drupal\Core\Routing\RouteMatchInterface; |
8 | 8 | use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 9 | +use Drupal\os2web_nemlogin\Plugin\AuthProviderInterface; |
9 | 10 | use Drupal\os2web_nemlogin\Service\AuthProviderService; |
10 | 11 | use Drupal\webform\Utility\WebformFormHelper; |
11 | 12 | use Drupal\webform\WebformInterface; |
| 13 | +use Drupal\webform\WebformRequestInterface; |
12 | 14 | use Drupal\webform\WebformSubmissionInterface; |
13 | 15 | use Symfony\Component\Yaml\Yaml; |
14 | 16 |
|
@@ -41,13 +43,47 @@ class WebformHelper { |
41 | 43 | */ |
42 | 44 | private RouteMatchInterface $routeMatch; |
43 | 45 |
|
| 46 | + /** |
| 47 | + * The webform request handler. |
| 48 | + * |
| 49 | + * @var \Drupal\webform\WebformRequestInterface |
| 50 | + */ |
| 51 | + private WebformRequestInterface $webformRequest; |
| 52 | + |
44 | 53 | /** |
45 | 54 | * Constructor. |
46 | 55 | */ |
47 | | - public function __construct(AuthProviderService $authProviderService, MessengerInterface $messenger, RouteMatchInterface $routeMatch) { |
| 56 | + public function __construct(AuthProviderService $authProviderService, MessengerInterface $messenger, RouteMatchInterface $routeMatch, WebformRequestInterface $webformRequest) { |
48 | 57 | $this->authProviderService = $authProviderService; |
49 | 58 | $this->messenger = $messenger; |
50 | 59 | $this->routeMatch = $routeMatch; |
| 60 | + $this->webformRequest = $webformRequest; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get auth provider plugin for a webform. |
| 65 | + * |
| 66 | + * Resolves the plugin from the webform's NemID settings (session_type), |
| 67 | + * matching how os2forms_nemid resolves the plugin used for logging in, and |
| 68 | + * falls back to the site-wide active plugin. |
| 69 | + * |
| 70 | + * @param \Drupal\webform\WebformInterface|null $webform |
| 71 | + * The webform. If not set, the webform of the current request, if any, is |
| 72 | + * used. |
| 73 | + * |
| 74 | + * @return \Drupal\os2web_nemlogin\Plugin\AuthProviderInterface |
| 75 | + * The auth provider plugin. |
| 76 | + * |
| 77 | + * @throws \Drupal\Component\Plugin\Exception\PluginException |
| 78 | + */ |
| 79 | + public function getAuthProviderPlugin(?WebformInterface $webform = NULL): AuthProviderInterface { |
| 80 | + $webform ??= $this->webformRequest->getCurrentWebform(); |
| 81 | + $settings = $webform?->getThirdPartySetting('os2forms', 'os2forms_nemid'); |
| 82 | + $sessionType = $settings['session_type'] ?? NULL; |
| 83 | + |
| 84 | + return !empty($sessionType) |
| 85 | + ? $this->authProviderService->getPluginInstance($sessionType) |
| 86 | + : $this->authProviderService->getActivePlugin(); |
51 | 87 | } |
52 | 88 |
|
53 | 89 | /** |
@@ -131,7 +167,7 @@ private function checkAccess(WebformSubmissionInterface $webformSubmission, stri |
131 | 167 | $elementKey = $settings['element_key']; |
132 | 168 | $userClaim = $settings['user_claim']; |
133 | 169 |
|
134 | | - $plugin = $this->authProviderService->getActivePlugin(); |
| 170 | + $plugin = $this->getAuthProviderPlugin($webform); |
135 | 171 | if (!$plugin->isAuthenticated()) { |
136 | 172 | return (string) $this->t('Not authenticated'); |
137 | 173 | } |
@@ -163,7 +199,7 @@ public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterf |
163 | 199 | $webform = $formObject->getEntity(); |
164 | 200 | $settings = $webform->getThirdPartySetting('os2forms', 'os2forms_nemid'); |
165 | 201 |
|
166 | | - $options = $this->getUserClaimOptions(); |
| 202 | + $options = $this->getUserClaimOptions($webform); |
167 | 203 |
|
168 | 204 | $form['third_party_settings']['os2forms']['os2forms_nemid']['os2forms_nemlogin_openid_connect']['authentication_settings'] = [ |
169 | 205 | '#type' => 'fieldset', |
@@ -216,16 +252,19 @@ public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterf |
216 | 252 | /** |
217 | 253 | * Get user claim options. |
218 | 254 | * |
| 255 | + * @param \Drupal\webform\WebformInterface $webform |
| 256 | + * The webform. |
| 257 | + * |
219 | 258 | * @return array |
220 | 259 | * The user claim options. |
221 | 260 | * |
222 | 261 | * @phpstan-return array<string, mixed> |
223 | 262 | */ |
224 | | - private function getUserClaimOptions(): array { |
225 | | - $plugin = $this->authProviderService->getActivePlugin(); |
226 | | - $claims = $plugin->getConfiguration()['nemlogin_openid_connect_user_claims'] ?? ''; |
227 | | - |
| 263 | + private function getUserClaimOptions(WebformInterface $webform): array { |
228 | 264 | try { |
| 265 | + $plugin = $this->getAuthProviderPlugin($webform); |
| 266 | + $claims = $plugin->getConfiguration()['nemlogin_openid_connect_user_claims'] ?? ''; |
| 267 | + |
229 | 268 | $value = Yaml::parse($claims); |
230 | 269 | if (is_array($value)) { |
231 | 270 | asort($value); |
|
0 commit comments