Vincent GUFFON vor 4 Jahren
Ursprung
Commit
2f1650b0d7

+ 48 - 0
src/Doctrine/Access/PersonalizedListExtension.php

@@ -0,0 +1,48 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Doctrine\Access;
+
+use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
+use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
+use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
+use App\Entity\Access\PersonalizedList;
+use Doctrine\ORM\QueryBuilder;
+use Symfony\Component\Security\Core\Security;
+
+/**
+ * Class PersonalizedListExtension : Filtre de sécurité par défaut pour une resource PersonalizedList
+ * @package App\Doctrine\Access
+ */
+final class PersonalizedListExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
+{
+    private Security $security;
+
+    public function __construct(Security $security)
+    {
+        $this->security = $security;
+    }
+
+    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void
+    {
+        $this->addWhere($queryBuilder, $resourceClass, $operationName);
+    }
+
+    public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void
+    {
+        $this->addWhere($queryBuilder, $resourceClass, $operationName);
+    }
+
+    private function addWhere(QueryBuilder $queryBuilder, string $resourceClass, string $operationName): void
+    {
+        if (PersonalizedList::class !== $resourceClass) {
+            return;
+        }
+
+        /** @var PersonalizedList $currentUser */
+        $currentUser = $this->security->getUser();
+        $rootAlias = $queryBuilder->getRootAliases()[0];
+        $queryBuilder->andWhere(sprintf('%s.access = :current_access', $rootAlias));
+        $queryBuilder->setParameter('current_access', $currentUser);
+    }
+}

+ 37 - 0
src/Entity/Access/Access.php

@@ -7,6 +7,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
 use ApiPlatform\Core\Annotation\ApiSubresource;
 use App\Entity\Organization\Organization;
 use App\Entity\Organization\OrganizationLicence;
+use App\Entity\Access\PersonalizedList;
 use App\Repository\Access\AccessRepository;
 use App\Entity\Person\Person;
 use App\Entity\Person\PersonActivity;
@@ -105,6 +106,11 @@ class Access implements UserInterface
      * @ORM\OneToMany(targetEntity=OrganizationLicence::class, mappedBy="licensee", orphanRemoval=true)
      */
     private $organizationLicences;
+    /**
+     * @var ArrayCollection<PersonalizedList>
+     * @ORM\OneToMany(targetEntity=PersonalizedList::class, mappedBy="access", orphanRemoval=true)
+     */
+    private $personalizedLists;
 
     /**
      * @var ArrayCollection<Access>
@@ -126,6 +132,7 @@ class Access implements UserInterface
         $this->personActivity = new ArrayCollection();
         $this->organizationFunction = new ArrayCollection();
         $this->organizationLicences = new ArrayCollection();
+        $this->personalizedLists = new ArrayCollection();
         $this->guardians = new ArrayCollection();
         $this->children = new ArrayCollection();
     }
@@ -359,6 +366,36 @@ class Access implements UserInterface
         return $this;
     }
 
+    /**
+     * @return Collection|PersonalizedList[]
+     */
+    public function getPersonalizedLists(): Collection
+    {
+        return $this->personalizedLists;
+    }
+
+    public function addPersonalizedList(PersonalizedList $personalizedList): self
+    {
+        if (!$this->personalizedLists->contains($personalizedList)) {
+            $this->personalizedLists[] = $personalizedList;
+            $personalizedList->setAccess($this);
+        }
+
+        return $this;
+    }
+
+    public function removePersonalizedList(PersonalizedList $personalizedList): self
+    {
+        if ($this->personalizedLists->removeElement($personalizedList)) {
+            // set the owning side to null (unless already changed)
+            if ($personalizedList->getAccess() === $this) {
+                $personalizedList->setAccess(null);
+            }
+        }
+
+        return $this;
+    }
+
     /**
      * @return Collection|Access[]
      */

+ 152 - 0
src/Entity/Access/PersonalizedList.php

@@ -0,0 +1,152 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Entity\Access;
+
+use ApiPlatform\Core\Annotation\ApiResource;
+use App\Repository\Access\PersonalizedListRepository;
+use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Serializer\Annotation\Groups;
+
+/**
+ * Liste personnalisées
+ *
+ * @ApiResource(
+ *     attributes={"pagination_enabled"=false},
+ *     collectionOperations={
+ *         "get"={"normalization_context"={"groups"={"lists:output"}}}
+ *     },
+ *     itemOperations={
+ *         "get"={"security"="object.getAccess().getId() == user.getId()"}
+ *     }
+ * )
+ * @ORM\Entity(repositoryClass=PersonalizedListRepository::class)
+ */
+class PersonalizedList
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     * @Groups({"lists:output"})
+     */
+    private $id;
+    /**
+     * @ORM\ManyToOne(targetEntity="Access", inversedBy="personalizedLists")
+     * @ORM\JoinColumn(nullable=false)
+     */
+    private $access;
+
+    /**
+     * @ORM\Column(type="string", length=200, nullable=true)
+     * @Groups({"lists:output"})
+     */
+    private $label;
+
+    /**
+     * @ORM\Column(type="json_array", length=4294967295, nullable=true)
+     */
+    private $filters;
+
+    /**
+     * @ORM\Column(type="string", length=150)
+     * @Groups({"lists:output"})
+     */
+    private $entity;
+
+
+    /**
+     * @ORM\Column(type="json_array", nullable=true)
+     */
+    private $columns;
+
+    /**
+     * @ORM\Column(type="string", length=150, nullable=true)
+     * @Groups({"lists:output"})
+     */
+    private $menuKey;
+
+    public function __construct()
+    {
+    }
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+
+    public function setFilters(array $filters):self
+    {
+        $this->filters = $filters;
+
+        return $this;
+    }
+
+    public function getFilters(): ?array
+    {
+        return $this->filters;
+    }
+
+    public function setColumns(array $columns):self
+    {
+        $this->columns = $columns;
+
+        return $this;
+    }
+
+    public function getColumns(): ?array
+    {
+        return $this->columns;
+    }
+
+
+    public function setAccess(Access $access):self
+    {
+        $this->access = $access;
+
+        return $this;
+    }
+
+    public function getAccess(): Access
+    {
+        return $this->access;
+    }
+
+    public function setMenuKey(string $menuKey):self
+    {
+        $this->menuKey = $menuKey;
+
+        return $this;
+    }
+
+    public function getMenuKey(): ?string
+    {
+        return $this->menuKey;
+    }
+
+    public function setLabel(string $label):self
+    {
+        $this->label = $label;
+
+        return $this;
+    }
+
+    public function getLabel(): ?string
+    {
+        return $this->label;
+    }
+
+    public function setEntity(string $entity):self
+    {
+        $this->entity = $entity;
+
+        return $this;
+    }
+
+    public function getEntity(): ?string
+    {
+        return $this->entity;
+    }
+}

+ 16 - 0
src/Repository/Access/PersonalizedListRepository.php

@@ -0,0 +1,16 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Repository\Access;
+
+use App\Entity\Access\PersonalizedList;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Persistence\ManagerRegistry;
+
+final class PersonalizedListRepository extends ServiceEntityRepository
+{
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, PersonalizedList::class);
+    }
+}