diff --git a/README.md b/README.md index 3c28ebc2ef82cdb89666df33eae7399765652eb9..b03fa1f960acea6da8251a3c94ba18c43212028b 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ https://symfony.com/doc/current/bundles/configuration.html ## Customization +TODO: Update + You can implement the [LDAPApiProviderInterface](https://gitlab.tugraz.at/dbp/relay/dbp-relay-base-person-connector-ldap-bundle/-/blob/main/src/API/LDAPApiProviderInterface.php) to customize how attributes are fetched from the LDAP server and assigned to the `Person` entity or what `Person` entities are fetched from certain external services like in the [ALMA Bundle](https://gitlab.tugraz.at/dbp/library/api-alma-bundle). diff --git a/src/API/LDAPApiProviderInterface.php b/src/API/LDAPApiProviderInterface.php deleted file mode 100644 index 51d9bc35e2277dcfce62e49412fbe5abbfa447d9..0000000000000000000000000000000000000000 --- a/src/API/LDAPApiProviderInterface.php +++ /dev/null @@ -1,20 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Dbp\Relay\BasePersonConnectorLdapBundle\API; - -use Dbp\Relay\BasePersonBundle\Entity\Person; - -interface LDAPApiProviderInterface -{ - /** - * Allows manipulation of the person with a hash array of $attributes at the end of "personFromUserItem". - */ - public function personFromUserItemPostHook(array $attributes, Person $person, bool $full = false); - - /** - * Allows to fetch a person for a services by service id. - */ - public function getPersonForExternalServiceHook(string $service, string $serviceID): Person; -} diff --git a/src/Event/PersonForExternalServiceEvent.php b/src/Event/PersonForExternalServiceEvent.php new file mode 100644 index 0000000000000000000000000000000000000000..cca8c7601e5e769efb2b0e197357fa5c29360ec7 --- /dev/null +++ b/src/Event/PersonForExternalServiceEvent.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 PersonForExternalServiceEvent extends Event +{ + public const NAME = 'dbp.relay.base_person_connector_ldap_bundle.person_for_external_service'; + + protected $service; + protected $serviceID; + protected $person; + + public function __construct(string $service, string $serviceID) + { + $this->service = $service; + $this->serviceID = $serviceID; + $this->person = null; + } + + public function getService(): string + { + return $this->service; + } + + public function getServiceID(): string + { + return $this->serviceID; + } + + public function setPerson(Person $person): void + { + $this->person = $person; + } + + public function getPerson(): ?Person + { + return $this->person; + } +} diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 9d2c37775e2a20a6910e8f1544a61ee15c988cf7..e574c6467fdad2cade8daaec2761138ee388c3a2 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -9,6 +9,3 @@ services: Dbp\Relay\AuthBundle\API\UserRolesInterface: '@Dbp\Relay\BasePersonConnectorLdapBundle\Service\CustomUserRoles' - - Dbp\Relay\BasePersonConnectorLdapBundle\API\LDAPApiProviderInterface: - '@Dbp\Relay\BasePersonConnectorLdapBundle\Service\DummyLDAPApiProvider' diff --git a/src/Service/DummyLDAPApiProvider.php b/src/Service/DummyLDAPApiProvider.php deleted file mode 100644 index a54c492c5f012bac8582801fcdeb3689540164cc..0000000000000000000000000000000000000000 --- a/src/Service/DummyLDAPApiProvider.php +++ /dev/null @@ -1,34 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Dbp\Relay\BasePersonConnectorLdapBundle\Service; - -use Dbp\Relay\BasePersonBundle\Entity\Person; -use Dbp\Relay\BasePersonConnectorLdapBundle\API\LDAPApiProviderInterface; -use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; - -class DummyLDAPApiProvider implements LDAPApiProviderInterface -{ - /** - * Allows manipulation of the person with a hash array of $attributes at the end of "personFromUserItem". - */ - public function personFromUserItemPostHook(array $attributes, Person $person, bool $full = false) - { - // For example, you can parse the date of birth from the LDAP attribute and set it to the person object. - -// $birthDate = $attributes['dateofbirth'][0]; -// $person->setBirthDate($birthDate); - } - - public function getPersonForExternalServiceHook(string $service, string $serviceID): Person - { - // For example, you can use the service and serviceID to get the person from some other service. - -// if ($service === 'SOME-SERVICE') { -// return getPersonFromSomeService($serviceID); -// } - - throw new BadRequestHttpException("Unknown service: $service"); - } -} diff --git a/src/Service/LDAPApi.php b/src/Service/LDAPApi.php index c659988876a74bda3d21af4d36ba35cf08996e76..245d3d30f6ae4d6b127ca9581c5591c889b7c8e5 100644 --- a/src/Service/LDAPApi.php +++ b/src/Service/LDAPApi.php @@ -15,7 +15,7 @@ use Adldap\Connections\ProviderInterface; 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\PersonForExternalServiceEvent; use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPostEvent; use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonFromUserItemPreEvent; use Dbp\Relay\CoreBundle\API\UserSessionInterface; @@ -70,17 +70,14 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface private $birthdayAttributeName; - private $ldapApiProvider; - /** @var EventDispatcherInterface */ private $dispatcher; - public function __construct(ContainerInterface $locator, LDAPApiProviderInterface $ldapApiProvider, EventDispatcherInterface $dispatcher) + public function __construct(ContainerInterface $locator, EventDispatcherInterface $dispatcher) { $this->ad = new Adldap(); $this->cacheTTL = 0; $this->currentPerson = null; - $this->ldapApiProvider = $ldapApiProvider; $this->locator = $locator; $this->deploymentEnv = 'production'; $this->dispatcher = $dispatcher; @@ -280,9 +277,6 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface } } - // Call post-processing hook - $this->ldapApiProvider->personFromUserItemPostHook($attributes, $person, $full); - $postEvent = new PersonFromUserItemPostEvent($attributes, $person, $full); $this->dispatcher->dispatch($postEvent, PersonFromUserItemPostEvent::NAME); @@ -323,7 +317,9 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface public function getPersonForExternalService(string $service, string $serviceID): Person { - $person = $this->ldapApiProvider->getPersonForExternalServiceHook($service, $serviceID); + $event = new PersonForExternalServiceEvent($service, $serviceID); + $this->dispatcher->dispatch($event, PersonForExternalServiceEvent::NAME); + $person = $event->getPerson(); if (!$person) { throw new BadRequestHttpException("Unknown service: $service"); diff --git a/src/TestUtils/DummyLDAPApiProvider.php b/src/TestUtils/DummyLDAPApiProvider.php deleted file mode 100644 index ffdf2816bd7cd029705c788bd16ac33f2d19c2db..0000000000000000000000000000000000000000 --- a/src/TestUtils/DummyLDAPApiProvider.php +++ /dev/null @@ -1,25 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils; - -use Dbp\Relay\BasePersonBundle\Entity\Person; -use Dbp\Relay\BasePersonConnectorLdapBundle\API\LDAPApiProviderInterface; -use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; - -class DummyLDAPApiProvider implements LDAPApiProviderInterface -{ - /** - * Allows manipulation of the person with a hash array of $attributes at the end of "personFromUserItem". - */ - public function personFromUserItemPostHook(array $attributes, Person $person, bool $full = false) - { - $person->setExtraData('test', 'my-test-string'); - } - - public function getPersonForExternalServiceHook(string $service, string $serviceID): Person - { - throw new BadRequestHttpException("Unknown service: $service"); - } -}