Skip to content
Snippets Groups Projects
Commit c05a5f05 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Remove person code and rename to relay-base-organization-bundle

parent c92db122
Branches
Tags v0.1.3
No related merge requests found
Pipeline #58182 passed
......@@ -2,10 +2,10 @@
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Entity;
namespace Dbp\Relay\BaseOrganizationBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Dbp\Relay\BaseBundle\Controller\GetOrganizationsByPerson;
use Dbp\Relay\BaseOrganizationBundle\Controller\GetOrganizationsByPerson;
use Symfony\Component\Serializer\Annotation\Groups;
/**
......@@ -14,7 +14,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
* "get" = {
* "path" = "/organizations",
* "openapi_context" = {
* "tags" = {"Base"},
* "tags" = {"BaseOrganization"},
* "parameters" = {
* {"name" = "lang", "in" = "query", "description" = "Language of result", "type" = "string", "enum" = {"de", "en"}, "example" = "de"}
* }
......@@ -26,7 +26,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
* "controller" = GetOrganizationsByPerson::class,
* "read" = false,
* "openapi_context" = {
* "tags" = {"Base"},
* "tags" = {"BaseOrganization"},
* "summary" = "Get the organizations related to a person.",
* "parameters" = {
* {"name" = "identifier", "in" = "path", "description" = "Id of person", "required" = true, "type" = "string", "example" = "vlts01"},
......@@ -40,7 +40,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
* "get" = {
* "path" = "/organizations/{identifier}",
* "openapi_context" = {
* "tags" = {"Base"},
* "tags" = {"BaseOrganization"},
* "parameters" = {
* {"name" = "identifier", "in" = "path", "description" = "orgUnitID of organization", "required" = true, "type" = "string", "example" = "1190-F2050"},
* {"name" = "lang", "in" = "query", "description" = "Language of result", "type" = "string", "enum" = {"de", "en"}, "example" = "de"}
......
......@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Entity;
namespace Dbp\Relay\BaseOrganizationBundle\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use Symfony\Component\Serializer\Annotation\Groups;
......
<?php
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(
* collectionOperations={
* "get" = {
* "path" = "/people",
* "openapi_context" = {
* "tags" = {"Base"},
* "parameters" = {
* {"name" = "search", "in" = "query", "description" = "Search for a person name", "type" = "string", "example" = "woody007"}
* }
* }
* },
* },
* itemOperations={
* "get" = {
* "path" = "/people/{identifier}",
* "openapi_context" = {
* "tags" = {"Base"},
* }
*
* },
* },
* iri="http://schema.org/Person",
* description="A person of the LDAP system",
* normalizationContext={
* "groups" = {"BasePerson:output"},
* "jsonld_embed_context" = true,
* }
* )
*/
class Person
{
use PersonTrait;
}
<?php
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use Symfony\Component\Serializer\Annotation\Groups;
trait PersonTrait
{
/**
* @ApiProperty(identifier=true)
* @Groups({"BasePerson:output"})
*
* @var string
*/
private $identifier;
/**
* @ApiProperty(iri="http://schema.org/givenName")
* @Groups({"BasePerson:output"})
*
* @var string
*/
private $givenName;
/**
* @var string
* @ApiProperty(iri="http://schema.org/familyName")
* @Groups({"BasePerson:output"})
*
* @var string
*/
private $familyName;
/**
* @ApiProperty(iri="http://schema.org/email")
* @Groups({"BasePerson:current-user", "BasePerson:extended-access"})
*
* @var string
*/
private $email;
/**
* @var array
* @Groups({"BasePerson:current-user"})
*/
private $roles;
/**
* @var string
* @ApiProperty(iri="http://schema.org/birthDate")
* @Groups({"BasePerson:current-user"})
*/
private $birthDate;
/**
* @var array
*/
private $extraData;
public function __construct()
{
$this->extraData = [];
$this->roles = [];
}
public function setIdentifier(string $identifier)
{
$this->identifier = $identifier;
}
public function getIdentifier(): ?string
{
return $this->identifier;
}
public function getGivenName(): ?string
{
return $this->givenName;
}
public function setGivenName(?string $givenName)
{
$this->givenName = $givenName;
}
public function getFamilyName(): ?string
{
return $this->familyName;
}
public function setFamilyName(?string $familyName)
{
$this->familyName = $familyName;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email)
{
$this->email = $email;
}
/**
* Allows attaching extra information to a Person object with
* some random key. You can get the value back via getExtraData().
*
* @param ?mixed $value
*/
public function setExtraData(string $key, $value): void
{
$this->extraData[$key] = $value;
}
/**
* @return ?mixed
*/
public function getExtraData(string $key)
{
return $this->extraData[$key] ?? null;
}
public function getRoles(): array
{
return $this->roles;
}
public function setRoles(array $roles)
{
$this->roles = $roles;
}
public function getBirthDate(): ?string
{
return $this->birthDate;
}
public function setBirthDate(string $birthDate)
{
$this->birthDate = $birthDate;
}
}
services:
Dbp\Relay\BaseBundle\Controller\:
Dbp\Relay\BaseOrganizationBundle\Controller\:
resource: '../../Controller'
autowire: true
autoconfigure: true
Dbp\Relay\BaseBundle\API\OrganizationProviderInterface:
'@Dbp\Relay\BaseBundle\Service\DummyOrganizationProvider'
Dbp\Relay\BaseBundle\Service\DummyOrganizationProvider:
autowire: true
autoconfigure: true
Dbp\Relay\BaseBundle\Service\DummyPersonProvider:
autowire: true
autoconfigure: true
Dbp\Relay\BaseBundle\API\PersonProviderInterface:
'@Dbp\Relay\BaseBundle\Service\DummyPersonProvider'
Dbp\Relay\BaseBundle\DataProvider\:
Dbp\Relay\BaseOrganizationBundle\DataProvider\:
resource: '../../DataProvider'
autowire: true
autoconfigure: true
Dbp\Relay\BaseBundle\Serializer\PersonAttributeNormalizer:
Dbp\Relay\BaseOrganizationBundle\API\OrganizationProviderInterface:
'@Dbp\Relay\BaseOrganizationBundle\Service\DummyOrganizationProvider'
Dbp\Relay\BaseOrganizationBundle\Service\DummyOrganizationProvider:
autowire: true
autoconfigure: true
\ No newline at end of file
# This alias exist so we can replace the service during tests
test.PersonProviderInterface:
alias: 'Dbp\Relay\BaseBundle\API\PersonProviderInterface'
public: true
<?php
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Serializer;
use Dbp\Relay\BaseBundle\Entity\Person;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
class PersonAttributeNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private const ALREADY_CALLED = 'LDAP_PERSON_ATTRIBUTE_NORMALIZER_CURRENT_USER_ALREADY_CALLED';
/**
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function normalize($object, $format = null, array $context = [])
{
// set the group "Person:current-user" for the current user
if ($this->isCurrentUser($object)) {
$context['groups'][] = 'BasePerson:current-user';
}
$context[self::ALREADY_CALLED] = true;
return $this->normalizer->normalize($object, $format, $context);
}
public function supportsNormalization($data, $format = null, array $context = [])
{
// Make sure we're not called twice
if (isset($context[self::ALREADY_CALLED])) {
return false;
}
return $data instanceof Person;
}
/**
* @param Person $object
*/
private function isCurrentUser($object): bool
{
$user = $this->security->getUser();
return $user ? $user->getUsername() === $object->getIdentifier() : false;
}
}
......@@ -2,11 +2,11 @@
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Service;
namespace Dbp\Relay\BaseOrganizationBundle\Service;
use Dbp\Relay\BaseBundle\API\OrganizationProviderInterface;
use Dbp\Relay\BaseBundle\Entity\Organization;
use Dbp\Relay\BaseBundle\Entity\Person;
use Dbp\Relay\BaseOrganizationBundle\API\OrganizationProviderInterface;
use Dbp\Relay\BaseOrganizationBundle\Entity\Organization;
use Dbp\Relay\BasePersonBundle\Entity\Person;
class DummyOrganizationProvider implements OrganizationProviderInterface
{
......
<?php
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Service;
use Dbp\Relay\BaseBundle\API\PersonProviderInterface;
use Dbp\Relay\BaseBundle\Entity\Person;
class DummyPersonProvider implements PersonProviderInterface
{
/**
* @var string|null
*/
private $currentIdentifier;
public function __construct()
{
$this->currentIdentifier = null;
}
public function getPersons(array $filters): array
{
$person = $this->getCurrentPerson();
if ($person !== null) {
return [$person];
}
return [];
}
public function getPersonsByNameAndBirthDate(string $givenName, string $familyName, string $birthDate): array
{
return [];
}
public function getPerson(string $id): Person
{
$person = new Person();
$person->setIdentifier($id);
$person->setGivenName('John');
$person->setFamilyName('Doe');
$person->setEmail('john.doe@example.com');
return $person;
}
public function getCurrentPerson(): ?Person
{
if ($this->currentIdentifier === null) {
return null;
}
return $this->getPerson($this->currentIdentifier);
}
public function getPersonForExternalService(string $service, string $serviceID): Person
{
return new Person();
}
public function setCurrentIdentifier(string $identifier): void
{
$this->currentIdentifier = $identifier;
}
public function getRolesForCurrentPerson(): array
{
if ($this->currentIdentifier === null) {
return [];
}
return $this->getCurrentPerson()->getRoles();
}
public function setRolesForCurrentPerson(array $roles): void
{
if ($this->currentIdentifier === null) {
return;
}
$this->getCurrentPerson()->setRoles($roles);
}
}
<?php
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\TestUtils;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use Dbp\Relay\BaseBundle\API\PersonProviderInterface;
use Dbp\Relay\BaseBundle\Entity\Person;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DummyPersonProvider implements PersonProviderInterface
{
/* @var Person */
private $person;
public function __construct($person)
{
$this->person = $person;
}
public function getPersons(array $filters): array
{
return [$this->person];
}
public function getPerson(string $id): Person
{
if ($id !== $this->person->getIdentifier()) {
throw new NotFoundHttpException();
}
return $this->person;
}
public function getCurrentPerson(): Person
{
return $this->person;
}
public function getPersonForExternalService(string $service, string $serviceID): Person
{
throw new ItemNotFoundException();
}
public function getPersonsByNameAndBirthDate(string $givenName, string $familyName, string $birthDate): array
{
return [];
}
public function setCurrentIdentifier(string $identifier): void
{
$this->person->setIdentifier($identifier);
}
public function getRolesForCurrentPerson(): array
{
return $this->person->getRoles();
}
public function setRolesForCurrentPerson(array $roles): void
{
$this->person->setRoles($roles);
}
}
......@@ -2,102 +2,18 @@
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Tests;
namespace Dbp\Relay\BaseOrganizationBundle\Tests;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
use Dbp\Relay\BaseBundle\Entity\Person;
use Dbp\Relay\BaseBundle\TestUtils\DummyPersonProvider;
use Dbp\Relay\CoreBundle\TestUtils\UserAuthTrait;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserInterface;
class ExtTest extends ApiTestCase
{
use UserAuthTrait;
private function withPerson(Client $client, UserInterface $user): Person
{
$person = new Person();
$person->setIdentifier($user->getUserIdentifier());
$person->setRoles($user->getRoles());
$personProvider = new DummyPersonProvider($person);
$container = $client->getContainer();
$container->set('test.PersonProviderInterface', $personProvider);
return $person;
}
public function testGetPersonNoAuth()
{
$client = $this->withUser('foobar', ['foo']);
$response = $client->request('GET', '/people/foobar');
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode());
}
public function testGetPersonWrongAuth()
{
$client = $this->withUser('foobar', [], '42');
$response = $client->request('GET', '/people/foobar', ['headers' => [
'Authorization' => 'Bearer NOT42',
]]);
$this->assertEquals(Response::HTTP_FORBIDDEN, $response->getStatusCode());
}
public function testGetPerson()
{
$client = $this->withUser('foobar', [], '42');
$user = $this->getUser($client);
$person = $this->withPerson($client, $user);
$person->setEmail('foo@bar.com');
$response = $client->request('GET', '/people/foobar', ['headers' => [
'Authorization' => 'Bearer 42',
]]);
$this->assertJson($response->getContent(false));
$data = json_decode($response->getContent(false), true, 512, JSON_THROW_ON_ERROR);
$this->assertEquals('/people/foobar', $data['@id']);
$this->assertEquals('foobar', $data['identifier']);
$this->assertEquals('foo@bar.com', $data['email']);
}
public function testResponseHeaders()
{
$client = $this->withUser('foobar', [], '42');
$response = $client->request('GET', '/people/foobar', ['headers' => [
'Authorization' => 'Bearer 42',
]]);
$header = $response->getHeaders();
// We extend the defaults with CORS related headers
$this->assertArrayHasKey('vary', $header);
$this->assertContains('Accept', $header['vary']);
$this->assertContains('Origin', $header['vary']);
$this->assertContains('Access-Control-Request-Headers', $header['vary']);
$this->assertContains('Access-Control-Request-Method', $header['vary']);
// Make sure we have etag caching enabled
$this->assertArrayHasKey('etag', $header);
}
public function testGetPersonRoles()
{
$client = $this->withUser('foobar', ['ROLE'], '42');
$user = $this->getUser($client);
$this->withPerson($client, $user);
$response = $client->request('GET', '/people/foobar', ['headers' => [
'Authorization' => 'Bearer 42',
]]);
$data = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
$this->assertEquals(['ROLE'], $data['roles']);
}
public function testAuthChecks()
{
$client = self::createClient();
$endpoints = [
'/people',
'/people/foo',
// FIXME: '/people/foo/organizations',
'/organizations',
'/organizations/foo',
......
......@@ -2,10 +2,11 @@
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Tests;
namespace Dbp\Relay\BaseOrganizationBundle\Tests;
use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle;
use Dbp\Relay\BaseBundle\DbpRelayBaseBundle;
use Dbp\Relay\BaseOrganizationBundle\DbpRelayBaseOrganizationBundle;
use Dbp\Relay\BasePersonBundle\DbpRelayBasePersonBundle;
use Dbp\Relay\CoreBundle\DbpRelayCoreBundle;
use Nelmio\CorsBundle\NelmioCorsBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
......@@ -29,7 +30,8 @@ class Kernel extends BaseKernel
yield new NelmioCorsBundle();
yield new MonologBundle();
yield new ApiPlatformBundle();
yield new DbpRelayBaseBundle();
yield new DbpRelayBasePersonBundle();
yield new DbpRelayBaseOrganizationBundle();
yield new DbpRelayCoreBundle();
}
......
......@@ -2,9 +2,9 @@
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Tests;
namespace Dbp\Relay\BaseOrganizationBundle\Tests;
use Dbp\Relay\BaseBundle\Entity\Organization;
use Dbp\Relay\BaseOrganizationBundle\Entity\Organization;
use PHPUnit\Framework\TestCase;
class OrganizationTest extends TestCase
......
<?php
declare(strict_types=1);
namespace Dbp\Relay\BaseBundle\Tests;
use Dbp\Relay\BaseBundle\Entity\Person;
use PHPUnit\Framework\TestCase;
class PersonTest extends TestCase
{
public function testExtraData()
{
$person = new Person();
$person->setExtraData('foo', 42);
$this->assertSame(42, $person->getExtraData('foo'));
$person->setExtraData('foo', [1]);
$this->assertSame([1], $person->getExtraData('foo'));
$this->assertSame(null, $person->getExtraData('nope'));
}
public function testRoles()
{
$person = new Person();
$this->assertSame([], $person->getRoles());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment