src/Entity/User.php line 18

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 Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10.  * @ORM\Table(name="user")
  11.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  12.  * @ORM\HasLifecycleCallbacks()
  13.  * @UniqueEntity("email")
  14.  */
  15. class User implements UserInterface, \Serializable
  16. {
  17.     public const ROLE_ADMIN 'ROLE_ADMIN';
  18.     public const ROLE_RH 'ROLE_RH';
  19.     public const ROLE_USER_ACTIF 'ROLE_USER_ACTIF';
  20.     public const ROLE_USER_INACTIF 'ROLE_USER_INACTIF';
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @Assert\Length(max=50,min=6,minMessage="Le mot de passe doit faire minimum {{ limit }} caractères.")
  29.      */
  30.     private $plainPassword;
  31.     /**
  32.      * @ORM\Column(type="string", length=255, nullable=true)
  33.      */
  34.     private $password;
  35.     /**
  36.      * @ORM\Column(type="string", length=60, unique=true)
  37.      * @Assert\Email
  38.      */
  39.     private $email;
  40.     /**
  41.      * @ORM\ManyToOne(targetEntity="Company", inversedBy="users", fetch="EAGER")
  42.      * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
  43.      */
  44.     private $company;
  45.     /**
  46.      * @ORM\ManyToOne(targetEntity="Role", fetch="EAGER")
  47.      * @ORM\JoinColumn(name="role_id", referencedColumnName="id")
  48.      */
  49.     private $roles;
  50.     /**
  51.      * @ORM\Column(type="string", length=90, nullable=true)
  52.      */
  53.     private $firstname;
  54.     /**
  55.      * @ORM\Column(type="string", length=90, nullable=true)
  56.      */
  57.     private $lastname;
  58.     /**
  59.      * @ORM\Column(type="date", nullable=true)
  60.      */
  61.     private $birth;
  62.     /**
  63.      * @ORM\Column(type="string", length=2, nullable=true)
  64.      */
  65.     private $gender;
  66.     /**
  67.      * @ORM\Column(type="integer", nullable=true)
  68.      */
  69.     private $weight null;
  70.     /**
  71.      * @ORM\Column(type="float", nullable=true)
  72.      */
  73.     private $height null;
  74.     /**
  75.      * @ORM\Column(type="json", nullable=true)
  76.      */
  77.     private $punctual_sport;
  78.     /**
  79.      * @ORM\Column(type="boolean", nullable=true)
  80.      */
  81.     private $work_handicapped null;
  82.     /**
  83.      * @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
  84.      */
  85.     private $createdAt;
  86.     /**
  87.      * @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
  88.      */
  89.     private $updatedAt;
  90.     /**
  91.      * @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
  92.      */
  93.     private $updatedDataConfirmedAt;
  94.     /**
  95.      * @ORM\OneToMany(targetEntity="App\Entity\UserActivity", mappedBy="user", cascade={"persist","remove"})
  96.      */
  97.     private $userActivities;
  98.     /**
  99.      * @ORM\OneToMany(targetEntity=FormationRegistration::class, mappedBy="User",cascade={"remove"})
  100.      */
  101.     private $formations;
  102.     /**
  103.      * @ORM\OneToMany(targetEntity=Formation::class, mappedBy="owner", cascade={"remove"})
  104.      */
  105.     private $ownedFormations;
  106.     /**
  107.      * @ORM\OneToMany(targetEntity=UserFavorites::class, mappedBy="user",cascade={"remove"})
  108.      */
  109.     private $favorites;
  110.     /**
  111.      * @ORM\OneToMany(targetEntity=Notification::class, mappedBy="userTo",cascade={"remove"})
  112.      */
  113.     private $notifications;
  114.     /**
  115.      * @ORM\Column(type="boolean", options={"default": "0"} )
  116.      */
  117.     private $intro false;
  118.     /**
  119.      * @ORM\Column(type="boolean", options={"default": "0"} )
  120.      */
  121.     private $cgu false;
  122.     /**
  123.      * @ORM\OneToMany(targetEntity=Log::class, mappedBy="user", cascade={"remove"})
  124.      */
  125.     private $logs;
  126.     /**
  127.      * @ORM\Column(type="boolean", nullable=true)
  128.      */
  129.     private $isContact;
  130.     /**
  131.      * @ORM\Column(type="datetime", nullable=true, options={"default": null})
  132.      */
  133.     private $lastLoginAt;
  134.     /**
  135.      * @ORM\Column(type="string", length=255, nullable=true)
  136.      */
  137.     private $keycloakId;
  138.     /**
  139.      * @ORM\Column(type="datetime", nullable=true)
  140.      */
  141.     private $agreeTermsAt;
  142.     /**
  143.      * @ORM\Column(type="datetime", nullable=true)
  144.      */
  145.     private $agreePrivateDataAt;
  146.     public function __construct()
  147.     {
  148.         $this->userActivities = new ArrayCollection();
  149.         $this->formations = new ArrayCollection();
  150.         $this->ownedFormations = new ArrayCollection();
  151.         $this->notifications = new ArrayCollection();
  152.         $this->logs = new ArrayCollection();
  153.     }
  154.     /**
  155.      * @return mixed
  156.      */
  157.     public function getId()
  158.     {
  159.         return $this->id;
  160.     }
  161.     /**
  162.      * @return mixed
  163.      */
  164.     public function getFirstname()
  165.     {
  166.         return $this->firstname;
  167.     }
  168.     /**
  169.      * @param mixed $firstname
  170.      */
  171.     public function setFirstname($firstname)
  172.     {
  173.         $this->firstname $firstname;
  174.     }
  175.     /**
  176.      * @return mixed
  177.      */
  178.     public function getLastname()
  179.     {
  180.         return $this->lastname;
  181.     }
  182.     /**
  183.      * @param mixed $lastname
  184.      */
  185.     public function setLastname($lastname)
  186.     {
  187.         $this->lastname $lastname;
  188.     }
  189.     /**
  190.      * @return mixed
  191.      */
  192.     public function getBirth()
  193.     {
  194.         return $this->birth;
  195.     }
  196.     /**
  197.      * @param mixed $birth
  198.      */
  199.     public function setBirth($birth)
  200.     {
  201.         $this->birth $birth;
  202.     }
  203.     /**
  204.      * @return mixed
  205.      */
  206.     public function getGender()
  207.     {
  208.         return $this->gender;
  209.     }
  210.     /**
  211.      * @param mixed $gender
  212.      */
  213.     public function setGender($gender)
  214.     {
  215.         $this->gender $gender;
  216.     }
  217.     /**
  218.      * @return mixed
  219.      */
  220.     public function getWeight()
  221.     {
  222.         return $this->weight;
  223.     }
  224.     /**
  225.      * @param mixed $weight
  226.      */
  227.     public function setWeight($weight)
  228.     {
  229.         $this->weight $weight;
  230.     }
  231.     /**
  232.      * @return mixed
  233.      */
  234.     public function getHeight()
  235.     {
  236.         return $this->height;
  237.     }
  238.     /**
  239.      * @param mixed $height
  240.      */
  241.     public function setHeight($height)
  242.     {
  243.         $this->height $height;
  244.     }
  245.     /**
  246.      * @return mixed
  247.      */
  248.     public function getCompany()
  249.     {
  250.         return $this->company;
  251.     }
  252.     /**
  253.      * @param mixed $company
  254.      */
  255.     public function setCompany($company)
  256.     {
  257.         $this->company $company;
  258.     }
  259.     /**
  260.      * @return mixed
  261.      */
  262.     public function getEmail()
  263.     {
  264.         return $this->email;
  265.     }
  266.     /**
  267.      * Returns the roles granted to the user.
  268.      *
  269.      * <code>
  270.      * public function getRoles()
  271.      * {
  272.      *     return array('ROLE_USER_ACTIF');
  273.      * }
  274.      * </code>
  275.      *
  276.      * Alternatively, the roles might be stored on a ``roles`` property,
  277.      * and populated in any number of different ways when the user object
  278.      * is created.
  279.      *
  280.      * @return (Role|string)[] The user roles
  281.      */
  282.     public function getRoles()
  283.     {
  284.         return array($this->roles->getName());
  285.     }
  286.     public static function getStandardRoleNames(): array
  287.     {
  288.         return [
  289.             self::ROLE_USER_ACTIF,
  290.             self::ROLE_USER_INACTIF,
  291.         ];
  292.     }
  293.     public static function getAssignableRoleNames(): array
  294.     {
  295.         return array_merge(self::getStandardRoleNames(), [self::ROLE_RH]);
  296.     }
  297.     public static function getStandardAndManagerRoleNames(): array
  298.     {
  299.         return array_merge(self::getStandardRoleNames(), [self::ROLE_RH]);
  300.     }
  301.     public function isStandardUser(): bool
  302.     {
  303.         return in_array($this->getRoles()[0], self::getStandardRoleNames(), true);
  304.     }
  305.     public function getRoleLabel(): string
  306.     {
  307.         switch ($this->getRoles()[0]) {
  308.             case self::ROLE_USER_ACTIF:
  309.                 return 'Utilisateur actif';
  310.             case self::ROLE_USER_INACTIF:
  311.                 return 'Utilisateur non actif';
  312.             case self::ROLE_RH:
  313.                 return 'Gestionnaire';
  314.             case self::ROLE_ADMIN:
  315.                 return 'Administrateur';
  316.             default:
  317.                 return $this->getRoles()[0];
  318.         }
  319.     }
  320.     /**
  321.      * @param string|Role $role
  322.      * @return bool
  323.      */
  324.     public function hasRole($role)
  325.     {
  326.         $roles $this->getRoles();
  327.         foreach ($roles as $_role) {
  328.             if($_role == (is_string($role) ? $role $role->getName()))
  329.                 return true;
  330.         }
  331.         return false;
  332.     }
  333.     /**
  334.      * @return Collection|UserActivity[]
  335.      */
  336.     public function getUserActivities(): Collection
  337.     {
  338.         return $this->userActivities;
  339.     }
  340.     public function addUserActivity(UserActivity $userActivity): self
  341.     {
  342.         if (!$this->userActivities->contains($userActivity)) {
  343.             $this->userActivities[] = $userActivity;
  344.             $userActivity->setUser($this);
  345.         }
  346.         return $this;
  347.     }
  348.     public function removeUserActivity(UserActivity $userActivity): self
  349.     {
  350.         if ($this->userActivities->contains($userActivity)) {
  351.             $this->userActivities->removeElement($userActivity);
  352.             // set the owning side to null (unless already changed)
  353.             if ($userActivity->getUser() === $this) {
  354.                 $userActivity->setUser(null);
  355.             }
  356.         }
  357.         return $this;
  358.     }
  359.     /**
  360.      * @param mixed $roles
  361.      */
  362.     public function setRoles(Role $roles)
  363.     {
  364.         $this->roles $roles;
  365.     }
  366.     /**
  367.      * Returns the password used to authenticate the user.
  368.      *
  369.      * This should be the encoded password. On authentication, a plain-text
  370.      * password will be salted, encoded, and then compared to this value.
  371.      *
  372.      * @return string The password
  373.      */
  374.     public function getPassword()
  375.     {
  376.         return $this->password;
  377.     }
  378.     /**
  379.      * Returns the salt that was originally used to encode the password.
  380.      *
  381.      * This can return null if the password was not encoded using a salt.
  382.      *
  383.      * @return string|null The salt
  384.      */
  385.     public function getSalt()
  386.     {
  387.         return null;
  388.     }
  389.     /**
  390.      * Returns the username used to authenticate the user.
  391.      *
  392.      * @return string The username
  393.      */
  394.     public function getUsername()
  395.     {
  396.         return $this->getEmail();
  397.     }
  398.     /**
  399.      * @param mixed $password
  400.      */
  401.     public function setPassword($password)
  402.     {
  403.         $this->password $password;
  404.     }
  405.     /**
  406.      * @param mixed $email
  407.      */
  408.     public function setEmail($email)
  409.     {
  410.         $this->email $email;
  411.     }
  412.     /**
  413.      * @return mixed
  414.      */
  415.     public function getCreatedAt()
  416.     {
  417.         return $this->createdAt;
  418.     }
  419.     /**
  420.      * @param mixed $createdAt
  421.      */
  422.     public function setCreatedAt($createdAt): void
  423.     {
  424.         $this->createdAt $createdAt;
  425.     }
  426.     /**
  427.      * @return mixed
  428.      */
  429.     public function getUpdatedAt()
  430.     {
  431.         return $this->updatedAt;
  432.     }
  433.     /**
  434.      * @param mixed $updatedAt
  435.      */
  436.     public function setUpdatedAt($updatedAt): void
  437.     {
  438.         $this->updatedAt $updatedAt;
  439.     }
  440.     /**
  441.      * Removes sensitive data from the user.
  442.      *
  443.      * This is important if, at any given point, sensitive information like
  444.      * the plain-text password is stored on this object.
  445.      */
  446.     public function eraseCredentials()
  447.     {
  448.         // TODO: Implement eraseCredentials() method.
  449.     }
  450.     /**
  451.      * String representation of object
  452.      * @link http://php.net/manual/en/serializable.serialize.php
  453.      * @return string the string representation of the object or null
  454.      * @since 5.1.0
  455.      */
  456.     public function serialize()
  457.     {
  458.         return serialize(array(
  459.             $this->id,
  460.             $this->email,
  461.             $this->password,
  462.             $this->roles,
  463.             // see section on salt below
  464.             // $this->salt,
  465.         ));
  466.     }
  467.     /**
  468.      * Constructs the object
  469.      * @link http://php.net/manual/en/serializable.unserialize.php
  470.      * @param string $serialized <p>
  471.      * The string representation of the object.
  472.      * </p>
  473.      * @return void
  474.      * @since 5.1.0
  475.      */
  476.     public function unserialize($serialized)
  477.     {
  478.         list(
  479.             $this->id,
  480.             $this->email,
  481.             $this->password,
  482.             $this->roles,
  483.             // see section on salt below
  484.             // $this->salt
  485.         ) = unserialize($serialized);
  486.     }
  487.     /** @ORM\PrePersist */
  488.     public function onPrePersist()
  489.     {
  490.         $this->createdAt = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
  491.         $this->updatedAt = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
  492.     }
  493.     /** @ORM\PreUpdate */
  494.     public function onPreUpdate()
  495.     {
  496.         $this->updatedAt = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
  497.     }
  498.     /**
  499.      * @return mixed
  500.      */
  501.     public function getPlainPassword()
  502.     {
  503.         return $this->plainPassword;
  504.     }
  505.     /**
  506.      * @param mixed $plainPassword
  507.      */
  508.     public function setPlainPassword($plainPassword): void
  509.     {
  510.         $this->plainPassword $plainPassword;
  511.     }
  512.     /**
  513.      * @return Collection|FormationRegistration[]
  514.      */
  515.     public function getFormations(): Collection
  516.     {
  517.         return $this->formations;
  518.     }
  519.     public function addFormation(FormationRegistration $formation): self
  520.     {
  521.         if (!$this->formations->contains($formation)) {
  522.             $this->formations[] = $formation;
  523.             $formation->setUser($this);
  524.         }
  525.         return $this;
  526.     }
  527.     public function removeFormation(FormationRegistration $formation): self
  528.     {
  529.         if ($this->formations->removeElement($formation)) {
  530.             // set the owning side to null (unless already changed)
  531.             if ($formation->getUser() === $this) {
  532.                 $formation->setUser(null);
  533.             }
  534.         }
  535.         return $this;
  536.     }
  537.     public function getOwnedFormations(): Collection
  538.     {
  539.         return $this->ownedFormations;
  540.     }
  541.     public function addOwnedFormation(Formation $formation): self
  542.     {
  543.         if (!$this->ownedFormations->contains($formation)) {
  544.             $this->ownedFormations[] = $formation;
  545.             $formation->setOwner($this);
  546.         }
  547.         return $this;
  548.     }
  549.     public function removeOwnedFormation(Formation $formation): self
  550.     {
  551.         if ($this->ownedFormations->removeElement($formation)) {
  552.             if ($formation->getOwner() === $this) {
  553.                 $formation->setOwner(null);
  554.             }
  555.         }
  556.         return $this;
  557.     }
  558.     /**
  559.      * @return Collection|Notification[]
  560.      */
  561.     public function getNotifications(): Collection
  562.     {
  563.         return $this->notifications;
  564.     }
  565.     public function addNotification(Notification $notification): self
  566.     {
  567.         if (!$this->notifications->contains($notification)) {
  568.             $this->notifications[] = $notification;
  569.             $notification->setUserTo($this);
  570.         }
  571.         return $this;
  572.     }
  573.     public function removeNotification(Notification $notification): self
  574.     {
  575.         if ($this->notifications->removeElement($notification)) {
  576.             // set the owning side to null (unless already changed)
  577.             if ($notification->getUserTo() === $this) {
  578.                 $notification->setUserTo(null);
  579.             }
  580.         }
  581.         return $this;
  582.     }
  583.     /**
  584.      * @return bool
  585.      */
  586.     public function isIntro(): bool
  587.     {
  588.         return $this->intro;
  589.     }
  590.     /**
  591.      * @param bool $intro
  592.      */
  593.     public function setIntro(bool $intro): void
  594.     {
  595.         $this->intro $intro;
  596.     }
  597.     /**
  598.      * @return bool
  599.      */
  600.     public function getCgu(): bool
  601.     {
  602.         return $this->cgu;
  603.     }
  604.     /**
  605.      * @param bool $cgu
  606.      */
  607.     public function setCgu(bool $cgu): void
  608.     {
  609.         $this->cgu $cgu;
  610.     }
  611.     /**
  612.      * @return null
  613.      */
  614.     public function getWorkHandicapped()
  615.     {
  616.         return $this->work_handicapped;
  617.     }
  618.     /**
  619.      * @param null $work_handicapped
  620.      */
  621.     public function setWorkHandicapped($work_handicapped): void
  622.     {
  623.         $this->work_handicapped $work_handicapped;
  624.     }
  625.     /**
  626.      * @return
  627.      */
  628.     public function getPunctualSport()
  629.     {
  630.         return $this->punctual_sport;
  631.     }
  632.     /**
  633.      * @param $punctual_sport
  634.      */
  635.     public function setPunctualSport($punctual_sport): void
  636.     {
  637.         $this->punctual_sport $punctual_sport;
  638.     }
  639.     /**
  640.      * @return mixed
  641.      */
  642.     public function getUpdatedDataConfirmedAt()
  643.     {
  644.         return $this->updatedDataConfirmedAt;
  645.     }
  646.     /**
  647.      * @param mixed $updatedDataConfirmedAt
  648.      */
  649.     public function setUpdatedDataConfirmedAt($updatedDataConfirmedAt): void
  650.     {
  651.         $this->updatedDataConfirmedAt $updatedDataConfirmedAt;
  652.     }
  653.     public function getIsContact(): ?bool
  654.     {
  655.         return $this->isContact;
  656.     }
  657.     public function setIsContact(?bool $isContact): self
  658.     {
  659.         $this->isContact $isContact;
  660.         return $this;
  661.     }
  662.     /**
  663.      * @return mixed
  664.      */
  665.     public function getLastLoginAt()
  666.     {
  667.         return $this->lastLoginAt;
  668.     }
  669.     /**
  670.      * @param mixed $lastLoginAt
  671.      */
  672.     public function setLastLoginAt($lastLoginAt): void
  673.     {
  674.         $this->lastLoginAt $lastLoginAt;
  675.     }
  676.     //    /**
  677. //     * @ORM\PrePersist
  678. //     * @ORM\PreUpdate
  679. //     */
  680. //    public function updatedTimestamps(): void
  681. //    {
  682. //        $this->setUpdatedDataConfirmedAt(new \DateTime());
  683. //    }
  684.     /**
  685.      * @return mixed
  686.      */
  687.     public function getKeycloakId()
  688.     {
  689.         return $this->keycloakId;
  690.     }
  691.     /**
  692.      * @param mixed $keycloakId
  693.      */
  694.     public function setKeycloakId($keycloakId): void
  695.     {
  696.         $this->keycloakId $keycloakId;
  697.     }
  698.     /**
  699.      * @return mixed
  700.      */
  701.     public function getAgreeTermsAt()
  702.     {
  703.         return $this->agreeTermsAt;
  704.     }
  705.     /**
  706.      * @param mixed $agreeTermsAt
  707.      */
  708.     public function setAgreeTermsAt($agreeTermsAt): void
  709.     {
  710.         $this->agreeTermsAt $agreeTermsAt;
  711.     }
  712.     /**
  713.      * @return mixed
  714.      */
  715.     public function getAgreePrivateDataAt()
  716.     {
  717.         return $this->agreePrivateDataAt;
  718.     }
  719.     /**
  720.      * @param mixed $agreePrivateDataAt
  721.      */
  722.     public function setAgreePrivateDataAt($agreePrivateDataAt): void
  723.     {
  724.         $this->agreePrivateDataAt $agreePrivateDataAt;
  725.     }
  726. }