src/Entity/User.php line 33

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use App\Annotations\HideFromView;
  5. use App\Trait\EntityTrait;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use App\Repository\UserRepository;
  10. use Symfony\Component\Serializer\Annotation\Ignore;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use ApiPlatform\Action\NotFoundAction;
  14. use ApiPlatform\Metadata\Get;
  15. use ApiPlatform\Metadata\GetCollection;
  16. use ApiPlatform\Metadata\ApiResource;
  17. use Symfony\Component\Serializer\Annotation\MaxDepth;
  18. use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
  19. /**
  20.  * User entity
  21.  */
  22. #[ApiResource(
  23.     normalizationContext: ['enable_max_depth'=>true],
  24. )]
  25. #[ORM\Entity(repositoryClassUserRepository::class)]
  26. #[ORM\Table(name'`user`')]
  27. class User implements UserInterfacePasswordAuthenticatedUserInterfaceTwoFactorInterface
  28. {
  29.     use EntityTrait;
  30.     /**
  31.      * ID
  32.      */
  33.     #[ORM\Id]
  34.     #[ORM\GeneratedValue]
  35.     #[ORM\Column]
  36.     private ?int $id null;
  37.     /**
  38.      * @var string $username Max length 180, unique, not nullable
  39.      */
  40.     #[ORM\Column(length180uniquetrue)]
  41.     private ?string $username null;
  42.     // TODO: relation with UserRoles, OneToMany
  43.     /**
  44.      * The list of the user's roles
  45.      */
  46.     #[ORM\Column]
  47.     private array $roles = [];
  48.     /**
  49.      * @var string The hashed password
  50.      */
  51.     #[Ignore]
  52.     #[ORM\Column]
  53.     private ?string $password null;
  54.     /**
  55.      * @var string $apiToken Max length 255, unique, not nullable
  56.      */
  57.     #[ORM\Column(length255uniquetrue)]
  58.     private ?string $apiToken null;
  59.     /**
  60.      * @var string $email Max length 255, not nullable
  61.      */
  62.     #[ORM\Column(length255)]
  63.     private ?string $email null;
  64.     /**
  65.      * @var bool $enable Not nullable
  66.      */
  67.     #[ORM\Column]
  68.     private ?bool $enable null;
  69.     #[MaxDepth(1)]
  70.     #[ORM\ManyToMany(targetEntityUserGroup::class, mappedBy'users')]
  71.     private Collection $userGroups;
  72.     #[ORM\ManyToOne(inversedBy'users')]
  73.     private ?Channel $channel null;
  74.     #[MaxDepth(1)]
  75.     #[ORM\OneToOne(mappedBy'user'cascade: ['persist''remove'])]
  76.     private ?RefillSetting $refillSetting null;
  77.     
  78.     #[Ignore]
  79.     #[HideFromView]
  80.     #[MaxDepth(1)]
  81.     #[ORM\OneToMany(mappedBy'user'targetEntityRefill::class)]
  82.     private Collection $refills;
  83.     #[ORM\Column(length255)]
  84.     private ?string $authCode;
  85.     public function __construct()
  86.     {
  87.         $this->userGroups = new ArrayCollection();
  88.         $this->refills = new ArrayCollection();
  89.     }
  90.     /**
  91.      * Getter for the entity's id property
  92.      * 
  93.      * @return int The id property
  94.      */
  95.     public function getId(): ?int
  96.     {
  97.         return $this->id;
  98.     }
  99.     /**
  100.      * Setter for the entity's id property
  101.      * 
  102.      * @param $id The property's new value
  103.      * @return self
  104.      */
  105.     public function setId(int $id): self
  106.     {
  107.         $this->id $id;
  108.     
  109.         return $this;
  110.     }
  111.     /**
  112.      * Getter for the entity's username property
  113.      * 
  114.      * @return string The username property
  115.      */
  116.     public function getUsername(): ?string
  117.     {
  118.         return $this->username;
  119.     }
  120.     /**
  121.      * Setter for the entity's username property
  122.      * 
  123.      * @param $username The property's new value
  124.      * @return self
  125.      */
  126.     public function setUsername(string $username): self
  127.     {
  128.         $this->username $username;
  129.     
  130.         return $this;
  131.     }
  132.     /**
  133.      * A visual identifier that represents this user.
  134.      *
  135.      * @see UserInterface
  136.      */
  137.     public function getUserIdentifier(): string
  138.     {
  139.         return (string) $this->username;
  140.     }
  141.     /**
  142.      * @see UserInterface
  143.      */
  144.     public function getRoles(): array
  145.     {
  146.         $roles $this->roles;    // Roles belonging to the user
  147.    
  148.         foreach ($this->userGroups as $group)
  149.         {
  150.             foreach ($group->getRoles() as $groupRole)
  151.             {
  152.                 array_push($roles$groupRole);
  153.             }
  154.         }
  155.         
  156.         $roles[] = 'ROLE_STDUSER';    // guarantee every user at least has ROLE_USER
  157.     
  158.         return array_unique($roles); // Instead of checking every element with in_array, I just remove duplicates at the end
  159.     }
  160.     /**
  161.      * Setter for the entity's roles property
  162.      * 
  163.      * @param $roles The property's new value
  164.      * @return self
  165.      */
  166.     public function setRoles(array $roles): self
  167.     {
  168.         $this->roles $roles;
  169.     
  170.         return $this;
  171.     }
  172.     /**
  173.      * @see PasswordAuthenticatedUserInterface
  174.      */
  175.     public function getPassword(): string
  176.     {
  177.         return $this->password;
  178.     }
  179.     /**
  180.      * Setter for the entity's password property
  181.      * 
  182.      * @param $password The property's new value
  183.      * @return self
  184.      */
  185.     public function setPassword(string $password): self
  186.     {
  187.         $this->password $password;
  188.     
  189.         return $this;
  190.     }
  191.     /**
  192.      * @see UserInterface
  193.      */
  194.     public function eraseCredentials()
  195.     {
  196.         // If you store any temporary, sensitive data on the user, clear it here
  197.         // $this->plainPassword = null;
  198.     }
  199.     /**
  200.      * Getter for the entity's apiToken property
  201.      * 
  202.      * @return string The apiToken property
  203.      */
  204.     public function getApiToken(): ?string
  205.     {
  206.         return $this->apiToken;
  207.     }
  208.     /**
  209.      * Setter for the entity's apiToken property
  210.      * 
  211.      * @param $apiToken The property's new value
  212.      * @return self
  213.      */
  214.     public function setApiToken(?string $apiToken): self
  215.     {
  216.         $this->apiToken $apiToken;
  217.     
  218.         return $this;
  219.     }
  220.     /**
  221.      * @return string|null
  222.      */
  223.     public function getAuthCode(): ?string
  224.     {
  225.         return $this->authCode;
  226.     }
  227.     /**
  228.      * @param string|null $authCode
  229.      */
  230.     public function setAuthCode(?string $authCode): void
  231.     {
  232.         $this->authCode $authCode;
  233.     }
  234.     /**
  235.      * Getter for the entity's email property
  236.      * 
  237.      * @return string The email property
  238.      */
  239.     public function getEmail(): ?string
  240.     {
  241.         return $this->email;
  242.     }
  243.     /**
  244.      * Setter for the entity's email property
  245.      * 
  246.      * @param $email The property's new value
  247.      * @return self
  248.      */
  249.     public function setEmail(string $email): self
  250.     {
  251.         $this->email $email;
  252.     
  253.         return $this;
  254.     }
  255.     /**
  256.      * Converts the entity to a string. Used to show a string that represents the entity in form fields
  257.      * 
  258.      * @return string The username
  259.      */
  260.     public function __toString()
  261.     {
  262.         return $this->username;
  263.     }
  264.     /**
  265.      * Getter for the entity's enable property
  266.      * 
  267.      * @return string The enable property
  268.      */
  269.     public function isEnable(): ?bool
  270.     {
  271.         return $this->enable;
  272.     }
  273.     /**
  274.      * Setter for the entity's enable property
  275.      * 
  276.      * @param $enable The property's new value
  277.      * @return self
  278.      */
  279.     public function setEnable(bool $enable): self
  280.     {
  281.         $this->enable $enable;
  282.         return $this;
  283.     }
  284.     /**
  285.      * @return Collection<int, UserGroup>
  286.      */
  287.     public function getUserGroups(): Collection
  288.     {
  289.         return $this->userGroups;
  290.     }
  291.     public function addUserGroup(UserGroup $userGroup): self
  292.     {
  293.         if (!$this->userGroups->contains($userGroup)) {
  294.             $this->userGroups->add($userGroup);
  295.             $userGroup->addUser($this);
  296.         }
  297.         return $this;
  298.     }
  299.     public function removeUserGroup(UserGroup $userGroup): self
  300.     {
  301.         if ($this->userGroups->removeElement($userGroup)) {
  302.             $userGroup->removeUser($this);
  303.         }
  304.         return $this;
  305.     }
  306.     /**
  307.      * Gets the total amount of tokens belonging to the user, not taking into consideration entity type and action
  308.      */
  309.     public function getBalance(): ?int
  310.     {
  311.         $balance 0;
  312.         foreach($this->refills as $refill)
  313.             $balance += ($refill->getQuantity() - $refill->getUsed());
  314.         return $balance;
  315.     }
  316.     public function getChannel(): ?Channel
  317.     {
  318.         return $this->channel;
  319.     }
  320.     public function setChannel(?Channel $channel): self
  321.     {
  322.         $this->channel $channel;
  323.         return $this;
  324.     }
  325.     public function getRefillSetting(): ?RefillSetting
  326.     {
  327.         return $this->refillSetting;
  328.     }
  329.     public function setRefillSetting(?RefillSetting $refillSetting): self
  330.     {
  331.         // unset the owning side of the relation if necessary
  332.         if ($refillSetting === null && $this->refillSetting !== null) {
  333.             $this->refillSetting->setUser(null);
  334.         }
  335.         // set the owning side of the relation if necessary
  336.         if ($refillSetting !== null && $refillSetting->getUser() !== $this) {
  337.             $refillSetting->setUser($this);
  338.         }
  339.         $this->refillSetting $refillSetting;
  340.         return $this;
  341.     }
  342.     /**
  343.      * @return Collection<int, Refill>
  344.      */
  345.     public function getRefills(): Collection
  346.     {
  347.         return $this->refills;
  348.     }
  349.     public function addRefill(Refill $refill): self
  350.     {
  351.         if (!$this->refills->contains($refill)) {
  352.             $this->refills->add($refill);
  353.             $refill->setUser($this);
  354.         }
  355.         return $this;
  356.     }
  357.     public function removeRefill(Refill $refill): self
  358.     {
  359.         if ($this->refills->removeElement($refill)) {
  360.             // set the owning side to null (unless already changed)
  361.             if ($refill->getUser() === $this) {
  362.                 $refill->setUser(null);
  363.             }
  364.         }
  365.         return $this;
  366.     }
  367.     public function isEmailAuthEnabled(): bool
  368.     {
  369.         return true// This can be a persisted field to switch email code authentication on/off
  370.     }
  371.     public function getEmailAuthRecipient(): string
  372.     {
  373.         return $this->email;
  374.     }
  375.     public function getEmailAuthCode(): ?string
  376.     {
  377.         /*
  378.         if (null === $this->authCode) {
  379.             throw new \LogicException('The email authentication code was not set');
  380.         }
  381.         */
  382.         return $this->authCode;
  383.     }
  384.     public function setEmailAuthCode(?string $authCode): void
  385.     {
  386.         $this->authCode $authCode;
  387.     }
  388. }