diff --git a/src/API/LDAPApiProviderInterface.php b/src/API/LDAPApiProviderInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..29cd2649e1bd91102160649d9f22bed41ef6f6ec --- /dev/null +++ b/src/API/LDAPApiProviderInterface.php @@ -0,0 +1,28 @@ +<?php + +declare(strict_types=1); + +namespace Dbp\Relay\LdapPersonProviderBundle\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". + * + * @param array $attributes + * @param Person $person + * @param bool $full + */ + public function personFromUserItemPostHook(array $attributes, Person $person, bool $full = false); + + /** + * Allows to fetch a person for a services by service id. + * + * @param string $service + * @param string $serviceID + * @return Person + */ + public function getPersonForExternalServiceHook(string $service, string $serviceID): Person; +} diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 4bbb96b6568ef9be60380240636574f8edfa9916..b5eb3275b4dcf43ca331409c45f63f73452ee416 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -9,3 +9,6 @@ services: Dbp\Relay\AuthBundle\API\UserRolesInterface: '@Dbp\Relay\LdapPersonProviderBundle\Service\CustomUserRoles' + + Dbp\Relay\LdapPersonProviderBundle\API\LDAPApiProviderInterface: + '@Dbp\Relay\LdapPersonProviderBundle\Service\DummyLDAPApiProvider' diff --git a/src/Service/DummyLDAPApiProvider.php b/src/Service/DummyLDAPApiProvider.php new file mode 100644 index 0000000000000000000000000000000000000000..2733138a18aba5d71f42f34d0e9b5596e1ef01d3 --- /dev/null +++ b/src/Service/DummyLDAPApiProvider.php @@ -0,0 +1,33 @@ +<?php + +declare(strict_types=1); + +namespace Dbp\Relay\LdapPersonProviderBundle\Service; + +use Dbp\Relay\BasePersonBundle\Entity\Person; +use Dbp\Relay\LdapPersonProviderBundle\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 53092ea3430801fd5651b6035bb8a8e2352da7ac..075855eca13194d9b0fb9bcaacd99352ccd944f3 100644 --- a/src/Service/LDAPApi.php +++ b/src/Service/LDAPApi.php @@ -18,6 +18,7 @@ use Dbp\Relay\BasePersonBundle\Entity\Person; use Dbp\Relay\CoreBundle\API\UserSessionInterface; use Dbp\Relay\CoreBundle\Exception\ApiError; use Dbp\Relay\CoreBundle\Helpers\Tools as CoreTools; +use Dbp\Relay\LdapPersonProviderBundle\API\LDAPApiProviderInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\Container\ContainerInterface; use Psr\Log\LoggerAwareInterface; @@ -70,30 +71,17 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface private $birthdayAttributeName; - public function __construct(ContainerInterface $locator, ParameterBagInterface $params) + private $ldapApiProvider; + + public function __construct(ContainerInterface $locator, ParameterBagInterface $params, LDAPApiProviderInterface $ldapApiProvider) { $this->ad = new Adldap(); $this->cacheTTL = 0; $this->currentPerson = null; $this->params = $params; -// $this->providerConfig = [ -// 'hosts' => [$this->params->get('app.ldap.host') ?? ''], -// 'base_dn' => $this->params->get('app.ldap.base_dn') ?? '', -// 'username' => $this->params->get('app.ldap.username') ?? '', -// 'password' => $this->params->get('app.ldap.password') ?? '', -// 'use_tls' => true, -// ]; + $this->ldapApiProvider = $ldapApiProvider; $this->locator = $locator; $this->deploymentEnv = 'production'; - -// $this->setPersonCache(new FilesystemAdapter('app-core-auth-person', 60, (string) $this->params->get('app.cache.person-cache-path'))); -// $this->setLDAPCache(new FilesystemAdapter('app-core-ldap', 360, (string) $this->params->get('app.cache.ldap-cache-path')), 360); - -// $this->identifierAttributeName = $this->params->get('app.ldap.attributes.identifier') ?? 'cn'; -// $this->givenNameAttributeName = $this->params->get('app.ldap.attributes.given_name') ?? 'givenName'; -// $this->familyNameAttributeName = $this->params->get('app.ldap.attributes.family_name') ?? 'sn'; -// $this->emailAttributeName = $this->params->get('app.ldap.attributes.email') ?? ''; -// $this->birthdayAttributeName = $this->params->get('app.ldap.attributes.birthday') ?? ''; } public function setConfig(array $config) @@ -277,18 +265,17 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface } } - // TODO: Add code to decide what roles a user has (or just depend on the roles from CustomUserRoles) - $roles = ['ROLE_SCOPE_GREENLIGHT']; - $person->setExtraData('ldap-roles', $roles); - - // TODO: Allow injection of this setting - $campusOnlineIdentifierAttribute = (string) $this->params->get('app.campusonline.person.identifier') ?? ''; - - // Used in \Dbp\Relay\LdapPersonProviderBundle\Service\CampusonlinePersonPhotoProvider::getPhotoData - if ($campusOnlineIdentifierAttribute !== '' && $user->hasAttribute($campusOnlineIdentifierAttribute)) { - $person->setExtraData($campusOnlineIdentifierAttribute, $user->getAttribute($campusOnlineIdentifierAttribute)[0]); + // Remove all value with numeric keys + $attributes = []; + foreach($user->getAttributes() as $key => $value) { + if (!is_numeric($key)) { + $attributes[$key] = $value; + } } + // Call post-processing hook + $this->ldapApiProvider->personFromUserItemPostHook($attributes, $person, $full); + return $person; } @@ -326,7 +313,13 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface public function getPersonForExternalService(string $service, string $serviceID): Person { - throw new BadRequestHttpException("Unknown service: $service"); + $person = $this->ldapApiProvider->getPersonForExternalServiceHook($service, $serviceID); + + if (!$person) { + throw new BadRequestHttpException("Unknown service: $service"); + } + + return $person; } private function getUserSession(): UserSessionInterface