src/Entity/User.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Ramsey\Uuid\UuidInterface as Uuid;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. /**
  12.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  13.  * @ORM\InheritanceType("SINGLE_TABLE")
  14.  * @ORM\DiscriminatorColumn(name="userType", type="string")
  15.  * @ORM\DiscriminatorMap({"external_user" = "ExternalUser", "employee" = "App\Entity\Employees\Employee"})
  16.  * @Vich\Uploadable
  17.  */
  18. class User implements UserInterface\Serializable
  19. {
  20.     /**
  21.      * @var Uuid
  22.      *
  23.      * @ORM\Column(name="id", type="uuid", nullable=false)
  24.      * @ORM\Id
  25.      * @ORM\GeneratedValue(strategy="CUSTOM")
  26.      * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
  27.      */
  28.     protected $id;
  29.     /**
  30.      * @ORM\Column(type="string", length=180, unique=true)
  31.      */
  32.     #[Groups('main')]
  33.     protected $email;
  34.     /**
  35.      * @ORM\Column(type="json")
  36.      */
  37.     protected $roles = [];
  38.     /**
  39.      * @ORM\Column(type="string", length=255)
  40.      */
  41.     #[Groups('main')]
  42.     protected $firstName;
  43.     /**
  44.      * @ORM\Column(type="string", length=255)
  45.      */
  46.     #[Groups('main')]
  47.     protected $lastName;
  48.     /**
  49.      * @var string|null
  50.      * @ORM\Column(name="display_name", type="string", length=255, nullable=true, options={"default"=null})
  51.      */
  52.     #[Groups(['employee:read''employee:write'])]
  53.     protected $displayName null;
  54.     /**
  55.      * @ORM\Column(type="string", length=255)
  56.      */
  57.     protected $password;
  58.     /**
  59.      * @ORM\OneToMany(targetEntity=UserPermission::class, mappedBy="permission_user", orphanRemoval=true)
  60.      */
  61.     protected $userPermissions;
  62.     /**
  63.      * @ORM\Column(type="datetime", nullable=true)
  64.      *
  65.      * @var \DateTimeInterface|null
  66.      */
  67.     #[Groups(['employee:read''employee:write'])]
  68.     protected $updatedAt;
  69.     /**
  70.      * @var string|null
  71.      * @ORM\Column(name="image_filename", type="string", length=2048, nullable=true, options={"default"=null})
  72.      */
  73.     #[Groups(['employee:read''employee:write'])]
  74.     protected $imageFilename;
  75.     /**
  76.      * @ORM\Column(type="integer", nullable=true)
  77.      *
  78.      * @var int|null
  79.      */
  80.     #[Groups(['employee:read''employee:write'])]
  81.     protected $imageSize;
  82.     /**
  83.      * NOTE: This is not a mapped field of entity metadata, just a simple property.
  84.      *
  85.      * @Vich\UploadableField(mapping="employee_avatar", fileNameProperty="imageFilename", size="imageSize")
  86.      *
  87.      * @var File|null
  88.      */
  89.     protected $imageFile;
  90.     /**
  91.      * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
  92.      * of 'UploadedFile' is injected into this setter to trigger the update. If this
  93.      * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
  94.      * must be able to accept an instance of 'File' as the bundle will inject one here
  95.      * during Doctrine hydration.
  96.      *
  97.      * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
  98.      *
  99.      * @throws \Exception
  100.      */
  101.     public function setImageFile(?File $imageFile null): void
  102.     {
  103.         $this->imageFile $imageFile;
  104.         if (null !== $imageFile) {
  105.             // It is required that at least one field changes if you are using doctrine
  106.             // otherwise the event listeners won't be called and the file is lost
  107.             $this->updatedAt = new \DateTimeImmutable();
  108.         }
  109.     }
  110.     public function getImageFile(): ?File
  111.     {
  112.         return $this->imageFile;
  113.     }
  114.     public function getImageFilename(): ?string
  115.     {
  116.         return $this->imageFilename;
  117.     }
  118.     public function setImageFilename(?string $imageFilename): User
  119.     {
  120.         $this->imageFilename $imageFilename;
  121.         return $this;
  122.     }
  123.     public function getImageSize(): ?int
  124.     {
  125.         return $this->imageSize;
  126.     }
  127.     public function setImageSize(?int $imageSize): User
  128.     {
  129.         $this->imageSize $imageSize;
  130.         return $this;
  131.     }
  132.     public function __construct()
  133.     {
  134.         $this->userPermissions = new ArrayCollection();
  135.     }
  136.     public function getId()
  137.     {
  138.         return $this->id;
  139.     }
  140.     public function getEmail(): ?string
  141.     {
  142.         return $this->email;
  143.     }
  144.     public function setEmail(string $email): self
  145.     {
  146.         $this->email $email;
  147.         return $this;
  148.     }
  149.     /**
  150.      * A visual identifier that represents this user.
  151.      *
  152.      * @see UserInterface
  153.      */
  154.     public function getUsername(): string
  155.     {
  156.         return (string) $this->email;
  157.     }
  158.     /**
  159.      * @see UserInterface
  160.      */
  161.     public function getRoles(): array
  162.     {
  163.         $roles $this->roles;
  164.         // guarantee every user at least has ROLE_USER
  165.         $roles[] = 'ROLE_USER';
  166.         return array_unique($roles);
  167.     }
  168.     public function setRoles(array $roles): self
  169.     {
  170.         $this->roles $roles;
  171.         return $this;
  172.     }
  173.     /**
  174.      * @see UserInterface
  175.      */
  176.     public function getPassword()
  177.     {
  178.         return $this->password;
  179.     }
  180.     /**
  181.      * @see UserInterface
  182.      */
  183.     public function getSalt(): ?string
  184.     {
  185.         // not needed when using bcrypt or argon
  186.         return null;
  187.     }
  188.     /**
  189.      * @see UserInterface
  190.      */
  191.     public function eraseCredentials()
  192.     {
  193.         // If you store any temporary, sensitive data on the user, clear it here
  194.         // $this->plainPassword = null;
  195.     }
  196.     public function getFirstName(): ?string
  197.     {
  198.         return $this->firstName;
  199.     }
  200.     public function setFirstName(string $firstName): self
  201.     {
  202.         $this->firstName $firstName;
  203.         return $this;
  204.     }
  205.     /**
  206.      * @return string|null
  207.      */
  208.     public function getLastName(): ?string
  209.     {
  210.         return $this->lastName;
  211.     }
  212.     /**
  213.      * @param mixed $lastName
  214.      *
  215.      * @return User
  216.      */
  217.     public function setLastName($lastName): self
  218.     {
  219.         $this->lastName $lastName;
  220.         return $this;
  221.     }
  222.     public function setPassword(string $password): self
  223.     {
  224.         $this->password $password;
  225.         return $this;
  226.     }
  227.     public function __toString()
  228.     {
  229.         return $this->getFirstName();
  230.     }
  231.     /**
  232.      * @return Collection|UserPermission[]
  233.      */
  234.     public function getUserPermissions(): Collection
  235.     {
  236.         return $this->userPermissions;
  237.     }
  238.     public function addUserPermission(UserPermission $userPermission): self
  239.     {
  240.         if (!$this->userPermissions->contains($userPermission)) {
  241.             $this->userPermissions[] = $userPermission;
  242.             $userPermission->setPermissionUser($this);
  243.         }
  244.         return $this;
  245.     }
  246.     public function removeUserPermission(UserPermission $userPermission): self
  247.     {
  248.         if ($this->userPermissions->removeElement($userPermission)) {
  249.             // set the owning side to null (unless already changed)
  250.             if ($userPermission->getPermissionUser() === $this) {
  251.                 $userPermission->setPermissionUser(null);
  252.             }
  253.         }
  254.         return $this;
  255.     }
  256.     /**
  257.      * @param mixed $userPermissions
  258.      *
  259.      * @return User
  260.      */
  261.     public function setUserPermissions($userPermissions)
  262.     {
  263.         $this->userPermissions $userPermissions;
  264.         return $this;
  265.     }
  266.     public function getUpdatedAt(): ?\DateTimeInterface
  267.     {
  268.         return $this->updatedAt;
  269.     }
  270.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): User
  271.     {
  272.         $this->updatedAt $updatedAt;
  273.         return $this;
  274.     }
  275.     /**
  276.      * @return string|null
  277.      */
  278.     public function getDisplayName()
  279.     {
  280.         if (is_null($this->displayName)) {
  281.             return $this->firstName.' '.$this->lastName;
  282.         }
  283.         return $this->displayName;
  284.     }
  285.     /**
  286.      * @param string|null $displayName
  287.      *
  288.      * @return User
  289.      */
  290.     public function setDisplayName($displayName)
  291.     {
  292.         $this->displayName $displayName;
  293.         return $this;
  294.     }
  295.     public function getEntityType()
  296.     {
  297.         return 'user';
  298.     }
  299.     public function serialize()
  300.     {
  301.         return serialize([
  302.             $this->id,
  303.             $this->email,
  304.             $this->password,
  305.             // see section on salt below
  306.             // $this->salt,
  307.         ]);
  308.     }
  309.     /**
  310.      * @see \Serializable::unserialize()
  311.      */
  312.     public function unserialize($serialized)
  313.     {
  314.         list(
  315.             $this->id,
  316.             $this->email,
  317.             $this->password,
  318.             // see section on salt below
  319.             // $this->salt
  320.             ) = unserialize($serialized);
  321.     }
  322. }