Skip to content
Snippets Groups Projects
Commit 826eaf59 authored by Groß-Vogt, Tobias's avatar Groß-Vogt, Tobias
Browse files

adaptions for deprecation of email, birthDate and extraData attribute in BasePerson resource

parent 18fe3e8d
No related branches found
No related tags found
No related merge requests found
Pipeline #236365 passed
...@@ -358,11 +358,11 @@ ...@@ -358,11 +358,11 @@
}, },
{ {
"name": "dbp/relay-base-person-bundle", "name": "dbp/relay-base-person-bundle",
"version": "v0.2.13", "version": "v0.2.14",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://gitlab.tugraz.at/dbp/relay/dbp-relay-base-person-bundle", "url": "https://gitlab.tugraz.at/dbp/relay/dbp-relay-base-person-bundle",
"reference": "76dc3cf7a4ef16cee037739e0c612f63c29ef580" "reference": "2848187b29ac294fb5dcc65d53d2dc3ebc7252d5"
}, },
"require": { "require": {
"api-platform/core": "^2.6.3", "api-platform/core": "^2.6.3",
...@@ -403,7 +403,7 @@ ...@@ -403,7 +403,7 @@
"license": [ "license": [
"AGPL-3.0-or-later" "AGPL-3.0-or-later"
], ],
"time": "2023-03-23T09:35:04+00:00" "time": "2023-03-29T06:57:36+00:00"
}, },
{ {
"name": "dbp/relay-core-bundle", "name": "dbp/relay-core-bundle",
...@@ -11223,5 +11223,5 @@ ...@@ -11223,5 +11223,5 @@
"platform-overrides": { "platform-overrides": {
"php": "7.3" "php": "7.3"
}, },
"plugin-api-version": "2.3.0" "plugin-api-version": "2.2.0"
} }
...@@ -38,8 +38,10 @@ class Configuration implements ConfigurationInterface ...@@ -38,8 +38,10 @@ class Configuration implements ConfigurationInterface
->scalarNode('identifier')->end() ->scalarNode('identifier')->end()
->scalarNode('given_name')->end() ->scalarNode('given_name')->end()
->scalarNode('family_name')->end() ->scalarNode('family_name')->end()
->scalarNode('email')->end() ->scalarNode('email')->setDeprecated('dbp/relay-base-person-bundle', '0.2.14', 'The \'email\' attribute was removed from BasePerson resource. Use the local data mechanism to request a person\'s email')->end()
->scalarNode('birthday')->end() ->scalarNode('birthday')
->info('if the birthdate LDAP attribute name is specified here, its value is formatted to match the pattern \'YYYY-MM-DD\'')
->end()
->end(); ->end();
$ldapNode->append($attributesNode); $ldapNode->append($attributesNode);
......
...@@ -76,6 +76,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface ...@@ -76,6 +76,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
private $familyNameAttributeName; private $familyNameAttributeName;
/** @deprecated */
private $emailAttributeName; private $emailAttributeName;
private $birthdayAttributeName; private $birthdayAttributeName;
...@@ -217,6 +218,14 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface ...@@ -217,6 +218,14 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
$this->personCache = $cachePool; $this->personCache = $cachePool;
} }
/**
* For unit testing only.
*/
public function getEventDispatcher(): LocalDataEventDispatcher
{
return $this->eventDispatcher;
}
private function getProvider(): ProviderInterface private function getProvider(): ProviderInterface
{ {
if ($this->logger !== null) { if ($this->logger !== null) {
...@@ -310,7 +319,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface ...@@ -310,7 +319,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
$persons = []; $persons = [];
foreach ($this->getPeopleUserItems($currentPageNumber, $maxNumItemsPerPage, $options) as $userItem) { foreach ($this->getPeopleUserItems($currentPageNumber, $maxNumItemsPerPage, $options) as $userItem) {
$person = $this->personFromUserItem($userItem, false); $person = $this->personFromUserItem($userItem);
if ($person === null) { if ($person === null) {
continue; continue;
} }
...@@ -356,7 +365,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface ...@@ -356,7 +365,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
/** /**
* Returns null in case the user is not a valid Person, for example if the identifier is missing. * Returns null in case the user is not a valid Person, for example if the identifier is missing.
*/ */
public function personFromUserItem(User $user, bool $full): ?Person public function personFromUserItem(User $user): ?Person
{ {
$identifier = $user->getFirstAttribute($this->identifierAttributeName); $identifier = $user->getFirstAttribute($this->identifierAttributeName);
if ($identifier === null) { if ($identifier === null) {
...@@ -372,21 +381,21 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface ...@@ -372,21 +381,21 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
$person->setEmail($user->getFirstAttribute($this->emailAttributeName) ?? ''); $person->setEmail($user->getFirstAttribute($this->emailAttributeName) ?? '');
} }
$birthDateString = $this->birthdayAttributeName !== '' ? $attributes = [];
trim($user->getFirstAttribute($this->birthdayAttributeName) ?? '') : ''; foreach ($user->getAttributes() as $key => $value) {
// Remove all values with numeric keys
if (!is_numeric($key)) {
if ($this->birthdayAttributeName !== '' && $key === $this->birthdayAttributeName) {
$birthDateString = trim($user->getFirstAttribute($this->birthdayAttributeName) ?? '');
if ($birthDateString !== '') {
$matches = []; $matches = [];
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $birthDateString, $matches)) { if (preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $birthDateString, $matches)) {
$person->setBirthDate("{$matches[1]}-{$matches[2]}-{$matches[3]}"); $value = "{$matches[1]}-{$matches[2]}-{$matches[3]}";
} }
$person->setBirthDate($value);
} }
// Remove all value with numeric keys
$attributes = [];
foreach ($user->getAttributes() as $key => $value) {
if (!is_numeric($key)) {
$attributes[$key] = $value; $attributes[$key] = $value;
} }
} }
...@@ -417,7 +426,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface ...@@ -417,7 +426,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
assert($person !== null); assert($person !== null);
} else { } else {
$user = $this->getPersonUserItem($id); $user = $this->getPersonUserItem($id);
$person = $this->personFromUserItem($user, true); $person = $this->personFromUserItem($user);
if ($person === null) { if ($person === null) {
throw ApiError::withDetails(Response::HTTP_NOT_FOUND, sprintf("Person with id '%s' could not be found!", $id)); throw ApiError::withDetails(Response::HTTP_NOT_FOUND, sprintf("Person with id '%s' could not be found!", $id));
} }
...@@ -468,7 +477,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface ...@@ -468,7 +477,7 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface
} else { } else {
try { try {
$user = $this->getPersonUserItem($currentIdentifier); $user = $this->getPersonUserItem($currentIdentifier);
$person = $this->personFromUserItem($user, true); $person = $this->personFromUserItem($user);
} catch (ApiError $exc) { } catch (ApiError $exc) {
if ($exc->getStatusCode() !== Response::HTTP_NOT_FOUND) { if ($exc->getStatusCode() !== Response::HTTP_NOT_FOUND) {
throw $exc; throw $exc;
......
<?php
declare(strict_types=1);
namespace Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils;
use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonUserItemPreEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PersonUserItemSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
PersonUserItemPreEvent::class => 'onPre',
];
}
public function onPre(PersonUserItemPreEvent $event)
{
$identifier = $event->getIdentifier();
$event->setIdentifier($identifier);
}
}
...@@ -9,19 +9,23 @@ use Adldap\Models\User as AdldapUser; ...@@ -9,19 +9,23 @@ use Adldap\Models\User as AdldapUser;
use Adldap\Query\Builder; use Adldap\Query\Builder;
use Adldap\Query\Grammar; use Adldap\Query\Grammar;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use Dbp\Relay\BasePersonConnectorLdapBundle\EventSubscriber\PersonEventSubscriber;
use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPApi; use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPApi;
use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPPersonProvider; use Dbp\Relay\BasePersonConnectorLdapBundle\Service\LDAPPersonProvider;
use Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils\PersonEventSubscriber; use Dbp\Relay\BasePersonConnectorLdapBundle\Tests\TestUtils\TestPersonEventSubscriber;
use Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils\PersonUserItemSubscriber; use Dbp\Relay\CoreBundle\LocalData\LocalData;
use Mockery; use Mockery;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
class PersonTest extends ApiTestCase class PersonTest extends ApiTestCase
{ {
private const EMAIL_ATTRIBUTE_NAME = 'email';
private const BIRTHDATE_ATTRIBUTE_NAME = 'birthDate';
/** /**
* @var LDAPApi * @var LDAPApi
*/ */
private $api; private $ldapApi;
/** /**
* @var LDAPPersonProvider * @var LDAPPersonProvider
...@@ -31,14 +35,17 @@ class PersonTest extends ApiTestCase ...@@ -31,14 +35,17 @@ class PersonTest extends ApiTestCase
protected function setUp(): void protected function setUp(): void
{ {
parent::setUp(); parent::setUp();
$personFromUserItemSubscriber = new PersonEventSubscriber();
$personUserItemSubscriber = new PersonUserItemSubscriber(); $customPersonEventSubscriber = new TestPersonEventSubscriber();
$localDataEventSubscriber = new PersonEventSubscriber();
$localDataEventSubscriber->setConfig(self::createLocalDataMappingConfig());
$eventDispatcher = new EventDispatcher(); $eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber($personFromUserItemSubscriber); $eventDispatcher->addSubscriber($customPersonEventSubscriber);
$eventDispatcher->addSubscriber($personUserItemSubscriber); $eventDispatcher->addSubscriber($localDataEventSubscriber);
$this->api = new LDAPApi(self::createClient()->getContainer(), $eventDispatcher); $this->ldapApi = new LDAPApi(self::createClient()->getContainer(), $eventDispatcher);
$this->api->setConfig([ $this->ldapApi->setConfig([
'ldap' => [ 'ldap' => [
'encryption' => 'simple_tls', 'encryption' => 'simple_tls',
'attributes' => [ 'attributes' => [
...@@ -48,7 +55,7 @@ class PersonTest extends ApiTestCase ...@@ -48,7 +55,7 @@ class PersonTest extends ApiTestCase
], ],
]); ]);
$this->provider = new LDAPPersonProvider($this->api); $this->provider = new LDAPPersonProvider($this->ldapApi);
} }
public function testBasic() public function testBasic()
...@@ -72,13 +79,17 @@ class PersonTest extends ApiTestCase ...@@ -72,13 +79,17 @@ class PersonTest extends ApiTestCase
'sn' => ['Doe'], 'sn' => ['Doe'],
], $this->newBuilder()); ], $this->newBuilder());
$person = $this->api->personFromUserItem($user, false); $person = $this->ldapApi->personFromUserItem($user);
$this->assertEquals('John', $person->getGivenName()); $this->assertEquals('John', $person->getGivenName());
$this->assertEquals('Doe', $person->getFamilyName()); $this->assertEquals('Doe', $person->getFamilyName());
} }
public function testBirthDateParsing() public function testBirthDateParsing()
{ {
$options = [];
LocalData::requestLocalDataAttributes($options, [self::BIRTHDATE_ATTRIBUTE_NAME]);
$this->ldapApi->getEventDispatcher()->onNewOperation($options);
$variants = ['1994-06-24', '1994-06-24 00:00:00']; $variants = ['1994-06-24', '1994-06-24 00:00:00'];
foreach ($variants as $variant) { foreach ($variants as $variant) {
$user = new AdldapUser([ $user = new AdldapUser([
...@@ -88,13 +99,30 @@ class PersonTest extends ApiTestCase ...@@ -88,13 +99,30 @@ class PersonTest extends ApiTestCase
'sn' => ['familyName'], 'sn' => ['familyName'],
], $this->newBuilder()); ], $this->newBuilder());
$person = $this->api->personFromUserItem($user, false); $person = $this->ldapApi->personFromUserItem($user);
$this->assertNotNull($person->getBirthDate()); $this->assertEquals('1994-06-24', $person->getLocalDataValue(self::BIRTHDATE_ATTRIBUTE_NAME));
$this->assertEquals('1994-06-24', $person->getBirthDate());
} }
} }
public function testPersonFromUserItemPostEvent() public function testLocalDataAttributeEmail()
{
$options = [];
LocalData::requestLocalDataAttributes($options, [self::EMAIL_ATTRIBUTE_NAME]);
$this->ldapApi->getEventDispatcher()->onNewOperation($options);
$EMAIL = 'john@doe.com';
$user = new AdldapUser([
'cn' => ['johndoe'],
'givenName' => ['John'],
'sn' => ['Doe'],
'email' => [$EMAIL],
], $this->newBuilder());
$person = $this->ldapApi->personFromUserItem($user);
$this->assertEquals([$EMAIL], $person->getLocalDataValue(self::EMAIL_ATTRIBUTE_NAME));
}
public function testCustomPostEventSubscriber()
{ {
$user = new AdldapUser([ $user = new AdldapUser([
'cn' => ['foobar'], 'cn' => ['foobar'],
...@@ -102,9 +130,9 @@ class PersonTest extends ApiTestCase ...@@ -102,9 +130,9 @@ class PersonTest extends ApiTestCase
'sn' => ['familyName'], 'sn' => ['familyName'],
], $this->newBuilder()); ], $this->newBuilder());
$person = $this->api->personFromUserItem($user, false); $person = $this->ldapApi->personFromUserItem($user);
$this->assertEquals($person->getExtraData('test'), 'my-test-string'); $this->assertEquals('my-test-string', $person->getLocalDataValue('test'));
} }
public function testPersonFromUserItemNoIdentifier() public function testPersonFromUserItemNoIdentifier()
...@@ -114,7 +142,26 @@ class PersonTest extends ApiTestCase ...@@ -114,7 +142,26 @@ class PersonTest extends ApiTestCase
'sn' => ['familyName'], 'sn' => ['familyName'],
], $this->newBuilder()); ], $this->newBuilder());
$person = $this->api->personFromUserItem($user, false); $person = $this->ldapApi->personFromUserItem($user);
$this->assertNull($person); $this->assertNull($person);
} }
private static function createLocalDataMappingConfig(): array
{
$config = [];
$config['local_data_mapping'] = [
[
'local_data_attribute' => self::EMAIL_ATTRIBUTE_NAME,
'source_attribute' => self::EMAIL_ATTRIBUTE_NAME,
'default_value' => '',
],
[
'local_data_attribute' => self::BIRTHDATE_ATTRIBUTE_NAME,
'source_attribute' => 'dateofbirth',
'default_value' => '',
],
];
return $config;
}
} }
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
declare(strict_types=1); declare(strict_types=1);
namespace Dbp\Relay\BasePersonConnectorLdapBundle\TestUtils; namespace Dbp\Relay\BasePersonConnectorLdapBundle\Tests\TestUtils;
use Dbp\Relay\BasePersonBundle\Entity\Person; use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonPostEvent; use Dbp\Relay\BasePersonConnectorLdapBundle\Event\PersonPostEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PersonEventSubscriber implements EventSubscriberInterface class TestPersonEventSubscriber implements EventSubscriberInterface
{ {
public static function getSubscribedEvents(): array public static function getSubscribedEvents(): array
{ {
...@@ -21,7 +21,7 @@ class PersonEventSubscriber implements EventSubscriberInterface ...@@ -21,7 +21,7 @@ class PersonEventSubscriber implements EventSubscriberInterface
{ {
$person = $event->getEntity(); $person = $event->getEntity();
if ($person instanceof Person) { if ($person instanceof Person) {
$person->setExtraData('test', 'my-test-string'); $person->setLocalDataValue('test', 'my-test-string');
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment