|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\ApiProperty; |
| 17 | +use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\Get; |
| 19 | +use ApiPlatform\Metadata\Patch; |
| 20 | +use ApiPlatform\Metadata\Post; |
| 21 | +use Doctrine\ORM\Mapping as ORM; |
| 22 | + |
| 23 | +/** |
| 24 | + * Secured resource with properties depending on themselves. |
| 25 | + * |
| 26 | + * @author Loïc Boursin <[email protected]> |
| 27 | + */ |
| 28 | +#[ApiResource( |
| 29 | + operations: [ |
| 30 | + new Get(), |
| 31 | + new Patch(inputFormats: ['json' => ['application/merge-patch+json'], 'jsonapi']), |
| 32 | + new Post(security: 'is_granted("ROLE_ADMIN")'), |
| 33 | + ] |
| 34 | +)] |
| 35 | +#[ORM\Entity] |
| 36 | +class SecuredDummyWithPropertiesDependingOnThemselves |
| 37 | +{ |
| 38 | + #[ORM\Column(type: 'integer')] |
| 39 | + #[ORM\Id] |
| 40 | + #[ORM\GeneratedValue(strategy: 'AUTO')] |
| 41 | + private ?int $id = null; |
| 42 | + |
| 43 | + #[ORM\Column] |
| 44 | + private bool $canUpdateProperty = false; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var bool Special property, only writable when granted rights by another property |
| 48 | + */ |
| 49 | + #[ApiProperty(securityPostDenormalize: 'previous_object and previous_object.getCanUpdateProperty()')] |
| 50 | + #[ORM\Column] |
| 51 | + private bool $property = false; |
| 52 | + |
| 53 | + public function __construct() |
| 54 | + { |
| 55 | + } |
| 56 | + |
| 57 | + public function getId(): ?int |
| 58 | + { |
| 59 | + return $this->id; |
| 60 | + } |
| 61 | + |
| 62 | + public function getCanUpdateProperty(): bool |
| 63 | + { |
| 64 | + return $this->canUpdateProperty; |
| 65 | + } |
| 66 | + |
| 67 | + public function setCanUpdateProperty(bool $canUpdateProperty): void |
| 68 | + { |
| 69 | + $this->canUpdateProperty = $canUpdateProperty; |
| 70 | + } |
| 71 | + |
| 72 | + public function getProperty(): bool |
| 73 | + { |
| 74 | + return $this->property; |
| 75 | + } |
| 76 | + |
| 77 | + public function setProperty(bool $property): void |
| 78 | + { |
| 79 | + $this->property = $property; |
| 80 | + } |
| 81 | +} |
0 commit comments