Skip to content
Snippets Groups Projects
Unverified Commit eb8d874b authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire:
Browse files

Add PersonForExternalServiceEvent and remove LDAPApiProviderInterface

parent 2a542ed6
No related branches found
No related tags found
No related merge requests found
Pipeline #83936 failed
......@@ -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).
......
<?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;
}
<?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;
}
}
......@@ -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'
<?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");
}
}
......@@ -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");
......
<?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");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment