mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
AccompanyingPeriodResource: fix deserialization + code style
This commit is contained in:
parent
8e012982f5
commit
2c4d06371c
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
],
|
||||
*/
|
||||
],
|
||||
],
|
||||
],
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
@ -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 }
|
||||
|
@ -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)');
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user