src/DTOs/UserMe.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\DTOs;
  3. use App\Entity\Internal\TwoFaConfig;
  4. use App\Entity\Monolith\User;
  5. use App\Entity\Monolith\UserRole;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\Serializer\Annotation\Groups;
  9. class UserMe implements UserInterfacePasswordAuthenticatedUserInterface
  10. {
  11.     #[Groups(['user:read'])]
  12.     protected User $user;
  13.     #[Groups(['user:read'])]
  14.     protected array $kindergartens;
  15.     #[Groups(['user:read'])]
  16.     protected array $groups;
  17.     #[Groups(['user:read'])]
  18.     protected array $childrenIds;
  19.     #[Groups(['user:read'])]
  20.     protected string $type;
  21.     #[Groups(['user:read'])]
  22.     protected ?array $modulesAccess;
  23.     #[Groups(['user:read'])]
  24.     protected ?string $kgRole;
  25.     #[Groups(['user:read'])]
  26.     protected string $twoFaStatus;
  27.     protected int $kindergartenIdHeader;
  28.     protected ?string $userTypeHeader;
  29.     protected ?int $childIdHeader;
  30.     public static function map(array $user): self
  31.     {
  32.         $result = (new self())
  33.             ->setChildrenIds(array_map('intval'array_unique(array_filter(explode(','$user['my_children'])))))
  34.             ->setType($user['type'])
  35.             ->setModulesAccess(json_decode($user['modules_access'], true))
  36.             ->setKgRole($user['role_id'])
  37.         ;
  38.         unset($user['my_children'], $user['modules_access'], $user['type'], $user['role_id']);
  39.         $userEntity = new User();
  40.         foreach ($user as $field => $value) {
  41.             $setter 'set' implode(''array_map('ucfirst'explode('_'$field)));;
  42.             $userEntity->$setter($value);
  43.         }
  44.         return $result->setUser($userEntity);
  45.     }
  46.     public function getKgRole(): ?string
  47.     {
  48.         return $this->kgRole;
  49.     }
  50.     public function setKgRole(?string $kgRole): UserMe
  51.     {
  52.         $this->kgRole $kgRole;
  53.         return $this;
  54.     }
  55.     public function getKindergartens(): array
  56.     {
  57.         return $this->kindergartens;
  58.     }
  59.     public function setKindergartens(array $kindergartens): UserMe
  60.     {
  61.         $this->kindergartens $kindergartens;
  62.         return $this;
  63.     }
  64.     public function getGroups(): array
  65.     {
  66.         return $this->groups;
  67.     }
  68.     public function setGroups(array $groups): UserMe
  69.     {
  70.         $this->groups $groups;
  71.         return $this;
  72.     }
  73.     public function isAdmin(): bool
  74.     {
  75.         return UserRole::ROLE_ADMIN == $this->getType();
  76.     }
  77.     public function isManager(): bool
  78.     {
  79.         return UserRole::ROLE_ADMIN == $this->getType() &&
  80.             (empty($this->kgRole) || 'manager' == $this->kgRole);
  81.     }
  82.     public function getKindergartenIdHeader(): int
  83.     {
  84.         return $this->kindergartenIdHeader;
  85.     }
  86.     public function setKindergartenIdHeader(int $kindergartenIdHeader): UserMe
  87.     {
  88.         $this->kindergartenIdHeader $kindergartenIdHeader;
  89.         return $this;
  90.     }
  91.     public function getUserTypeHeader(): ?string
  92.     {
  93.         return $this->userTypeHeader;
  94.     }
  95.     public function setUserTypeHeader(?string $userTypeHeader): UserMe
  96.     {
  97.         $this->userTypeHeader $userTypeHeader;
  98.         return $this;
  99.     }
  100.     public function getChildIdHeader(): ?int
  101.     {
  102.         return $this->childIdHeader;
  103.     }
  104.     public function setChildIdHeader(?int $childIdHeader): UserMe
  105.     {
  106.         $this->childIdHeader $childIdHeader;
  107.         return $this;
  108.     }
  109.     public function getModulesAccess(): ?array
  110.     {
  111.         return $this->modulesAccess;
  112.     }
  113.     public function setModulesAccess(?array $modulesAccess): UserMe
  114.     {
  115.         $this->modulesAccess $modulesAccess;
  116.         return $this;
  117.     }
  118.     public function getUser(): User
  119.     {
  120.         return $this->user;
  121.     }
  122.     public function setUser(User $user): UserMe
  123.     {
  124.         $this->user $user;
  125.         return $this;
  126.     }
  127.     #[Groups(['user:read'])]
  128.     public function getKindergartenIds(): array
  129.     {
  130.         return array_keys($this->kindergartens);
  131.     }
  132.     #[Groups(['user:read'])]
  133.     public function getGroupIds(): array
  134.     {
  135.         return array_keys($this->groups);
  136.     }
  137.     public function getActiveGroupIds(): array
  138.     {
  139.         return array_keys($this->getActiveGroups());
  140.     }
  141.     public function getActiveGroups(): array
  142.     {
  143.         return array_filter($this->groups, function (array $group) {
  144.             return is_null($group[1]) || (strtotime($group[0]) <= time() && strtotime($group[1]) >= time());
  145.         });
  146.     }
  147.     public function getChildrenIds(): array
  148.     {
  149.         return $this->childrenIds;
  150.     }
  151.     public function setChildrenIds(array $childrenIds): UserMe
  152.     {
  153.         $this->childrenIds $childrenIds;
  154.         return $this;
  155.     }
  156.     public function getType(): string
  157.     {
  158.         return $this->type;
  159.     }
  160.     public function setType(string $type): UserMe
  161.     {
  162.         $this->type $type;
  163.         return $this;
  164.     }
  165.     public function getRoles(): array
  166.     {
  167.         $roles = ['ROLE_USER'];
  168.         return $roles;
  169.     }
  170.     public function eraseCredentials()
  171.     {
  172.     }
  173.     public function getUserIdentifier(): string
  174.     {
  175.         return $this->user->getEmail();
  176.     }
  177.     public function getPassword(): string
  178.     {
  179.         if (
  180.             isset($_SERVER['HTTP_X_SSO_KEY']) &&
  181.             $_ENV['APP_SECRET'] == $_SERVER['HTTP_X_SSO_KEY']
  182.         ) {
  183.             return $_ENV['SSO_MASTER_PASS'];
  184.         }
  185.         return $this->user->getPassword();
  186.     }
  187.     public function getTwoFaStatus(): string
  188.     {
  189.         return $this->twoFaStatus;
  190.     }
  191.     public function setTwoFaStatus(int $twoFaStatus): UserMe
  192.     {
  193.         if (!isset(TwoFaConfig::STATUS_MAP[$twoFaStatus])) {
  194.             throw new \Exception(sprintf("Status %s not allowed."$twoFaStatus));
  195.         }
  196.         $this->twoFaStatus TwoFaConfig::STATUS_MAP[$twoFaStatus];
  197.         return $this;
  198.     }
  199. }