src/Entity/Admin.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Traits\CreatedUpdatedTrait;
  5. use JMS\Serializer\Annotation\Expose;
  6. use JMS\Serializer\Annotation\ExclusionPolicy;
  7. use Symfony\Component\Security\Core\User\EquatableInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10.  * @ORM\Table(name="v_admin")
  11.  * @ORM\Entity(repositoryClass="App\Repository\AdminRepository")
  12.  * @ExclusionPolicy("all")
  13.  */
  14. class Admin implements UserInterfaceEquatableInterface
  15. {
  16.     public function isEqualTo(UserInterface $user)
  17.     {
  18.         return ($user->getUsername() == $this->email && $user->getPassword() == $this->password);
  19.     }
  20.     /**
  21.      * @ORM\Id()
  22.      * @ORM\GeneratedValue()
  23.      * @ORM\Column(type="integer")
  24.      * @Expose
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\Column(type="string", length=255)
  29.      * @Expose
  30.      */
  31.     private $name;
  32.     /**
  33.      * @ORM\Column(type="string", length=255, unique=true)
  34.      * @Expose
  35.      */
  36.     private $email;
  37.     /**
  38.      * @ORM\Column(type="string", length=80, nullable=true)
  39.      * @Expose
  40.      */
  41.     private $phone;
  42.     /**
  43.      * @var string[]
  44.      * @ORM\Column(type="json", nullable=true)
  45.      */
  46.     private $roles;
  47.     /**
  48.      * @var string Salt
  49.      * @ORM\Column(type="string")
  50.      */
  51.     private $salt;
  52.     /**
  53.      * @var string The hashed password
  54.      * @ORM\Column(type="string")
  55.      */
  56.     private $password;
  57.     /**
  58.      * Plain password. Used for model validation. Must not be persisted.
  59.      * @var string
  60.      */
  61.     private $plainPassword;
  62.     /**
  63.      * @var boolean
  64.      * @ORM\Column(type="boolean")
  65.      * @Expose
  66.      */
  67.     private $confirmed true;
  68.     /**
  69.      * @var boolean
  70.      * @ORM\Column(type="boolean")
  71.      * @Expose
  72.      */
  73.     private $enabled true;
  74.     /**
  75.      * @ORM\ManyToMany(targetEntity="App\Entity\Group")
  76.      */
  77.     protected  $groups;
  78.     use CreatedUpdatedTrait;
  79.     public function __construct()
  80.     {
  81.         $this->salt base_convert(sha1(uniqid(mt_rand(), true)), 1636);
  82.     }
  83.     public function __toString()
  84.     {
  85.         return (string) $this->name;
  86.     }
  87.     public function getId(): ?int
  88.     {
  89.         return $this->id;
  90.     }
  91.     /**
  92.      * @return mixed
  93.      */
  94.     public function getName()
  95.     {
  96.         return $this->name;
  97.     }
  98.     /**
  99.      * @param mixed $name
  100.      */
  101.     public function setName($name): void
  102.     {
  103.         $this->name $name;
  104.     }
  105.     public function getEmail(): ?string
  106.     {
  107.         return $this->email;
  108.     }
  109.     public function setEmail(string $email): self
  110.     {
  111.         $this->email $email;
  112.         return $this;
  113.     }
  114.     /**
  115.      * @return mixed
  116.      */
  117.     public function getPhone()
  118.     {
  119.         return $this->phone;
  120.     }
  121.     /**
  122.      * @param mixed $phone
  123.      */
  124.     public function setPhone($phone): void
  125.     {
  126.         $this->phone $phone;
  127.     }
  128.     /**
  129.      * A visual identifier that represents this user.
  130.      *
  131.      * @see UserInterface
  132.      */
  133.     public function getUsername(): string
  134.     {
  135.         return (string) $this->email;
  136.     }
  137.     /**
  138.      * @see UserInterface
  139.      */
  140.     public function getRoles(): array
  141.     {
  142.         $roles $this->roles;
  143.         // guarantee every user at least has ROLE_USER
  144.         $roles[] = 'ROLE_USER';
  145.         foreach ($this->getGroups() as $group)
  146.         {
  147.             if(count($group->getRoles()) > 0) {
  148.                 foreach ($group->getRoles() as $role) {
  149.                     $roles[] = $role;
  150.                 }
  151.             }
  152.         }
  153.         return array_unique($roles);
  154.     }
  155.     public function setRoles(array $roles): self
  156.     {
  157.         $this->roles $roles;
  158.         return $this;
  159.     }
  160.     public function addRole($role)
  161.     {
  162.         $role strtoupper($role);
  163.         if (!in_array($role$this->rolestrue)) {
  164.             $this->roles[] = $role;
  165.         }
  166.         return $this;
  167.     }
  168.     /**
  169.      * @see UserInterface
  170.      */
  171.     public function getPassword(): string
  172.     {
  173.         return (string) $this->password;
  174.     }
  175.     public function setPassword(string $password): self
  176.     {
  177.         $this->password $password;
  178.         return $this;
  179.     }
  180.     /**
  181.      * @return string
  182.      */
  183.     public function getPlainPassword(): ?string
  184.     {
  185.         return $this->plainPassword;
  186.     }
  187.     /**
  188.      * @param string $plainPassword
  189.      */
  190.     public function setPlainPassword(string $plainPassword): void
  191.     {
  192.         $this->plainPassword $plainPassword;
  193.     }
  194.     public function getSalt(): ?string
  195.     {
  196.         return $this->salt;
  197.     }
  198.     /**
  199.      * @see UserInterface
  200.      */
  201.     public function eraseCredentials()
  202.     {
  203.         // If you store any temporary, sensitive data on the user, clear it here
  204.         // $this->plainPassword = null;
  205.     }
  206.     /**
  207.      * @return bool
  208.      */
  209.     public function isConfirmed(): bool
  210.     {
  211.         return $this->confirmed;
  212.     }
  213.     /**
  214.      * @param bool $confirmed
  215.      */
  216.     public function setConfirmed(bool $confirmed): void
  217.     {
  218.         $this->confirmed $confirmed;
  219.     }
  220.     /**
  221.      * @return bool
  222.      */
  223.     public function isEnabled(): bool
  224.     {
  225.         return $this->enabled;
  226.     }
  227.     /**
  228.      * @param bool $enabled
  229.      */
  230.     public function setEnabled(bool $enabled): void
  231.     {
  232.         $this->enabled $enabled;
  233.     }
  234.     /**
  235.      * @return mixed
  236.      */
  237.     public function getGroups()
  238.     {
  239.         return $this->groups;
  240.     }
  241.     /**
  242.      * @param mixed $groups
  243.      */
  244.     public function setGroups($groups): void
  245.     {
  246.         $this->groups $groups;
  247.     }
  248. }