src/UserBundle/Entity/User.php line 15

Open in your IDE?
  1. <?php
  2. namespace User\Entity;
  3. use Core\Entity\Traits\EntityTrait;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. /**
  9.  * @ORM\Entity(repositoryClass="User\Repository\UserRepository")
  10.  * @ORM\Table(name="users_user")
  11.  */
  12. class User implements UserInterface, \Serializable
  13. {
  14.     use EntityTrait {
  15.         EntityTrait::__construct as private __entityConstruct;
  16.     }
  17.     /**
  18.      *
  19.      * @ORM\ManyToMany(targetEntity="UserGroup", mappedBy="users")
  20.      */
  21.     private $userGroups;
  22.     /**
  23.      * @ORM\Column(type="string", length=255, nullable=true)
  24.      */
  25.     private $lastname;
  26.     /**
  27.      * @ORM\Column(type="string", length=255, nullable=true)
  28.      */
  29.     private $firstname;
  30.     /**
  31.      * @ORM\Column(type="string", length=255, nullable=true)
  32.      */
  33.     private $email;
  34.     /**
  35.      * @Assert\NotBlank()
  36.      * @Assert\Length(max=250)
  37.      */
  38.     private $plainPassword;
  39.     /**
  40.      * @ORM\Column(type="string", length=255, nullable=true)
  41.      */
  42.     private $password;
  43.     /**
  44.      * @ORM\Column(type="string", length=255, nullable=true)
  45.      */
  46.     private $forgot_token;
  47.     /**
  48.      * Date de création
  49.      *
  50.      * @ORM\Column(type="datetime", nullable=true)
  51.      */
  52.     private $forgot_time;
  53.     /**
  54.      * @ORM\Column(name="is_active", type="boolean")
  55.      */
  56.     private $isActive;
  57.     /**
  58.      * @ORM\Column(name="roles", type="array")
  59.      */
  60.     private $roles = array();
  61.     /**
  62.      * Constructeur
  63.      * @throws \Exception
  64.      */
  65.     public function __construct()
  66.     {
  67.         $this->__entityConstruct();
  68.         $this->isActive true;
  69.         // $this->salt = md5(uniqid('', true));
  70.         $this->userGroups = new ArrayCollection();
  71.     }
  72.     /**
  73.      * @return mixed
  74.      */
  75.     public function getForgotToken()
  76.     {
  77.         return $this->forgot_token;
  78.     }
  79.     /**
  80.      * @param mixed $forgot_token
  81.      */
  82.     public function setForgotToken($forgot_token): void
  83.     {
  84.         $this->forgot_token $forgot_token;
  85.     }
  86.     /**
  87.      * @return mixed
  88.      */
  89.     public function getForgotTime()
  90.     {
  91.         return $this->forgot_time;
  92.     }
  93.     /**
  94.      * @param mixed $forgot_time
  95.      */
  96.     public function setForgotTime($forgot_time): void
  97.     {
  98.         $this->forgot_time $forgot_time;
  99.     }
  100.     public function getUserGroups()
  101.     {
  102.         return $this->userGroups;
  103.     }
  104.     /**
  105.      * @param UserGroup $userGroup
  106.      */
  107.     public function addUserGroup(UserGroup $userGroup)
  108.     {
  109.         if ($this->userGroups->contains($userGroup)) {
  110.             return;
  111.         }
  112.         $this->userGroups->add($userGroup);
  113.     }
  114.     public function hasUserGroup($group_id){
  115.         foreach($this->userGroups as $group){
  116.             if($group->getId() == $group_id){
  117.                 return true;
  118.             }
  119.         }
  120.         return false;
  121.     }
  122.     public function clearUserGroups()
  123.     {
  124.         $this->userGroups = new ArrayCollection();
  125.         return $this->getUserGroups();
  126.     }
  127.     /**
  128.      * @param UserGroup $userGroup
  129.      */
  130.     public function removeUserGroup(UserGroup $userGroup)
  131.     {
  132.         if (!$this->userGroups->contains($userGroup)) {
  133.             return;
  134.         }
  135.         $this->userGroups->removeElement($userGroup);
  136.     }
  137.     public function getIdentity()
  138.     {
  139.         return trim((String)$this->getFirstname() . ' ' $this->getLastname());
  140.     }
  141.     /**
  142.      * @return mixed
  143.      */
  144.     public function getLastname()
  145.     {
  146.         return $this->lastname;
  147.     }
  148.     /**
  149.      * @param mixed $lastname
  150.      */
  151.     public function setLastname($lastname): void
  152.     {
  153.         $this->lastname $lastname;
  154.     }
  155.     /**
  156.      * @return mixed
  157.      */
  158.     public function getFirstname()
  159.     {
  160.         return $this->firstname;
  161.     }
  162.     /**
  163.      * @param mixed $firstname
  164.      */
  165.     public function setFirstname($firstname): void
  166.     {
  167.         $this->firstname $firstname;
  168.     }
  169.     public function getUsername()
  170.     {
  171.         return $this->email;
  172.     }
  173.     public function getSalt()
  174.     {
  175.         // you *may* need a real salt depending on your encoder
  176.         // see section on salt below
  177.         return null;
  178.     }
  179.     public function getPassword()
  180.     {
  181.         return $this->password;
  182.     }
  183.     public function setPassword($password)
  184.     {
  185.         $this->password $password;
  186.     }
  187.     public function getRoles()
  188.     {
  189.         if (empty($this->roles)) {
  190.             return ['ROLE_USER'];
  191.         }
  192.         return $this->roles;
  193.     }
  194.     public function addRole($role)
  195.     {
  196.         $this->roles[] = $role;
  197.     }
  198.     public function eraseCredentials()
  199.     {
  200.     }
  201.     /** @see \Serializable::serialize() */
  202.     public function serialize()
  203.     {
  204.         return json_encode([
  205.             'id' => $this->id,
  206.             'email' => $this->email,
  207.             'password' => $this->password,
  208.             'isActive' => $this->isActive
  209.         ]);
  210.     }
  211.     /** @see \Serializable::unserialize() */
  212.     public function unserialize($serialized)
  213.     {
  214.         $params json_decode($serialized);
  215.         $this->id $params->id;
  216.         $this->email $params->email;
  217.         $this->password $params->password;
  218.         $this->isActive $params->isActive;
  219.         return $this;
  220.     }
  221.     public function getId()
  222.     {
  223.         return $this->id;
  224.     }
  225.     public function getEmail()
  226.     {
  227.         return $this->email;
  228.     }
  229.     public function getPlainPassword()
  230.     {
  231.         return $this->plainPassword;
  232.     }
  233.     public function getIsActive()
  234.     {
  235.         return $this->isActive;
  236.     }
  237.     public function setId($id)
  238.     {
  239.         $this->id $id;
  240.     }
  241.     public function setEmail($email)
  242.     {
  243.         $this->email $email;
  244.     }
  245.     public function setPlainPassword($plainPassword)
  246.     {
  247.         $this->plainPassword $plainPassword;
  248.     }
  249.     public function setIsActive($isActive)
  250.     {
  251.         $this->isActive $isActive;
  252.     }
  253.     public function __toString()
  254.     {
  255.         return $this->getIdentity();
  256.     }
  257. }