Skip to content

Commit 80c5f85

Browse files
authored
Merge pull request #31 from itk-dev/feature/correctly-resolve-used-provider-plugin
Resolved configured auth provider plugin
2 parents 10dc732 + b86fdd3 commit 80c5f85

4 files changed

Lines changed: 70 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
## [Unreleased]
1010

11+
## [2.5.1] 2026-07-16
12+
13+
* Resolved auth provider plugin from webform NemID settings (`session_type`)
14+
instead of always using the site-wide active plugin when setting
15+
organisation user id and when checking webform access.
16+
1117
## [2.5.0] 2026-07-14
1218

1319
* Use unique token keys for OIDC providers.
@@ -53,7 +59,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5359

5460
* Allowed multiple providers.
5561

56-
[Unreleased]: https://github.com/itk-dev/os2forms_nemlogin_openid_connect/compare/2.5.0...HEAD
62+
[Unreleased]: https://github.com/itk-dev/os2forms_nemlogin_openid_connect/compare/2.5.1...HEAD
63+
[2.5.1]: https://github.com/itk-dev/os2forms_nemlogin_openid_connect/compare/2.5.0...2.5.1
5764
[2.5.0]: https://github.com/itk-dev/os2forms_nemlogin_openid_connect/compare/2.4.1...2.5.0
5865
[2.4.1]: https://github.com/itk-dev/os2forms_nemlogin_openid_connect/compare/2.4.0...2.4.1
5966
[2.4.0]: https://github.com/itk-dev/os2forms_nemlogin_openid_connect/compare/2.3.1...2.4.0

os2forms_nemlogin_openid_connect.services.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ services:
99
- '@os2web_nemlogin.auth_provider'
1010
- '@messenger'
1111
- '@current_route_match'
12+
- '@webform.request'
1213

1314
Drupal\os2forms_nemlogin_openid_connect\Helper\Settings:
1415
arguments:
1516
- "@keyvalue"
1617

1718
Drupal\os2forms_nemlogin_openid_connect\EventSubscriber\OrganisationEventSubscriber:
1819
arguments:
19-
- '@os2web_nemlogin.auth_provider'
20+
- '@Drupal\os2forms_nemlogin_openid_connect\Helper\WebformHelper'
2021
tags:
2122
- { name: 'event_subscriber' }

src/EventSubscriber/OrganisationEventSubscriber.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Drupal\os2forms_nemlogin_openid_connect\EventSubscriber;
44

55
use Drupal\Component\Plugin\Exception\PluginException;
6+
use Drupal\os2forms_nemlogin_openid_connect\Helper\WebformHelper;
67
use Drupal\os2forms_organisation\Event\OrganisationUserIdEvent;
7-
use Drupal\os2web_nemlogin\Service\AuthProviderService;
88
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
99

1010
/**
@@ -13,17 +13,17 @@
1313
class OrganisationEventSubscriber implements EventSubscriberInterface {
1414

1515
/**
16-
* The OS2Web Nemlogin authorization provider.
16+
* The webform helper.
1717
*
18-
* @var \Drupal\os2web_nemlogin\Service\AuthProviderService
18+
* @var \Drupal\os2forms_nemlogin_openid_connect\Helper\WebformHelper
1919
*/
20-
protected AuthProviderService $authProvider;
20+
protected WebformHelper $webformHelper;
2121

2222
/**
2323
* The constructor.
2424
*/
25-
public function __construct(AuthProviderService $authProvider) {
26-
$this->authProvider = $authProvider;
25+
public function __construct(WebformHelper $webformHelper) {
26+
$this->webformHelper = $webformHelper;
2727
}
2828

2929
/**
@@ -54,11 +54,15 @@ public function setOrganisationUserId(OrganisationUserIdEvent $event): void {
5454
}
5555

5656
try {
57-
$plugin = $this->authProvider->getActivePlugin();
57+
// Use the plugin configured on the current webform.
58+
$plugin = $this->webformHelper->getAuthProviderPlugin();
5859

59-
if ($plugin->isAuthenticated() && !empty($plugin->fetchValue('nameidentifier'))) {
60-
/* @phpstan-ignore-next-line */
61-
$event->setUserId($plugin->fetchValue('nameidentifier'));
60+
if ($plugin->isAuthenticated()) {
61+
$userId = $plugin->fetchValue('nameidentifier');
62+
if (!empty($userId)) {
63+
/* @phpstan-ignore-next-line */
64+
$event->setUserId($userId);
65+
}
6266
}
6367
}
6468
catch (PluginException $exception) {

src/Helper/WebformHelper.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use Drupal\Core\Messenger\MessengerInterface;
77
use Drupal\Core\Routing\RouteMatchInterface;
88
use Drupal\Core\StringTranslation\StringTranslationTrait;
9+
use Drupal\os2web_nemlogin\Plugin\AuthProviderInterface;
910
use Drupal\os2web_nemlogin\Service\AuthProviderService;
1011
use Drupal\webform\Utility\WebformFormHelper;
1112
use Drupal\webform\WebformInterface;
13+
use Drupal\webform\WebformRequestInterface;
1214
use Drupal\webform\WebformSubmissionInterface;
1315
use Symfony\Component\Yaml\Yaml;
1416

@@ -41,13 +43,47 @@ class WebformHelper {
4143
*/
4244
private RouteMatchInterface $routeMatch;
4345

46+
/**
47+
* The webform request handler.
48+
*
49+
* @var \Drupal\webform\WebformRequestInterface
50+
*/
51+
private WebformRequestInterface $webformRequest;
52+
4453
/**
4554
* Constructor.
4655
*/
47-
public function __construct(AuthProviderService $authProviderService, MessengerInterface $messenger, RouteMatchInterface $routeMatch) {
56+
public function __construct(AuthProviderService $authProviderService, MessengerInterface $messenger, RouteMatchInterface $routeMatch, WebformRequestInterface $webformRequest) {
4857
$this->authProviderService = $authProviderService;
4958
$this->messenger = $messenger;
5059
$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();
5187
}
5288

5389
/**
@@ -131,7 +167,7 @@ private function checkAccess(WebformSubmissionInterface $webformSubmission, stri
131167
$elementKey = $settings['element_key'];
132168
$userClaim = $settings['user_claim'];
133169

134-
$plugin = $this->authProviderService->getActivePlugin();
170+
$plugin = $this->getAuthProviderPlugin($webform);
135171
if (!$plugin->isAuthenticated()) {
136172
return (string) $this->t('Not authenticated');
137173
}
@@ -163,7 +199,7 @@ public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterf
163199
$webform = $formObject->getEntity();
164200
$settings = $webform->getThirdPartySetting('os2forms', 'os2forms_nemid');
165201

166-
$options = $this->getUserClaimOptions();
202+
$options = $this->getUserClaimOptions($webform);
167203

168204
$form['third_party_settings']['os2forms']['os2forms_nemid']['os2forms_nemlogin_openid_connect']['authentication_settings'] = [
169205
'#type' => 'fieldset',
@@ -216,16 +252,19 @@ public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterf
216252
/**
217253
* Get user claim options.
218254
*
255+
* @param \Drupal\webform\WebformInterface $webform
256+
* The webform.
257+
*
219258
* @return array
220259
* The user claim options.
221260
*
222261
* @phpstan-return array<string, mixed>
223262
*/
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 {
228264
try {
265+
$plugin = $this->getAuthProviderPlugin($webform);
266+
$claims = $plugin->getConfiguration()['nemlogin_openid_connect_user_claims'] ?? '';
267+
229268
$value = Yaml::parse($claims);
230269
if (is_array($value)) {
231270
asort($value);

0 commit comments

Comments
 (0)