-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrganisationEventSubscriber.php
More file actions
73 lines (63 loc) · 2.01 KB
/
Copy pathOrganisationEventSubscriber.php
File metadata and controls
73 lines (63 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Drupal\os2forms_nemlogin_openid_connect\EventSubscriber;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\os2forms_nemlogin_openid_connect\Helper\WebformHelper;
use Drupal\os2forms_organisation\Event\OrganisationUserIdEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Organisation user id event subscriber.
*/
class OrganisationEventSubscriber implements EventSubscriberInterface {
/**
* The webform helper.
*
* @var \Drupal\os2forms_nemlogin_openid_connect\Helper\WebformHelper
*/
protected WebformHelper $webformHelper;
/**
* The constructor.
*/
public function __construct(WebformHelper $webformHelper) {
$this->webformHelper = $webformHelper;
}
/**
* Subscribed events.
*/
public static function getSubscribedEvents(): array {
return [
// If class does not exist this event subscriber is not initialized,
// meaning this will cause no error.
// @see https://www.php.net/manual/en/language.namespaces.importing.php#121045
// This is done to avoid a hard dependency on os2forms_organsation.
// Set priority high (100) to get this running early.
/* @phpstan-ignore-next-line */
OrganisationUserIdEvent::class => ['setOrganisationUserId', 100],
];
}
/**
* Attempts settings organisation user id.
*
* @phpstan-ignore-next-line
*/
public function setOrganisationUserId(OrganisationUserIdEvent $event): void {
// Check if id has already been set.
/* @phpstan-ignore-next-line */
if (!empty($event->getUserId())) {
return;
}
try {
// Use the plugin configured on the current webform.
$plugin = $this->webformHelper->getAuthProviderPlugin();
if ($plugin->isAuthenticated()) {
$userId = $plugin->fetchValue('nameidentifier');
if (!empty($userId)) {
/* @phpstan-ignore-next-line */
$event->setUserId($userId);
}
}
}
catch (PluginException $exception) {
return;
}
}
}