src/Entity/User/User.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Entity\User;
  3. use App\Entity\Application;
  4. use App\Entity\AvailableDay;
  5. use App\Entity\PrimaryIdTrait;
  6. use App\Entity\Security\ServiceSession;
  7. use App\Entity\SMS;
  8. use App\Entity\Survey\Survey;
  9. use App\Entity\Survey\SurveyNotification;
  10. use App\Entity\Technic;
  11. use App\Entity\Vacation;
  12. use App\Repository\User\UserRepository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Gedmo\Timestampable\Traits\TimestampableEntity;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  19. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  20. use Symfony\Component\Serializer\Annotation\Groups;
  21. #[ORM\Table(name'`user`')]
  22. #[ORM\Entity(repositoryClassUserRepository::class)]
  23. #[UniqueEntity(fields: ['email'], message'There is already an account with this email')]
  24. class User implements UserInterfacePasswordAuthenticatedUserInterface
  25. {
  26.     public const ROUTE_NAME_CREATE null;
  27.     public const ROUTE_NAME_EDIT 'USER_EDIT';
  28.     public const ROUTE_NAME_DETAIL 'USER_DETAIL';
  29.     public const ROUTE_NAME_DELETE 'USER_DELETE';
  30.     public const DEPARTMENT_BED 0;
  31.     public const DEPARTMENT_FED 1;
  32.     public const DEPARTMENT_QA 2;
  33.     public const DEPARTMENT_CM 3;
  34.     public const DEPARTMENT_AM 4;
  35.     public const DEPARTMENT_DES 5;
  36.     public const DEPARTMENT_BACK_OFFICE 6;
  37.     public const DEPARTMENT_HR 7;
  38.     public const DEPARTMENT_CEO 8;
  39.     public const DEPARTMENTS = [
  40.         self::DEPARTMENT_BED => 'user.department.bed',
  41.         self::DEPARTMENT_FED => 'user.department.fed',
  42.         self::DEPARTMENT_QA => 'user.department.qa',
  43.         self::DEPARTMENT_CM => 'user.department.cm',
  44.         self::DEPARTMENT_AM => 'user.department.am',
  45.         self::DEPARTMENT_DES => 'user.department.des',
  46.         self::DEPARTMENT_BACK_OFFICE => 'user.department.backOffice',
  47.         self::DEPARTMENT_HR => 'user.department.hr',
  48.         self::DEPARTMENT_CEO => 'user.department.ceo'
  49.     ];
  50.     public const POSITION_JUNIOR 0;
  51.     public const POSITION_MIDDLE 1;
  52.     public const POSITION_SENIOR 2;
  53.     public const POSITION_GROUP_HEAD 3;
  54.     public const POSITION_TEAM_LEAD 4;
  55.     public const POSITIONS = [
  56.         self::POSITION_JUNIOR => 'user.position.junior',
  57.         self::POSITION_MIDDLE => 'user.position.middle',
  58.         self::POSITION_SENIOR => 'user.position.senior',
  59.         self::POSITION_GROUP_HEAD => 'user.position.groupHead',
  60.         self::POSITION_TEAM_LEAD => 'user.position.teamLead'
  61.     ];
  62.     public const DEPARTMENT_ROLES = [
  63.         self::DEPARTMENT_BED => ['ROLE_BED'],
  64.         self::DEPARTMENT_FED => ['ROLE_FED'],
  65.         self::DEPARTMENT_QA => ['ROLE_QA'],
  66.         self::DEPARTMENT_CM => ['ROLE_CM'],
  67.         self::DEPARTMENT_AM => ['ROLE_AM'],
  68.         self::DEPARTMENT_DES => ['ROLE_DES'],
  69.         self::DEPARTMENT_BACK_OFFICE => ['ROLE_BACK_OFFICE'],
  70.         self::DEPARTMENT_HR => ['ROLE_HR'],
  71.         self::DEPARTMENT_CEO => ['ROLE_CEO']
  72.     ];
  73.     public const POSITION_ROLES = [
  74.         self::POSITION_JUNIOR => ['ROLE_JUNIOR'],
  75.         self::POSITION_MIDDLE => ['ROLE_MIDDLE'],
  76.         self::POSITION_SENIOR => ['ROLE_SENIOR'],
  77.         self::POSITION_GROUP_HEAD => ['ROLE_GROUP_HEAD'],
  78.         self::POSITION_TEAM_LEAD => ['ROLE_TEAM_LEAD']
  79.     ];
  80.     use PrimaryIdTrait;
  81.     use TimestampableEntity;
  82.     #[ORM\Column(type'string'length255nullabletrue)]
  83.     #[Groups(['poll'])]
  84.     private ?string $firstname;
  85.     #[ORM\Column(type'string'length255nullabletrue)]
  86.     #[Groups(['poll'])]
  87.     private ?string $lastname;
  88.     #[ORM\Column(type'string'length255)]
  89.     private string $login;
  90.     #[ORM\Column(type'json')]
  91.     private $roles = [];
  92.     #[ORM\Column(type'string'length255)]
  93.     private string $email;
  94.     #[ORM\Column(type'string'length255)]
  95.     private string $password '';
  96.     #[ORM\Column(type'boolean')]
  97.     private $isVerified false;
  98.     #[ORM\Column(type'string'length255nullabletrue)]
  99.     #[Groups(['poll'])]
  100.     private ?string $avatar;
  101.     #[ORM\Column(type'string'length255nullabletrue)]
  102.     private ?string $slackNick;
  103.     #[ORM\Column(type'integer'nullabletrue)]
  104.     private ?int $position;
  105.     #[ORM\Column(type'integer'nullabletrue)]
  106.     private ?int $department;
  107.     #[ORM\OneToMany(targetEntity'App\Entity\Survey\Survey'mappedBy'author'cascade: ['remove'])]
  108.     #[ORM\OrderBy(['createdAt' => 'DESC'])]
  109.     public $survey;
  110.     #[ORM\OneToMany(targetEntity'App\Entity\Survey\SurveyNotification'mappedBy'user'cascade: ['remove'])]
  111.     public $surveyNotification;
  112.     #[ORM\Column(type'integer')]
  113.     private int $status 0;
  114.     #[ORM\Column(type'string'length255nullabletrue)]
  115.     private ?string $slackId;
  116.     #[ORM\OneToMany(targetEntityVacation::class, mappedBy'user'cascade: ['remove'])]
  117.     private $vacations;
  118.     #[ORM\OneToMany(targetEntityApplication::class, mappedBy'user')]
  119.     private $applications;
  120.     #[ORM\OneToMany(targetEntityAvailableDay::class, mappedBy'user'cascade: ['remove'])]
  121.     private $availableDays;
  122.     #[ORM\OneToMany(targetEntitySMS::class, mappedBy'user')]
  123.     private $sms;
  124.     #[ORM\OneToMany(targetEntityServiceSession::class, mappedBy'userId'cascade: ['remove'])]
  125.     private $serviceSession;
  126.     #[ORM\Column(name'last_activity_at'type'datetime'nullabletrue)]
  127.     protected ?\Datetime $lastActivityAt null;
  128.     #[ORM\OneToMany(targetEntityTechnic::class, mappedBy'user')]
  129.     private $technic;
  130.     public function __construct()
  131.     {
  132.         $this->vacations = new ArrayCollection();
  133.         $this->applications = new ArrayCollection();
  134.         $this->sms = new ArrayCollection();
  135.         $this->survey = new ArrayCollection();
  136.         $this->surveyNotification = new ArrayCollection();
  137.         $this->technic = new ArrayCollection();
  138.     }
  139.     public function getPosition(): ?int
  140.     {
  141.         return $this->position;
  142.     }
  143.     public function setPosition(?int $position): void
  144.     {
  145.         $this->position $position;
  146.     }
  147.     public function getDepartment(): ?int
  148.     {
  149.         return $this->department;
  150.     }
  151.     public function setDepartment(?int $department): void
  152.     {
  153.         $this->department $department;
  154.     }
  155.     public function getSlackNick(): ?string
  156.     {
  157.         return $this->slackNick;
  158.     }
  159.     public function setSlackNick(?string $slackNick): self
  160.     {
  161.         $this->slackNick $slackNick;
  162.         return $this;
  163.     }
  164.     public function getSlackId(): ?string
  165.     {
  166.         return $this->slackId;
  167.     }
  168.     public function setSlackId(?string $slackId): self
  169.     {
  170.         $this->slackId $slackId;
  171.         return $this;
  172.     }
  173.     public function getFirstname(): ?string
  174.     {
  175.         return $this->firstname;
  176.     }
  177.     /**
  178.      * A visual identifier that represents this user.
  179.      *
  180.      * @see UserInterface
  181.      */
  182.     public function getUserIdentifier(): string
  183.     {
  184.         return (string) $this->email;
  185.     }
  186.     public function setFirstname(?string $firstname): self
  187.     {
  188.         $this->firstname $firstname;
  189.         return $this;
  190.     }
  191.     public function getLastname(): ?string
  192.     {
  193.         return $this->lastname;
  194.     }
  195.     public function setLastname(?string $lastname): self
  196.     {
  197.         $this->lastname $lastname;
  198.         return $this;
  199.     }
  200.     public function getUsername(): ?string
  201.     {
  202.         return $this->login;
  203.     }
  204.     public function setLogin(string $login): self
  205.     {
  206.         $this->login $login;
  207.         return $this;
  208.     }
  209.     public function getEmail(): ?string
  210.     {
  211.         return $this->email;
  212.     }
  213.     public function setEmail(string $email): self
  214.     {
  215.         $this->email $email;
  216.         return $this;
  217.     }
  218.     /**
  219.      * Returning a salt is only needed, if you are not using a modern
  220.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  221.      *
  222.      * @see UserInterface
  223.      */
  224.     public function getSalt(): ?string
  225.     {
  226.         return null;
  227.     }
  228.     /**
  229.      * @see UserInterface
  230.      */
  231.     public function eraseCredentials()
  232.     {
  233.         // If you store any temporary, sensitive data on the user, clear it here
  234.         // $this->plainPassword = null;
  235.     }
  236.     public function isVerified(): bool
  237.     {
  238.         return $this->isVerified;
  239.     }
  240.     public function setIsVerified(bool $isVerified): self
  241.     {
  242.         $this->isVerified $isVerified;
  243.         return $this;
  244.     }
  245.     /**
  246.      * @see UserInterface
  247.      */
  248.     public function getRoles(): array
  249.     {
  250.         $roles $this->roles;
  251.         // guarantee every user at least has ROLE_USER
  252.         $roles[] = 'ROLE_USER';
  253.         return array_unique($roles);
  254.     }
  255.     public function setRoles(array $roles): self
  256.     {
  257.         $this->roles $roles;
  258.         return $this;
  259.     }
  260.     public function hasRoles(string $role){
  261.         if (in_array($role$this->getRoles(), true)) {
  262.             return true;
  263.         }
  264.         return false;
  265.     }
  266.     /**
  267.      * @see PasswordAuthenticatedUserInterface
  268.      */
  269.     public function getPassword(): string
  270.     {
  271.         return $this->password;
  272.     }
  273.     public function setPassword(string $password): self
  274.     {
  275.         $this->password $password;
  276.         return $this;
  277.     }
  278.     public function getAvatar(): ?string
  279.     {
  280.         return $this->avatar;
  281.     }
  282.     public function setAvatar(?string $avatar): self
  283.     {
  284.         $this->avatar $avatar;
  285.         return $this;
  286.     }
  287.     /**
  288.      * @return Collection|Vacation[]
  289.      */
  290.     public function getVacations(): Collection
  291.     {
  292.         return $this->vacations;
  293.     }
  294.     public function getAvailableDays(): AvailableDay
  295.     {
  296.         return $this->availableDays;
  297.     }
  298.     public function addVacation(Vacation $vacation): self
  299.     {
  300.         if (!$this->vacations->contains($vacation)) {
  301.             $this->vacations[] = $vacation;
  302.             $vacation->setUser($this);
  303.         }
  304.         return $this;
  305.     }
  306.     public function removeVacation(Vacation $vacation): self
  307.     {
  308.         if ($this->vacations->removeElement($vacation)) {
  309.             // set the owning side to null (unless already changed)
  310.             if ($vacation->getUser() === $this) {
  311.                 $vacation->setUser(null);
  312.             }
  313.         }
  314.         return $this;
  315.     }
  316.     public function getSms(): Collection
  317.     {
  318.         return $this->sms;
  319.     }
  320.     public function addSms(SMS $sms): void
  321.     {
  322.         $sms->setUser($this);
  323.         $this->sms->add($sms);
  324.     }
  325.     public function removeSms(SMS $sms): void
  326.     {
  327.         if ($this->sms->contains($sms)) {
  328.             $this->sms->removeElement($sms);
  329.         }
  330.     }
  331.     /**
  332.      * @return Collection|Survey[]
  333.      */
  334.     public function getSurvey(): Collection
  335.     {
  336.         return $this->survey;
  337.     }
  338.     /**
  339.      * @return Collection|SurveyNotification[]
  340.      */
  341.     public function getSurveyNotification(): Collection
  342.     {
  343.         return $this->surveyNotification;
  344.     }
  345.     /**
  346.      * @param \Datetime $lastActivityAt
  347.      */
  348.     public function setLastActivityAt($lastActivityAt)
  349.     {
  350.         $this->lastActivityAt $lastActivityAt;
  351.     }
  352.     /**
  353.      * @return \Datetime
  354.      */
  355.     public function getLastActivityAt()
  356.     {
  357.         return $this->lastActivityAt;
  358.     }
  359.     /**
  360.      * @return Bool Whether the user is active or not
  361.      */
  362.     public function isActiveNow()
  363.     {
  364.         $delay = new \DateTime('2 minutes ago');
  365.         return ( $this->getLastActivityAt() > $delay );
  366.     }
  367.     public function getStatus(): int
  368.     {
  369.         return $this->status;
  370.     }
  371.     public function setStatus(int $status): void
  372.     {
  373.         $this->status $status;
  374.     }
  375.     /**
  376.      * @return Collection<int, Technic>
  377.      */
  378.     public function getTechnic(): Collection
  379.     {
  380.         return $this->technic;
  381.     }
  382.     public function addTechnic(Technic $technic): self
  383.     {
  384.         if (!$this->technic->contains($technic)) {
  385.             $this->technic[] = $technic;
  386.             $technic->setUser($this);
  387.         }
  388.         return $this;
  389.     }
  390.     public function removeTechnic(Technic $technic): self
  391.     {
  392.         if ($this->technic->removeElement($technic)) {
  393.             // set the owning side to null (unless already changed)
  394.             if ($technic->getUser() === $this) {
  395.                 $technic->setUser(null);
  396.             }
  397.         }
  398.         return $this;
  399.     }
  400.     public function getFullName():string
  401.     {
  402.         return sprintf('%s %s'$this->firstname$this->lastname);
  403.     }
  404. }