src/Entity/User.php line 27

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use App\Repository\UserRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Exception;
  9. use Scheb\TwoFactorBundle\Model\Totp\TotpConfigurationInterface;
  10. use Scheb\TwoFactorBundle\Model\Totp\TwoFactorInterface;
  11. use Scheb\TwoFactorBundle\Model\Totp\TotpConfiguration;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use Symfony\Component\Validator\Mapping\ClassMetadata;
  17. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  18. #[ORM\Entity(repositoryClassUserRepository::class)]
  19. #[UniqueEntity(fields: ['email'], message'There is already an account with this email')]
  20. #[UniqueEntity(fields: ['uuid'], message'There is already an account with this ID')]
  21. class User implements UserInterfacePasswordAuthenticatedUserInterfaceTwoFactorInterface
  22. {
  23.     public static function loadValidatorMetadata(ClassMetadata $metadata): void
  24.     {
  25.         $metadata->addPropertyConstraint('rawPassword', new Assert\NotCompromisedPassword());
  26.     }
  27.     private string $rawPassword;
  28.     /**
  29.      * @return string
  30.      */
  31.     public function getRawPassword():string
  32.     {
  33.         return $this->rawPassword;
  34.     }
  35.     public function setRawPassword($rawPassword):self
  36.     {
  37.         $this->rawPassword $rawPassword;
  38.         return $this;
  39.     }
  40.     #[ORM\Id]
  41.     #[ORM\GeneratedValue]
  42.     #[ORM\Column]
  43.     protected ?int $id null;
  44.     #[ORM\OneToOne(inversedBy"user"targetEntityUserDetails::class, cascade: ["persist""remove"])]
  45.     #[ORM\JoinColumn(name"userdetails"nullablefalse)]
  46.     protected ?UserDetails $userdetails;
  47.     #[ORM\OneToOne(inversedBy"user"targetEntityUserRegister::class, cascade: ["persist""remove"])]
  48.     #[ORM\JoinColumn(name"register"nullabletrue)]
  49.     protected ?UserRegister $register;
  50.     /**
  51.      * @ORM\OneToMany(targetEntity="ProductLicense", mappedBy="user")
  52.      * @ORM\JoinTable(name="ProductLicense",
  53.      *  joinColumns={@ORM\JoinColumn(name="user_id",referencedColumnName="id")}
  54.      * )
  55.      */
  56.     protected Collection $productLicense;
  57.     #[ORM\Column(type"string",length255nullabletrue)]
  58.     private ?string $totpSecret null;
  59.     #[ORM\Column(length180uniquetrue)]
  60.     #[Groups(['lizenz:read'])]
  61.     private ?string $email null;
  62.     #[ORM\Column]
  63.     private array $roles = [];
  64.     /**
  65.      * @var ?string The hashed password
  66.      */
  67.     #[ORM\Column]
  68.     private ?string $password null;
  69.     #[ORM\Column(length36,uniquetrue)]
  70.     #[Groups(['lizenz:read'])]
  71.     private ?string $uuid;
  72.     #[ORM\Column(length255nullabletrue)]
  73.     private ?string $locale null;
  74.     #[ORM\OneToMany(mappedBy'user'targetEntityOAuth2UserConsent::class, orphanRemovaltrue)]
  75.     private Collection $oAuth2UserConsents;
  76.     /**
  77.      * @throws Exception
  78.      */
  79.     public function __construct()
  80.     {
  81.         $this->oAuth2UserConsents = new ArrayCollection();
  82.     }
  83.     /**
  84.      * @return Collection
  85.      */
  86.     public function getProductLicense(): Collection
  87.     {
  88.         return $this->productLicense;
  89.     }
  90.     /**
  91.      * @param Collection $productLicense
  92.      */
  93.     public function setProductLicense(Collection $productLicense): void
  94.     {
  95.         $this->productLicense $productLicense;
  96.     }
  97.     public function getId(): ?int
  98.     {
  99.         return $this->id;
  100.     }
  101.     public function getEmail(): ?string
  102.     {
  103.         return $this->email;
  104.     }
  105.     public function setEmail(string $email): self
  106.     {
  107.         $this->email $email;
  108.         return $this;
  109.     }
  110.     public function getSalt(): ?string
  111.     {
  112.         return null;
  113.     }
  114.     /**
  115.      * A visual identifier that represents this user.
  116.      *
  117.      * @see UserInterface
  118.      */
  119.     public function getUserIdentifier(): string
  120.     {
  121.       //  return $this->email;
  122.        return $this->uuid;
  123.     }
  124.     /**
  125.      * @see UserInterface
  126.      */
  127.     public function getRoles(): array
  128.     {
  129.         $roles $this->roles;
  130.         // guarantee every user at least has ROLE_USER
  131.         $roles[] = 'ROLE_USER';
  132.         return array_unique($roles);
  133.     }
  134.     public function setRoles(array $roles): self
  135.     {
  136.         $this->roles $roles;
  137.         return $this;
  138.     }
  139.     /**
  140.      * @see PasswordAuthenticatedUserInterface
  141.      */
  142.     public function getPassword(): string
  143.     {
  144.         return $this->password;
  145.     }
  146.     public function setPassword(string $password): self
  147.     {
  148.         $this->password $password;
  149.         return $this;
  150.     }
  151.     /**
  152.      * @see UserInterface
  153.      */
  154.     public function eraseCredentials()
  155.     {
  156.         // If you store any temporary, sensitive data on the user, clear it here
  157.         // $this->plainPassword = null;
  158.     }
  159.   /*  public function __toString()
  160.     {
  161.         $format = "User (id: %d,email: %s, role: %s, information: %s)\n";
  162.         return sprintf($format, $this->id, $this->email, $this->roles, $this->information);
  163.     }*/
  164.   public function getUserdetails(): ?UserDetails
  165.   {
  166.       return $this->userdetails;
  167.   }
  168.   public function setUserdetails(UserDetails $userdetails): self
  169.   {
  170.       $this->userdetails $userdetails;
  171.       return $this;
  172.   }
  173.   public function getLocale(): ?string
  174.   {
  175.       return $this->locale;
  176.   }
  177.   public function setLocale(string $locale): self
  178.   {
  179.       $this->locale $locale;
  180.       return $this;
  181.   }
  182.   public function getRegister(): ?UserRegister
  183.   {
  184.       return $this->register;
  185.   }
  186.   public function setRegister(?UserRegister $register): self
  187.   {
  188.       $this->register $register;
  189.       return $this;
  190.   }
  191.   /**
  192.    * @return Collection<int, OAuth2UserConsent>
  193.    */
  194.   public function getOAuth2UserConsents(): Collection
  195.   {
  196.       return $this->oAuth2UserConsents;
  197.   }
  198.   public function addOAuth2UserConsent(OAuth2UserConsent $oAuth2UserConsent): self
  199.   {
  200.       if (!$this->oAuth2UserConsents->contains($oAuth2UserConsent)) {
  201.           $this->oAuth2UserConsents->add($oAuth2UserConsent);
  202.           $oAuth2UserConsent->setUser($this);
  203.       }
  204.       return $this;
  205.   }
  206.   public function removeOAuth2UserConsent(OAuth2UserConsent $oAuth2UserConsent): self
  207.   {
  208.       if ($this->oAuth2UserConsents->removeElement($oAuth2UserConsent)) {
  209.           // set the owning side to null (unless already changed)
  210.           if ($oAuth2UserConsent->getUser() === $this) {
  211.               $oAuth2UserConsent->setUser(null);
  212.           }
  213.       }
  214.       return $this;
  215.   }
  216.   public function getUuid(): ?string
  217.   {
  218.       return $this->uuid;
  219.   }
  220.   public function setUuid(string $uuid): self
  221.   {
  222.       $this->uuid $uuid;
  223.       return $this;
  224.   }
  225.     public function isTotpAuthenticationEnabled(): bool
  226.     {
  227.         return (bool)$this->totpSecret;
  228.     }
  229.     public function getTotpAuthenticationUsername(): string
  230.     {
  231.         return $this->getUserIdentifier();
  232.     }
  233.     public function getTotpAuthenticationConfiguration(): ?TotpConfigurationInterface
  234.     {
  235.         return new TotpConfiguration($this->totpSecretTotpConfiguration::ALGORITHM_SHA1306);
  236.     }
  237.     public function getTotpSecret(): string
  238.     {
  239.         return $this->totpSecret ?? '';
  240.     }
  241.     public function setTotpSecret(?string $totpSecret): self
  242.     {
  243.         $this->totpSecret $totpSecret;
  244.         return $this;
  245.     }
  246. }