浏览代码

add id and network to organization profile

Vincent GUFFON 4 年之前
父节点
当前提交
71af63fc7f

+ 6 - 6
src/ApiResources/Profile/AccessProfile.php

@@ -24,12 +24,12 @@ class AccessProfile
     /**
      * @ApiProperty(identifier=true)
      */
-    public int $id;
-    private bool $isAdminAccess;
-    private string $name;
-    private string $givenName;
-    private array $roles = [];
-    private OrganizationProfile $organization;
+    public $id;
+    private $isAdminAccess;
+    private $name;
+    private $givenName;
+    private $roles = [];
+    private $organization;
 
     public function __construct()
     {

+ 21 - 8
src/ApiResources/Profile/OrganizationProfile.php

@@ -14,14 +14,15 @@ class OrganizationProfile
     /**
      * @ApiProperty(identifier=true)
      */
-    public int $id;
-    private string $name;
-    private string $product;
-    private string $subDomain;
-    private string $website;
-    private array $modules = [];
-    private bool $hasChildren = false;
-    private array $parents = [];
+    public $id;
+    private $name;
+    private $product;
+    private $subDomain;
+    private $networks = [];
+    private $website;
+    private $modules = [];
+    private $hasChildren = false;
+    private $parents = [];
 
     public function __construct()
     {
@@ -63,6 +64,18 @@ class OrganizationProfile
         return $this;
     }
 
+    public function getNetworks(): array
+    {
+        return $this->networks;
+    }
+
+    public function addNetwork(?string $network): self
+    {
+        $this->networks[] = $network;
+
+        return $this;
+    }
+
     public function getSubDomain(): ?string
     {
         return $this->subDomain;

+ 5 - 1
src/DataProvider/Access/AccessProfileDataProvider.php

@@ -9,7 +9,6 @@ use App\ApiResources\Profile\AccessProfile;
 use App\ApiResources\Profile\OrganizationProfile;
 use App\Entity\Access\Access;
 use App\Entity\Organization\Organization;
-use App\Repository\Organization\OrganizationRepository;
 use App\Service\Network\Tree;
 use App\Service\Security\Module;
 use Symfony\Component\Security\Core\Role\Role;
@@ -80,6 +79,7 @@ final class AccessProfileDataProvider implements ItemDataProviderInterface, Rest
      */
     public function setOrganizationProfileFromOrganization(Organization $organization): OrganizationProfile{
         $organizationProfile = new OrganizationProfile();
+        $organizationProfile->setId($organization->getId());
         $organizationProfile->setName($organization->getName());
         $organizationProfile->setModules($this->module->getOrganizationModules($organization));
         $organizationProfile->setProduct($organization->getSettings()->getProduct());
@@ -87,6 +87,10 @@ final class AccessProfileDataProvider implements ItemDataProviderInterface, Rest
         $organizationProfile->setSubDomain($organization->getParameters()->getSubDomain());
         $organizationProfile->setWebsite($organization->getParameters()->getWebsite());
 
+        foreach ($organization->getNetworkOrganizations() as $networkOrganization){
+            $organizationProfile->addNetwork($networkOrganization->getNetwork()->getName());
+        }
+
         /** @var Organization $parent */
         foreach ($this->tree->findAllParentsAndSortByType($organization) as $parent){
             $parentProfile = new OrganizationProfile();

+ 3 - 6
src/Entity/Access/Access.php

@@ -18,12 +18,12 @@ use Symfony\Component\Security\Core\User\UserInterface;
  * Fais le lien entre une Person et une Organization
  * @ApiResource(
  *     collectionOperations={
- *         "get"={"security"="is_granted('ROLE_USER')"},
+ *         "get"={"security"="is_granted('ROLE_USERS')"},
  *         "post"
  *     },
  *     itemOperations={
- *         "get"={"security"="is_granted('ROLE_USER') or object.getId() == user.getId()"},
- *         "put"={"security"="is_granted('ROLE_USER')"},
+ *         "get"={"security"="is_granted('ROLE_USERS') or object.getId() == user.getId()"},
+ *         "put"={"security"="is_granted('ROLE_USERS')"},
  *         "delete"
  *     }
  * )
@@ -149,9 +149,6 @@ class Access implements UserInterface
     public function getRoles()
     {
         $roles = $this->roles;
-        // guarantee every user at least has ROLE_USER
-        $roles[] = 'ROLE_USER';
-
         return array_unique($roles);
     }
 

+ 1 - 6
src/Service/Network/Tree.php

@@ -49,12 +49,7 @@ class Tree
         ];
 
         usort($organizations, function(Organization $organization1, Organization $organization2) use($typeOrder){
-            $orderOrganization1 = array_keys($typeOrder, $organization1->getPrincipalType());
-            $orderOrganization2 = array_keys($typeOrder, $organization2->getPrincipalType());
-            if ($orderOrganization1 == $orderOrganization2) {
-                return 0;
-            }
-            return ($orderOrganization1 < $orderOrganization2) ? -1 : 1;
+            return array_keys($typeOrder, $organization1->getPrincipalType()) <=> array_keys($typeOrder, $organization2->getPrincipalType());
         });
 
         return $organizations;

+ 28 - 0
src/Service/Utils/Dates.php

@@ -0,0 +1,28 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Service\Utils;
+
+/**
+ * Class Dates : méthodes d'aide pour la gestion de dates.
+ * @package App\Service\Utils
+ */
+class Dates
+{
+    public function __construct()
+    {
+    }
+
+    /**
+     * Vérifie si la date du jour est comprise dans l'inerval passé en paramètres
+     * @param \DateTime $dateStart
+     * @param \DateTime $dateEnd
+     * @return bool
+     * @throws \Exception
+     * @see DatesTest::testIsIntervalIsValidNow()
+     */
+    public function isIntervalIsValidNow(\DateTime $dateStart, \DateTime $dateEnd = null): bool {
+        $now = new \DateTime('now');
+        return $dateStart <= $now && (is_null($dateEnd) || $dateEnd >= $now);
+    }
+}

+ 27 - 0
tests/Service/Utils/DatesTest.php

@@ -0,0 +1,27 @@
+<?php
+namespace App\Tests\Service\Utils;
+
+use App\Service\Utils\Dates;
+use PHPUnit\Framework\TestCase;
+
+class DatesTest extends TestCase
+{
+    /**
+     * @see Dates::isIntervalIsValidNow()
+     */
+    public function testIsIntervalIsValidNow():void
+    {
+        $dates = new Dates();
+        $this->assertTrue($dates->isIntervalIsValidNow(new \DateTime('2020-01-02'), new \DateTime('2025-01-02')));
+        $this->assertTrue($dates->isIntervalIsValidNow(new \DateTime('2020-01-02'), null));
+    }
+
+    /**
+     * @see Dates::isIntervalIsValidNow()
+     */
+    public function testIsIntervalIsNotValidNow():void
+    {
+        $dates = new Dates();
+        $this->assertFalse($dates->isIntervalIsValidNow(new \DateTime('2019-01-02'), new \DateTime('2020-01-02')));
+    }
+}