|
5 | 5 | use ApiPlatform\Core\Annotation\ApiProperty;
|
6 | 6 | use ApiPlatform\Core\Annotation\ApiResource;
|
7 | 7 | use App\Entity\ContentNode;
|
| 8 | +use App\Repository\MultiSelectRepository; |
8 | 9 | use Doctrine\Common\Collections\ArrayCollection;
|
9 | 10 | use Doctrine\Common\Collections\Collection;
|
10 | 11 | use Doctrine\ORM\Mapping as ORM;
|
| 12 | +use Symfony\Component\Serializer\Annotation\Groups; |
11 | 13 |
|
12 | 14 | /**
|
13 |
| - * @ORM\Entity |
| 15 | + * @ORM\Entity(repositoryClass=MultiSelectRepository::class) |
14 | 16 | * @ORM\Table(name="content_node_multiselect")
|
15 |
| - * @ApiResource(routePrefix="/content_node")] |
16 | 17 | */
|
| 18 | +#[ApiResource( |
| 19 | + routePrefix: '/content_node', |
| 20 | + collectionOperations: [ |
| 21 | + 'get' => [ |
| 22 | + 'security' => 'is_fully_authenticated()', |
| 23 | + ], |
| 24 | + 'post' => [ |
| 25 | + 'denormalization_context' => ['groups' => ['write', 'create']], |
| 26 | + 'security_post_denormalize' => 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)', |
| 27 | + 'validation_groups' => ['Default', 'create'], |
| 28 | + ], |
| 29 | + ], |
| 30 | + itemOperations: [ |
| 31 | + 'get' => ['security' => 'is_granted("CAMP_COLLABORATOR", object) or is_granted("CAMP_IS_PROTOTYPE", object)'], |
| 32 | + 'patch' => [ |
| 33 | + 'denormalization_context' => ['groups' => ['write', 'update']], |
| 34 | + 'security' => 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)', |
| 35 | + 'validation_groups' => ['Default', 'update'], |
| 36 | + ], |
| 37 | + 'delete' => ['security' => '(is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)) and object.owner === null'], // disallow delete when contentNode is a root node |
| 38 | + ], |
| 39 | + denormalizationContext: ['groups' => ['write']], |
| 40 | + normalizationContext: ['groups' => ['read']], |
| 41 | +)] |
17 | 42 | class MultiSelect extends ContentNode {
|
18 | 43 | /**
|
19 | 44 | * @ORM\OneToMany(targetEntity="MultiSelectOption", mappedBy="multiSelect", orphanRemoval=true, cascade={"persist"})
|
20 | 45 | */
|
21 | 46 | #[ApiProperty(readableLink: true, writableLink: false)]
|
| 47 | + #[Groups(['read'])] |
22 | 48 | public Collection $options;
|
23 | 49 |
|
24 | 50 | public function __construct() {
|
25 | 51 | $this->options = new ArrayCollection();
|
26 | 52 |
|
27 | 53 | parent::__construct();
|
28 | 54 | }
|
| 55 | + |
| 56 | + /** |
| 57 | + * @return MultiSelectOption[] |
| 58 | + */ |
| 59 | + public function getOptions(): array { |
| 60 | + return $this->options->getValues(); |
| 61 | + } |
| 62 | + |
| 63 | + public function addOption(MultiSelectOption $option): self { |
| 64 | + if (!$this->options->contains($option)) { |
| 65 | + $this->options->add($option); |
| 66 | + $option->multiSelect = $this; |
| 67 | + } |
| 68 | + |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + public function removeOption(MultiSelectOption $option): self { |
| 73 | + if ($this->options->removeElement($option)) { |
| 74 | + if ($option->multiSelect === $this) { |
| 75 | + $option->multiSelect = null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $this; |
| 80 | + } |
29 | 81 | }
|
0 commit comments