AccompanyingPeriodResource: fix deserialization + code style

This commit is contained in:
Julien Fastré 2022-01-10 23:08:23 +01:00
parent 8e012982f5
commit 2c4d06371c
8 changed files with 143 additions and 22 deletions

View File

@ -162,7 +162,7 @@ class ApiController extends AbstractCRUDController
} catch (NotEncodableValueException $e) {
throw new BadRequestException('invalid json', 400, $e);
}
dump($entity);
$errors = $this->validate($action, $request, $_format, $entity);
$response = $this->onAfterValidation($action, $request, $_format, $entity, $errors);
@ -177,7 +177,7 @@ class ApiController extends AbstractCRUDController
return $response;
}
dump('before flush');
$this->getDoctrine()->getManager()->flush();
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);
@ -351,6 +351,7 @@ class ApiController extends AbstractCRUDController
$default,
$this->getContextForSerialization($action, $request, $_format, $entity)
);
dump($context);
return $this->getSerializer()->deserialize($request->getContent(), $this->getEntityClass(), $_format, $context);
}

View File

@ -425,10 +425,11 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
Request::METHOD_PATCH => true,
Request::METHOD_HEAD => false,
Request::METHOD_DELETE => false,
],
], /*
'roles' => [
//Request::METHOD_PATCH => \Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter::SEE
],
*/
],
],
],

View File

@ -20,6 +20,8 @@ use Symfony\Component\Serializer\Annotation\Groups;
use UnexpectedValueException;
/**
* **About denormalization**: this operation is operated by @see{AccompanyingPeriodResourdeNormalizer}.
*
* @ORM\Entity
* @ORM\Table(
* name="chill_person_accompanying_period_resource",
@ -45,7 +47,7 @@ class Resource
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"read", "write"})
* @Groups({"read"})
*/
private ?string $comment = '';
@ -93,7 +95,7 @@ class Resource
/**
* @return Person|ThirdParty
* @Groups({"read", "write"})
* @Groups({"read"})
*/
public function getResource()
{

View File

@ -0,0 +1,46 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Security\Authorization;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use UnexpectedValueException;
class AccompanyingPeriodResourceVoter extends Voter
{
public const EDIT = 'CHILL_PERSON_ACCOMPANYING_PERIOD_RESOURCE_EDIT';
private AccessDecisionManagerInterface $accessDecisionManager;
protected function supports($attribute, $subject)
{
return $subject instanceof Resource && self::EDIT === $attribute;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var \Chill\PersonBundle\Entity\AccompanyingPeriod\Resource $subject */
switch ($attribute) {
case self::EDIT:
return $this->accessDecisionManager->decide(
$token,
AccompanyingPeriodVoter::EDIT,
$subject->getAccompanyingPeriod()
);
default:
throw new UnexpectedValueException("attribute not supported: {$attribute}");
}
}
}

View File

@ -38,15 +38,6 @@ class AccompanyingPeriodResourceNormalizer implements DenormalizerAwareInterface
$this->repository = $repository;
}
public function normalize($resource, $format = null, array $context = [])
{
return [
'type' => 'accompanying_period_resource',
'id' => $resource->getId(),
'comment' => $resource->getComment()
];
}
public function denormalize($data, $type, $format = null, array $context = [])
{
$resource = $this->extractObjectToPopulate($type, $context);
@ -93,9 +84,22 @@ class AccompanyingPeriodResourceNormalizer implements DenormalizerAwareInterface
$resource->setResource($res);
}
if (array_key_exists('comment', $data)) {
$resource->setComment($data['comment']);
}
return $resource;
}
public function normalize($resource, $format = null, array $context = [])
{
return [
'type' => 'accompanying_period_resource',
'id' => $resource->getId(),
'comment' => $resource->getComment(),
];
}
public function supportsDenormalization($data, $type, $format = null)
{
return Resource::class === $type;

View File

@ -0,0 +1,51 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Serializer\Normalizer;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* @internal
* @coversNothing
*/
final class ResourceJsonNormalizerTest extends KernelTestCase
{
private DenormalizerInterface $denormalizer;
protected function setUp(): void
{
self::bootKernel();
$this->denormalizer = self::$container->get(DenormalizerInterface::class);
}
public function testDenormalize()
{
$resource = new Resource();
$context = [
AbstractNormalizer::GROUPS => ['write'],
AbstractNormalizer::OBJECT_TO_POPULATE => $resource,
];
$json = [
'comment' => 'bloup',
'type' => 'accompanying_period_resource',
];
$resource = $this->denormalizer->denormalize($json, Resource::class, 'json', $context);
$this->assertEquals('bloup', $resource->getComment());
}
}

View File

@ -1,6 +1,7 @@
services:
chill.person.security.authorization.person:
autowire: true
autoconfigure: true
class: Chill\PersonBundle\Security\Authorization\PersonVoter
tags:
- { name: security.voter }
@ -8,11 +9,19 @@ services:
Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter:
autowire: true
autoconfigure: true
tags:
- { name: security.voter }
- { name: chill.role }
Chill\PersonBundle\Security\Authorization\AccompanyingPeriodCommentVoter:
autowire: true
autoconfigure: true
tags:
- { name: security.voter }
Chill\PersonBundle\Security\Authorization\AccompanyingPeriodResourceVoter:
autowire: true
autoconfigure: true
tags:
- { name: security.voter }

View File

@ -1,5 +1,12 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\Person;
@ -12,6 +19,14 @@ use Doctrine\Migrations\AbstractMigration;
*/
final class Version20220104133334 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_person_accompanying_period_resource ADD comment_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE chill_person_accompanying_period_resource DROP comment');
$this->addSql('ALTER TABLE chill_person_accompanying_period_resource ADD CONSTRAINT fk_dc78989ff8697d13 FOREIGN KEY (comment_id) REFERENCES chill_person_accompanying_period_comment (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX idx_dc78989ff8697d13 ON chill_person_accompanying_period_resource (comment_id)');
}
public function getDescription(): string
{
return 'Replace accompanyingCourse Resources comment by a string';
@ -24,12 +39,4 @@ final class Version20220104133334 extends AbstractMigration
$this->addSql('ALTER TABLE chill_person_accompanying_period_resource ADD comment TEXT DEFAULT NULL');
$this->addSql('ALTER TABLE chill_person_accompanying_period_resource DROP comment_id');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_person_accompanying_period_resource ADD comment_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE chill_person_accompanying_period_resource DROP comment');
$this->addSql('ALTER TABLE chill_person_accompanying_period_resource ADD CONSTRAINT fk_dc78989ff8697d13 FOREIGN KEY (comment_id) REFERENCES chill_person_accompanying_period_comment (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX idx_dc78989ff8697d13 ON chill_person_accompanying_period_resource (comment_id)');
}
}