src/Entity/User.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
  8. use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
  9. use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
  10. use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. /**
  15.  * @ORM\Entity(repositoryClass=UserRepository::class)
  16.  * @ORM\Table(name="`user`")
  17.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  18.  */
  19. class User implements UserInterfacePasswordAuthenticatedUserInterfaceBlameableInterfaceTimestampableInterface
  20. {
  21.   use TimestampableTrait;
  22.   use BlameableTrait;
  23.   /**
  24.    * @ORM\Id
  25.    * @ORM\GeneratedValue
  26.    * @ORM\Column(type="integer")
  27.    */
  28.   private $id;
  29.   /**
  30.    * @ORM\Column(type="string", length=180, unique=true)
  31.    */
  32.   private $email;
  33.   /**
  34.    * @var $firstName string
  35.    * @ORM\Column(type="string", length=255)
  36.    */
  37.   private $firstName;
  38.   /**
  39.    * @var $lastName string
  40.    * @ORM\Column(type="string", length=255)
  41.    */
  42.   private $lastName;
  43.   /**
  44.    * @ORM\Column(type="json")
  45.    */
  46.   private $roles = [];
  47.   private $role;
  48.   /**
  49.    * @var string The hashed password
  50.    * @ORM\Column(type="string")
  51.    */
  52.   private $password;
  53.   /**
  54.    * @var int $contractualWeeklyHour
  55.    * @ORM\Column(type="integer", options={"default":39})
  56.    */
  57.   private int $contractualWeeklyHour 39;
  58.   /**
  59.    * @var $plainPassword string|null
  60.    */
  61.   private $plainPassword;
  62.   /**
  63.    * @ORM\Column(type="boolean")
  64.    */
  65.   private $isVerified false;
  66.   /**
  67.    * @ORM\Column(type="boolean", options={"default":true})
  68.    */
  69.   private $isActive true;
  70.   /**
  71.    * @ORM\OneToMany(targetEntity="Log", mappedBy="user")
  72.    */
  73.   private Collection $logs;
  74.   public function __construct() {
  75.     $this->logs = new ArrayCollection();
  76.   }
  77.   public static $ALL_ROLES = [
  78.     'ROLE_ADMIN',
  79.     'ROLE_CP',
  80.     'ROLE_USER',
  81.   ];
  82.   public function getId(): ?int
  83.   {
  84.     return $this->id;
  85.   }
  86.   public function getEmail(): ?string
  87.   {
  88.     return $this->email;
  89.   }
  90.   public function setEmail(string $email): self
  91.   {
  92.     $this->email $email;
  93.     return $this;
  94.   }
  95.   /**
  96.    * @return string
  97.    */
  98.   public function getFirstName(): string
  99.   {
  100.     return $this->firstName;
  101.   }
  102.   /**
  103.    * @param string $firstName
  104.    * @return User
  105.    */
  106.   public function setFirstName(string $firstName): self
  107.   {
  108.     $this->firstName $firstName;
  109.     return $this;
  110.   }
  111.   /**
  112.    * @return string
  113.    */
  114.   public function getLastName(): string
  115.   {
  116.     return $this->lastName;
  117.   }
  118.   /**
  119.    * @param string $lastName
  120.    * @return User
  121.    */
  122.   public function setLastName(string $lastName): self
  123.   {
  124.     $this->lastName $lastName;
  125.     return $this;
  126.   }
  127.   /**
  128.    * A visual identifier that represents this user.
  129.    *
  130.    * @see UserInterface
  131.    */
  132.   public function getUserIdentifier(): string
  133.   {
  134.     return (string)$this->email;
  135.   }
  136.   /**
  137.    * @deprecated since Symfony 5.3, use getUserIdentifier instead
  138.    */
  139.   public function getUsername(): string
  140.   {
  141.     return (string)$this->email;
  142.   }
  143.   /**
  144.    * @see UserInterface
  145.    */
  146.   public function getRoles(){
  147.     return $this->roles;
  148.   }
  149.   /**
  150.    * @param null $roles
  151.    * @return $this
  152.    */
  153.   public function setRoles($roles null){
  154.     $this->roles = !empty($roles) ? $roles : ['ROLE_USER'];
  155.     return $this;
  156.   }
  157.   /**
  158.    * @see PasswordAuthenticatedUserInterface
  159.    */
  160.   public function getPassword(): string
  161.   {
  162.     return $this->password;
  163.   }
  164.   public function setPassword(string $password): self
  165.   {
  166.     $this->password $password;
  167.     return $this;
  168.   }
  169.   /**
  170.    * Returning a salt is only needed, if you are not using a modern
  171.    * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  172.    *
  173.    * @see UserInterface
  174.    */
  175.   public function getSalt(): ?string
  176.   {
  177.     return null;
  178.   }
  179.   /**
  180.    * @see UserInterface
  181.    */
  182.   public function eraseCredentials()
  183.   {
  184.     // If you store any temporary, sensitive data on the user, clear it here
  185.     // $this->plainPassword = null;
  186.   }
  187.   public function isVerified(): bool
  188.   {
  189.       return $this->isVerified;
  190.   }
  191.   public function setIsVerified(bool $isVerified): self
  192.   {
  193.       $this->isVerified $isVerified;
  194.       return $this;
  195.   }
  196.   /**
  197.    * @return string|null
  198.    */
  199.   public function getPlainPassword(): ?string
  200.   {
  201.     return $this->plainPassword;
  202.   }
  203.   /**
  204.    * @param string|null $plainPassword
  205.    * @return User
  206.    */
  207.   public function setPlainPassword(?string $plainPassword): self
  208.   {
  209.     $this->plainPassword $plainPassword;
  210.     return $this;
  211.   }
  212.   /**
  213.    * @return mixed
  214.    */
  215.   public function getRole()
  216.   {
  217.     if (empty($this->roles)) {
  218.       $role null;
  219.     } else {
  220.       $role $this->roles[0];
  221.     }
  222.     return $role;
  223.   }
  224.   /**
  225.    * @param null $role
  226.    * @return User
  227.    */
  228.   public function setRole($role null): User
  229.   {
  230.     if (!empty($role) && !is_array($role))
  231.       $role = [$role];
  232.     return $this->setRoles($role);
  233.   }
  234.   /**
  235.    * @return mixed
  236.    */
  237.   public function getLogs(): Collection
  238.   {
  239.     return $this->logs;
  240.   }
  241.   /**
  242.    * @param Log $log
  243.    * @return $this
  244.    */
  245.   public function addLog(Log $log): User
  246.   {
  247.     if(!$this->logs->contains($log)){
  248.       $this->logs->add($log);
  249.     }
  250.     return $this;
  251.   }
  252.   /**
  253.    * @param Log $log
  254.    * @return $this
  255.    */
  256.   public function removeLog(Log $log): User
  257.   {
  258.     if($this->logs->contains($log)){
  259.       $this->logs->removeElement($log);
  260.     }
  261.     return $this;
  262.   }
  263.   /**
  264.    * @return string
  265.    */
  266.   public function getFullName(): string
  267.   {
  268.     return strtoupper($this->getLastName()).' '.$this->getFirstName();
  269.   }
  270.   public function __toString()
  271.   {
  272.     return $this->getFullName();
  273.   }
  274.   /**
  275.    * @return mixed
  276.    */
  277.   public function getContractualWeeklyHour(): int
  278.   {
  279.     return $this->contractualWeeklyHour;
  280.   }
  281.   /**
  282.    * @param mixed $contractualWeeklyHour
  283.    * @return User
  284.    */
  285.   public function setContractualWeeklyHour(int $contractualWeeklyHour): self
  286.   {
  287.     $this->contractualWeeklyHour $contractualWeeklyHour;
  288.     return $this;
  289.   }
  290.   /**
  291.    * @return bool
  292.    */
  293.   public function isActive(): bool
  294.   {
  295.     return $this->isActive;
  296.   }
  297.   /**
  298.    * @param bool $isActive
  299.    * @return User
  300.    */
  301.   public function setIsActive(bool $isActive): self
  302.   {
  303.     $this->isActive $isActive;
  304.     return $this;
  305.   }
  306. }