diff --git a/composer.json b/composer.json index 80a5ca16accb38c70ddf1e633e32a587d2b6e62c..b4d007e8d8f3dd197ef0a1c029a35b9e3c8d5aab 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "dbp/relay-base-person-bundle": "^0.1.5", "guzzlehttp/guzzle": "^7.3", "league/uri": "^6.5", + "symfony/event-dispatcher": "^5.4", "symfony/framework-bundle": "^5.2" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 4776055eaa93bf39b35a93f0fc571eece8a51167..c10692bf8e070c6bcd2c197be2f11e5c4a36fa06 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7f54ebe0115a00d910c4fa039efaee3f", + "content-hash": "cbf75567a9a5c49eda7c8c2ef0ab9930", "packages": [ { "name": "adldap2/adldap2", @@ -9605,5 +9605,5 @@ "platform-overrides": { "php": "7.3" }, - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.1.0" } diff --git a/src/Event/PersonFromUserItemPostEvent.php b/src/Event/PersonFromUserItemPostEvent.php new file mode 100644 index 0000000000000000000000000000000000000000..a45ff926a84d256a94319a7c8821b74db5dad0e4 --- /dev/null +++ b/src/Event/PersonFromUserItemPostEvent.php @@ -0,0 +1,44 @@ +<?php + +declare(strict_types=1); + +namespace Dbp\Relay\BasePersonConnectorLdapBundle\Event; + +use Dbp\Relay\BasePersonBundle\Entity\Person; +use Symfony\Contracts\EventDispatcher\Event; + +class PersonFromUserItemPostEvent extends Event +{ + public const NAME = 'dbp.relay.base_person_connector_ldap_bundle.person_from_user_item.post'; + + protected $attributes; + protected $person; + protected $full; + + public function __construct(array $attributes, Person $person, bool $full) + { + $this->attributes = $attributes; + $this->person = $person; + $this->full = $full; + } + + public function getAttributes(): array + { + return $this->attributes; + } + + public function getPerson(): Person + { + return $this->person; + } + + public function isFull(): bool + { + return $this->full; + } + + public function setPerson(Person $person): void + { + $this->person = $person; + } +} diff --git a/src/Event/PersonFromUserItemPreEvent.php b/src/Event/PersonFromUserItemPreEvent.php new file mode 100644 index 0000000000000000000000000000000000000000..9bd0c3881063ffd5613def8c3f2ba51eba843b34 --- /dev/null +++ b/src/Event/PersonFromUserItemPreEvent.php @@ -0,0 +1,37 @@ +<?php + +declare(strict_types=1); + +namespace Dbp\Relay\BasePersonConnectorLdapBundle\Event; + +use Adldap\Models\User; +use Symfony\Contracts\EventDispatcher\Event; + +class PersonFromUserItemPreEvent extends Event +{ + public const NAME = 'dbp.relay.base_person_connector_ldap_bundle.person_from_user_item.pre'; + + protected $user; + protected $full; + + public function __construct(User $user, bool $full) + { + $this->user = $user; + $this->full = $full; + } + + public function getUser(): User + { + return $this->user; + } + + public function isFull(): bool + { + return $this->full; + } + + public function setUser(User $user): void + { + $this->user = $user; + } +} diff --git a/src/Service/LDAPApi.php b/src/Service/LDAPApi.php index dc3c906d4acce02f5803bcaf008aabcd40dfff88..c659988876a74bda3d21af4d36ba35cf08996e76 100644 --- a/src/Service/LDAPApi.php +++ b/src/Service/LDAPApi.php @@ -16,6 +16,8 @@ use Adldap\Models\User; use Adldap\Query\Builder; use Dbp\Relay\BasePersonBundle\Entity\Person; use Dbp\Relay\BasePersonConnectorLdapBundle\API\LDAPApiProviderInterface; +use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPostEvent; +use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPreEvent; use Dbp\Relay\CoreBundle\API\UserSessionInterface; use Dbp\Relay\CoreBundle\Exception\ApiError; use Dbp\Relay\CoreBundle\Helpers\Tools as CoreTools; @@ -24,6 +26,7 @@ use Psr\Container\ContainerInterface; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; use Symfony\Component\Cache\Psr16Cache; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -69,7 +72,10 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface private $ldapApiProvider; - public function __construct(ContainerInterface $locator, LDAPApiProviderInterface $ldapApiProvider) + /** @var EventDispatcherInterface */ + private $dispatcher; + + public function __construct(ContainerInterface $locator, LDAPApiProviderInterface $ldapApiProvider, EventDispatcherInterface $dispatcher) { $this->ad = new Adldap(); $this->cacheTTL = 0; @@ -77,6 +83,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface $this->ldapApiProvider = $ldapApiProvider; $this->locator = $locator; $this->deploymentEnv = 'production'; + $this->dispatcher = $dispatcher; } public function setConfig(array $config) @@ -239,6 +246,10 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface public function personFromUserItem(User $user, bool $full): Person { + $preEvent = new PersonFromUserItemPreEvent($user, $full); + $this->dispatcher->dispatch($preEvent, PersonFromUserItemPreEvent::NAME); + $user = $preEvent->getUser(); + $identifier = $user->getFirstAttribute($this->identifierAttributeName); $person = new Person(); @@ -272,7 +283,10 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface // Call post-processing hook $this->ldapApiProvider->personFromUserItemPostHook($attributes, $person, $full); - return $person; + $postEvent = new PersonFromUserItemPostEvent($attributes, $person, $full); + $this->dispatcher->dispatch($postEvent, PersonFromUserItemPostEvent::NAME); + + return $postEvent->getPerson(); } public function getRolesForCurrentPerson(): array