mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
Merge branch '139_demandeur' of gitlab.com:Chill-Projet/chill-bundles into 139_demandeur
This commit is contained in:
@@ -12,6 +12,8 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Chill\PersonBundle\Privacy\AccompanyingPeriodPrivacyEvent;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
|
||||
class AccompanyingCourseApiController extends ApiController
|
||||
{
|
||||
@@ -64,6 +66,50 @@ class AccompanyingCourseApiController extends ApiController
|
||||
return $this->json($participation);
|
||||
}
|
||||
|
||||
public function requestorApi($id, Request $request, string $_format): Response
|
||||
{
|
||||
/** @var AccompanyingPeriod $accompanyingPeriod */
|
||||
$action = 'requestor';
|
||||
$accompanyingPeriod = $this->getEntity($action, $id, $request);
|
||||
// a requestor may be a person or a thirdParty
|
||||
|
||||
$this->checkACL($action, $request, $_format, $accompanyingPeriod);
|
||||
$this->onPostCheckACL($action, $request, $_format, $accompanyingPeriod);
|
||||
|
||||
if (Request::METHOD_DELETE === $request->getMethod()) {
|
||||
$accompanyingPeriod->setRequestor(NULL);
|
||||
} elseif (Request::METHOD_POST === $request->getMethod()) {
|
||||
$requestor = null;
|
||||
$exceptions = [];
|
||||
foreach ([Person::class, ThirdParty::class] as $class) {
|
||||
try {
|
||||
$requestor = $this->getSerializer()
|
||||
->deserialize($request->getContent(), $class, $_format, []);
|
||||
} catch (RuntimeException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
}
|
||||
if ($requestor === null) {
|
||||
throw new BadRequestException('Could not find any person or requestor', 0, $exceptions[0]);
|
||||
}
|
||||
|
||||
$accompanyingPeriod->setRequestor($requestor);
|
||||
} else {
|
||||
throw new BadRequestException('method not supported');
|
||||
}
|
||||
|
||||
$errors = $this->validator->validate($accompanyingPeriod);
|
||||
|
||||
if ($errors->count() > 0) {
|
||||
// only format accepted
|
||||
return $this->json($errors);
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
return $this->json($accompanyingPeriod->getRequestor());
|
||||
}
|
||||
|
||||
protected function onPostCheckACL(string $action, Request $request, string $_format, $entity): ?Response
|
||||
{
|
||||
$this->eventDispatcher->dispatch(
|
||||
|
@@ -334,6 +334,18 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
|
||||
Request::METHOD_POST => \Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter::SEE,
|
||||
Request::METHOD_DELETE=> \Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter::SEE
|
||||
]
|
||||
],
|
||||
'requestor' => [
|
||||
'methods' => [
|
||||
Request::METHOD_POST => true,
|
||||
Request::METHOD_DELETE => true,
|
||||
Request::METHOD_GET => false,
|
||||
Request::METHOD_HEAD => false,
|
||||
],
|
||||
'roles' => [
|
||||
Request::METHOD_POST => \Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter::SEE,
|
||||
Request::METHOD_DELETE=> \Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter::SEE
|
||||
]
|
||||
]
|
||||
|
||||
]
|
||||
|
@@ -515,9 +515,9 @@ class AccompanyingPeriod
|
||||
return $this->requestorPerson;
|
||||
}
|
||||
|
||||
public function setRequestorPerson(Person $requestorPerson): self
|
||||
public function setRequestorPerson(Person $requestorPerson = null): self
|
||||
{
|
||||
$this->requestorPerson = ($this->requestorThirdParty === null) ? $requestorPerson : null;
|
||||
$this->requestorPerson = $requestorPerson;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -527,9 +527,9 @@ class AccompanyingPeriod
|
||||
return $this->requestorThirdParty;
|
||||
}
|
||||
|
||||
public function setRequestorThirdParty(ThirdParty $requestorThirdParty): self
|
||||
public function setRequestorThirdParty(ThirdParty $requestorThirdParty = null): self
|
||||
{
|
||||
$this->requestorThirdParty = ($this->requestorPerson === null) ? $requestorThirdParty : null;
|
||||
$this->requestorThirdParty = $requestorThirdParty;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -542,6 +542,37 @@ class AccompanyingPeriod
|
||||
return $this->requestorPerson ?? $this->requestorThirdParty;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set a requestor
|
||||
*
|
||||
* The requestor is either an instance of ThirdParty, or an
|
||||
* instance of Person
|
||||
*
|
||||
* @param $requestor Person|ThirdParty
|
||||
* @return self
|
||||
* @throw UnexpectedValueException if the requestor is not a Person or ThirdParty
|
||||
*
|
||||
*/
|
||||
public function setRequestor($requestor): self
|
||||
{
|
||||
if ($requestor instanceof Person) {
|
||||
$this->setRequestorThirdParty(NULL);
|
||||
$this->setRequestorPerson($requestor);
|
||||
} elseif ($requestor instanceof ThirdParty) {
|
||||
$this->setRequestorThirdParty($requestor);
|
||||
$this->setRequestorPerson(NULL);
|
||||
} elseif (NULL === $requestor) {
|
||||
$this->setRequestorPerson(NULL);
|
||||
$this->setRequestorThirdParty(NULL);
|
||||
} else {
|
||||
throw new \UnexpectedValueException("requestor is not an instance of Person or ThirdParty");
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function isRequestorAnonymous(): bool
|
||||
{
|
||||
return $this->requestorAnonymous;
|
||||
|
@@ -83,16 +83,16 @@ class PersonNormalizer implements
|
||||
public function denormalize($data, string $type, string $format = null, array $context = []): Person
|
||||
{
|
||||
if ($context[self::GET_PERSON] ?? true) {
|
||||
$id = $data['id'] ?? null;
|
||||
$id = $data['person_id'] ?? null;
|
||||
if (NULL === $id) {
|
||||
throw new RuntimeException("missing id into person object");
|
||||
throw new RuntimeException("missing person_id into person object");
|
||||
}
|
||||
}
|
||||
/** var Person $person */
|
||||
$person = $this->repository->findOneById($id);
|
||||
|
||||
if (NULL === $person) {
|
||||
return UnexpectedValueException("person id not found");
|
||||
throw new UnexpectedValueException("person id not found");
|
||||
}
|
||||
|
||||
return $person;
|
||||
|
@@ -24,6 +24,7 @@ namespace Chill\PersonBundle\Tests\Entity;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
|
||||
class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
@@ -104,4 +105,30 @@ class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
|
||||
$participation = $period->getOpenParticipationContainsPerson($person);
|
||||
$this->assertNull($participation);
|
||||
}
|
||||
|
||||
public function testRequestor()
|
||||
{
|
||||
$period = new AccompanyingPeriod(new \DateTime());
|
||||
$person = new Person();
|
||||
$thirdParty = new ThirdParty();
|
||||
|
||||
$this->assertNull($period->getRequestorThirdParty());
|
||||
$this->assertNull($period->getRequestorPerson());
|
||||
$this->assertNull($period->getRequestor());
|
||||
|
||||
$period->setRequestor($person);
|
||||
$this->assertNull($period->getRequestorThirdParty());
|
||||
$this->assertSame($person, $period->getRequestorPerson());
|
||||
$this->assertSame($person, $period->getRequestor());
|
||||
|
||||
$period->setRequestor($thirdParty);
|
||||
$this->assertNull($period->getRequestorPerson());
|
||||
$this->assertSame($thirdParty, $period->getRequestorThirdParty());
|
||||
$this->assertSame($thirdParty, $period->getRequestor());
|
||||
|
||||
$period->setRequestor(NULL);
|
||||
$this->assertNull($period->getRequestorThirdParty());
|
||||
$this->assertNull($period->getRequestorPerson());
|
||||
$this->assertNull($period->getRequestor());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user