src/Entity/User.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  9.  */
  10. class User implements UserInterface\SerializablePasswordAuthenticatedUserInterface
  11. {
  12.     /**
  13.      * @ORM\Id()
  14.      * @ORM\GeneratedValue()
  15.      * @ORM\Column(type="integer")
  16.      */
  17.     private $id;
  18.     /**
  19.      * @ORM\Column(type="string", length=50, nullable=true)
  20.      */
  21.     private $firstname;
  22.     /**
  23.      * @ORM\Column(type="string", length=100, nullable=true)
  24.      */
  25.     private $lastname;
  26.     /**
  27.      * @ORM\Column(type="string", length=190, unique=true)
  28.      * @Assert\Email(message="Dit is geen geldig emailadres")
  29.      */
  30.     private $email;
  31.     /**
  32.      * @ORM\Column(type="string", length=100)
  33.      */
  34.     private $password;
  35.     /**
  36.      * @ORM\Column(type="boolean", options={"default":"0"}, nullable=true)
  37.      */
  38.     private $active;
  39.     /**
  40.      * @ORM\Column(type="text")
  41.      */
  42.     private $roles;
  43.     public function getId()
  44.     {
  45.         return $this->id;
  46.     }
  47.     public function getName()
  48.     {
  49.         return trim($this->firstname ' ' $this->lastname);
  50.     }
  51.     public function getFirstname(): ?string
  52.     {
  53.         return $this->firstname;
  54.     }
  55.     public function setFirstname(?string $firstname): self
  56.     {
  57.         $this->firstname $firstname;
  58.         return $this;
  59.     }
  60.     public function getLastname(): ?string
  61.     {
  62.         return $this->lastname;
  63.     }
  64.     public function setLastname(?string $lastname): self
  65.     {
  66.         $this->lastname $lastname;
  67.         return $this;
  68.     }
  69.     public function getEmail(): ?string
  70.     {
  71.         return $this->email;
  72.     }
  73.     public function setEmail(string $email): self
  74.     {
  75.         $this->email $email;
  76.         return $this;
  77.     }
  78.     /**
  79.      * String representation of object
  80.      * @link http://php.net/manual/en/serializable.serialize.php
  81.      * @return string the string representation of the object or null
  82.      * @since 5.1.0
  83.      */
  84.     public function serialize()
  85.     {
  86.         return serialize(array(
  87.             $this->id,
  88.             $this->email,
  89.             $this->password,
  90.             $this->firstname,
  91.             $this->lastname,
  92.             $this->active,
  93.             $this->roles
  94.         ));
  95.     }
  96.     /**
  97.      * Constructs the object
  98.      * @link http://php.net/manual/en/serializable.unserialize.php
  99.      * @param string $serialized <p>
  100.      * The string representation of the object.
  101.      * </p>
  102.      * @return void
  103.      * @since 5.1.0
  104.      */
  105.     public function unserialize($serialized)
  106.     {
  107.         [
  108.             $this->id,
  109.             $this->email,
  110.             $this->password,
  111.             $this->firstname,
  112.             $this->lastname,
  113.             $this->active,
  114.             $this->roles] = unserialize($serialized);
  115.     }
  116.     /**
  117.      * Returns the roles granted to the user.
  118.      *
  119.      * <code>
  120.      * public function getRoles()
  121.      * {
  122.      *     return array('ROLE_USER');
  123.      * }
  124.      * </code>
  125.      *
  126.      * Alternatively, the roles might be stored on a ``roles`` property,
  127.      * and populated in any number of different ways when the user object
  128.      * is created.
  129.      *
  130.      * @return array (Role|string)[] The user roles
  131.      */
  132.     public function getRoles()
  133.     {
  134.         return (array)json_decode($this->rolestrue);
  135.     }
  136.     public function setRoles(array $roles): self
  137.     {
  138.         $this->roles json_encode($rolesJSON_PRETTY_PRINT);
  139.         return $this;
  140.     }
  141.     /**
  142.      * Set password
  143.      *
  144.      * @param string $password
  145.      */
  146.     public function setPassword($password)
  147.     {
  148.         $this->password $password;
  149.     }
  150.     /**
  151.      * Returns the password used to authenticate the user.
  152.      *
  153.      * This should be the encoded password. On authentication, a plain-text
  154.      * password will be salted, encoded, and then compared to this value.
  155.      *
  156.      * @return string The password
  157.      */
  158.     public function getPassword(): ?string
  159.     {
  160.         return $this->password;
  161.     }
  162.     /**
  163.      * Returns the salt that was originally used to encode the password.
  164.      *
  165.      * This can return null if the password was not encoded using a salt.
  166.      *
  167.      * @return string|null The salt
  168.      */
  169.     public function getSalt()
  170.     {
  171.         return null;
  172.     }
  173.     /**
  174.      * Returns the username used to authenticate the user.
  175.      *
  176.      * @return string The username
  177.      */
  178.     public function getUsername()
  179.     {
  180.         return $this->getEmail();
  181.     }
  182.     public function getUserIdentifier(): string
  183.     {
  184.         return $this->getEmail();
  185.     }
  186.     /**
  187.      * Removes sensitive data from the user.
  188.      *
  189.      * This is important if, at any given point, sensitive information like
  190.      * the plain-text password is stored on this object.
  191.      */
  192.     public function eraseCredentials()
  193.     {
  194.     }
  195.     /**
  196.      * Remove password from returnvalue
  197.      * @return array
  198.      */
  199.     public function forVue()
  200.     {
  201.         $driver $this->isDriver();
  202.         $picker $this->isPicker();
  203.         return [
  204.             'id' => (int)$this->getId(),
  205.             'admin' => !$driver && !$picker && $this->isAdmin(),
  206.             'driver' => !$picker && $driver,
  207.             'picker' => $picker,
  208.             'role' => $this->getRoleName(),
  209.             'firstname' => $this->getFirstname(),
  210.             'lastname' => $this->getLastname(),
  211.             'email' => $this->getEmail(),
  212.             'active' => $this->getActive(),
  213.         ];
  214.     }
  215.     public function getRoleName()
  216.     {
  217.         if ($this->isPicker()) {
  218.             return 'picker';
  219.         }
  220.         if ($this->isDriver()) {
  221.             return 'driver';
  222.         }
  223.         if ($this->isAdmin()) {
  224.             return 'admin';
  225.         }
  226.         return 'user';
  227.     }
  228.     public function isAdmin()
  229.     {
  230.         return \in_array('ROLE_ADMIN'$this->getRoles(), true);
  231.     }
  232.     public function isDriver()
  233.     {
  234.         return \in_array('ROLE_DRIVER'$this->getRoles(), true);
  235.     }
  236.     public function isPicker()
  237.     {
  238.         return \in_array('ROLE_PICKER'$this->getRoles(), true);
  239.     }
  240.     public function getActive(): ?bool
  241.     {
  242.         return $this->active;
  243.     }
  244.     public function setActive(?bool $active): self
  245.     {
  246.         $this->active $active;
  247.         return $this;
  248.     }
  249. }