Forráskód Böngészése

Ajout de PersonAddressPostal et ajout d'ApiResource et groups de normalization

Vincent GUFFON 4 éve
szülő
commit
ec56dd40ab

+ 8 - 0
src/Entity/Core/Country.php

@@ -3,9 +3,17 @@ declare(strict_types=1);
 
 namespace App\Entity\Core;
 
+use ApiPlatform\Core\Annotation\ApiResource;
 use App\Repository\Core\CountryRepository;
 use Doctrine\ORM\Mapping as ORM;
 
+#[ApiResource(
+    collectionOperations: ['get'],
+    itemOperations: ['get'],
+    attributes:[
+        'pagination_enabled' => false
+    ]
+)]
 #[ORM\Entity(repositoryClass: CountryRepository::class)]
 class Country
 {

+ 25 - 4
src/Entity/Organization/OrganizationAddressPostal.php

@@ -9,25 +9,46 @@ use App\Repository\Organization\OrganizationAddressPostalRepository;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
 
-#[ApiResource]
+use Symfony\Component\Serializer\Annotation\Groups;
+
+#[ApiResource(
+    collectionOperations: [
+        "get" => ["security" => "is_granted('ROLE_ORGANIZATION_VIEW')"],
+        "post"
+    ],
+    itemOperations: [
+        "get" => ["security" => "is_granted('ROLE_ORGANIZATION_VIEW') and object.getOrganization().getId() == user.getOrganization().getId()"],
+        "put" => ["security" => "object.getOrganization().getId() == user.getOrganization().getId()"],
+        "delete" => ["security" => "object.getOrganization().getId() == user.getOrganization().getId()"],
+    ],
+    attributes: ["security" => "is_granted('ROLE_ORGANIZATION')"],
+    denormalizationContext: [
+        'groups' => ['address'],
+    ],
+    normalizationContext: [
+        'groups' => ['address'],
+    ],
+)]
 #[ORM\Entity(repositoryClass: OrganizationAddressPostalRepository::class)]
 class OrganizationAddressPostal
 {
     #[ORM\Id]
     #[ORM\Column]
     #[ORM\GeneratedValue]
+    #[Groups("address")]
     private ?int $id = null;
 
     #[ORM\ManyToOne(inversedBy: 'organizationAddressPostals')]
     #[ORM\JoinColumn(nullable: false)]
-    private ?Organization $organization = null;
+    private Organization $organization;
 
     #[ORM\OneToOne(inversedBy: 'organizationAddressPostal', cascade: ['persist', 'remove'])]
     #[ORM\JoinColumn(nullable: false)]
-    private ?AddressPostal $addressPostal = null;
+    private AddressPostal $addressPostal;
 
     #[ORM\Column(length: 255)]
-    #[Assert\Choice(callback: ['\App\Enum\Core\AddressPostalTypeEnum', 'toArray'], message: 'invalid-address-postal-type')]
+    #[Assert\Choice(callback: ['\App\Enum\Organization\AddressPostalOrganizationTypeEnum', 'toArray'], message: 'invalid-address-postal-type')]
+    #[Groups("address")]
     private string $type;
 
     public function getId(): ?int

+ 57 - 1
src/Entity/Person/Person.php

@@ -13,11 +13,17 @@ use Doctrine\ORM\Mapping as ORM;
 use JetBrains\PhpStorm\Pure;
 use Symfony\Component\Security\Core\User\UserInterface;
 use Symfony\Component\Validator\Constraints as Assert;
+use Symfony\Component\Serializer\Annotation\Groups;
 
 /**
  * Personne physique ou morale
  */
-#[ApiResource]
+#[ApiResource(
+    collectionOperations: [],
+    itemOperations: [
+        "get" => ["security" => "is_granted('ROLE_USERS_VIEW')"],
+    ]
+)]
 #[ORM\Entity(repositoryClass: PersonRepository::class)]
 class Person implements UserInterface
 {
@@ -35,14 +41,20 @@ class Person implements UserInterface
     private ?string $password = null;
 
     #[ORM\Column(length: 255, nullable: true)]
+    #[Groups(["access_people_ref", "access_address"])]
     private ?string $name = null;
 
     #[ORM\Column(length: 255, nullable: true)]
+    #[Groups(["access_people_ref", "access_address"])]
     private ?string $givenName = null;
 
     #[ORM\ManyToMany(targetEntity: ContactPoint::class, mappedBy: 'person')]
     private Collection $contactPoints;
 
+    #[ORM\OneToMany( mappedBy: 'person', targetEntity: PersonAddressPostal::class, orphanRemoval: true)]
+    #[Groups("access_address")]
+    private Collection $personAddressPostal;
+
     #[ORM\Column(nullable: true)]
     #[Assert\Choice(callback: ['\App\Enum\Person\GenderEnum', 'toArray'], message: 'invalid-gender')]
     private ?string $gender = null;
@@ -51,9 +63,13 @@ class Person implements UserInterface
     #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
     private ?File $image = null;
 
+    #[ORM\Column(options: ['default' => true])]
+    private bool $isPhysical = true;
+
     #[Pure] public function __construct()
     {
         $this->contactPoints = new ArrayCollection();
+        $this->personAddressPostal = new ArrayCollection();
     }
 
     public function getId(): ?int
@@ -189,4 +205,44 @@ class Person implements UserInterface
     {
         return $this->image;
     }
+
+    public function getPersonAddressPostal(): Collection
+    {
+        return $this->personAddressPostal;
+    }
+
+    public function addPersonAddressPostal(PersonAddressPostal $personAddressPostal): self
+    {
+        if (!$this->personAddressPostal->contains($personAddressPostal)) {
+            $this->personAddressPostal[] = $personAddressPostal;
+            $personAddressPostal->setPerson($this);
+        }
+
+        return $this;
+    }
+
+    public function removePersonAddressPostal(PersonAddressPostal $personAddressPostal): self
+    {
+        if ($this->personAddressPostal->removeElement($personAddressPostal)) {
+            // set the owning side to null (unless already changed)
+            if ($personAddressPostal->getPerson() === $this) {
+                $personAddressPostal->setPerson(null);
+            }
+        }
+
+        return $this;
+    }
+
+    public function getIsPhysical(): ?bool
+    {
+        return $this->isPhysical;
+    }
+
+    public function setAdminAccess(bool $isPhysical): self
+    {
+        $this->isPhysical = $isPhysical;
+
+        return $this;
+    }
+
 }

+ 84 - 0
src/Entity/Person/PersonAddressPostal.php

@@ -0,0 +1,84 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Entity\Person;
+
+use ApiPlatform\Core\Annotation\ApiResource;
+use App\Entity\Core\AddressPostal;
+use App\Repository\Person\PersonAddressPostalRepository;
+use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Serializer\Annotation\Groups;
+use Symfony\Component\Validator\Constraints as Assert;
+
+/**
+ * Lien entre une Person et une AddressPostal
+ */
+#[ApiResource(
+    collectionOperations: [],
+    itemOperations: [
+        "get" => ["security" => "is_granted('ROLE_USERS_VIEW')"],
+    ]
+)]
+#[ORM\Entity(repositoryClass: PersonAddressPostalRepository::class)]
+class PersonAddressPostal
+{
+    #[ORM\Id]
+    #[ORM\Column]
+    #[ORM\GeneratedValue]
+    private ?int $id = null;
+
+    #[ORM\ManyToOne(inversedBy: 'personAddressPostal')]
+    #[ORM\JoinColumn(nullable: false)]
+    private Person $person;
+
+    #[ORM\OneToOne(inversedBy: 'personAddressPostal', cascade: ['persist', 'remove'])]
+    #[ORM\JoinColumn(nullable: false)]
+    #[Groups("access_address")]
+    private AddressPostal $addressPostal;
+
+    #[ORM\Column(length: 255)]
+    #[Assert\Choice(callback: ['\App\Enum\Person\AddressPostalPersonTypeEnum', 'toArray'], message: 'invalid-address-postal-type')]
+    #[Groups("access_address")]
+    private string $type;
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+    public function getPerson(): ?Person
+    {
+        return $this->person;
+    }
+
+    public function setPerson(Person $person): self
+    {
+        $this->person = $person;
+
+        return $this;
+    }
+
+    public function getAddressPostal(): ?AddressPostal
+    {
+        return $this->addressPostal;
+    }
+
+    public function setAddressPostal(AddressPostal $addressPostal): self
+    {
+        $this->addressPostal = $addressPostal;
+
+        return $this;
+    }
+
+    public function getType(): string
+    {
+        return $this->type;
+    }
+
+    public function setType(string $type): self
+    {
+        $this->type = $type;
+
+        return $this;
+    }
+}