src/Modules/User/Entity/User.php line 19

  1. <?php
  2. namespace App\Modules\User\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\DBAL\Types\Types;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use App\Admin\Modules\User\Entity\UserAdmin;
  11. use App\Admin\Modules\Contract\Entity\ContractClient;
  12. /**
  13.  * @ORM\Table(name="user", indexes={@ORM\Index(name="username", columns={"username"}), @ORM\Index(name="email", columns={"email"}), @ORM\Index(name="first_name", columns={"first_name"}), @ORM\Index(name="last_name", columns={"last_name"}), @ORM\Index(name="type", columns={"type"}), @ORM\Index(name="system_id", columns={"system_id"}), @ORM\Index(name="is_active", columns={"is_active"}), @ORM\Index(name="added_at", columns={"added_at"})})
  14.  * @ORM\Entity
  15.  */
  16. class User implements UserInterfacePasswordAuthenticatedUserInterface\Serializable
  17. {
  18.     const TYPE_APP_ADMIN 1;
  19.     const TYPE_SUPER_ADMIN 2;
  20.     const TYPE_ADMIN 3;
  21.     const TYPE_CLIENT 4;
  22.     const TYPE_HR_SPECIALIST 5;
  23.     const TYPE_ACCOUNTANT 6;
  24.     /**
  25.      * @ORM\Column(type="integer")
  26.      * @ORM\Id
  27.      * @ORM\GeneratedValue(strategy="AUTO")
  28.      */
  29.     private $id;
  30.     /**
  31.      * @ORM\Column(name="username", type="string", length=255)
  32.      */
  33.     private $username;
  34.     /**
  35.      * @ORM\Column(name="password", type="string", length=64)
  36.      */
  37.     private $password;
  38.     /**
  39.      * @ORM\Column(name="email", type="string", length=255, nullable=true)
  40.      */
  41.     private $email;
  42.     /**
  43.      * @ORM\Column(name="first_name", type="string", length=255, nullable=true)
  44.      */
  45.     private $firstName;
  46.     /**
  47.      * @ORM\Column(name="last_name", type="string", length=255, nullable=true)
  48.      */
  49.     private $lastName;
  50.     /**
  51.      * @ORM\Column(name="screen_locker_pin", type="string", length=25, nullable=true)
  52.      */
  53.     private $screenLockerPin;
  54.     /**
  55.      * @ORM\Column(name="selected_lang", type="string", length=2, nullable=true)
  56.      */
  57.     private $selectedLang;
  58.     /**
  59.      * @var int
  60.      *
  61.      * @ORM\Column(name="type", type="smallint", nullable=false)
  62.      */
  63.     private $type;
  64.  
  65.     /**
  66.      * @ORM\Column(name="system_id", type="string", length=255, nullable=true)
  67.      */
  68.     private $systemId;
  69.     /**
  70.      * @ORM\Column(name="is_active", type="boolean")
  71.      */
  72.     private $isActive;
  73.     /**
  74.      * @var \DateTime
  75.      *
  76.      * @ORM\Column(name="added_at", type="datetime", nullable=false)
  77.      * @Gedmo\Timestampable(on="create")
  78.      */
  79.     private $addedAt;
  80.     /**
  81.      * @var \App\Admin\Modules\User\Entity\UserAdmin
  82.      *
  83.      * @ORM\OneToMany(targetEntity="App\Admin\Modules\User\Entity\UserAdmin", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  84.      */
  85.     private $admin
  86.     /**
  87.      * @var \App\Admin\Modules\Contract\Entity\ContractClient
  88.      *
  89.      * @ORM\OneToMany(targetEntity="App\Admin\Modules\Contract\Entity\ContractClient", mappedBy="user", cascade={"persist"})
  90.      */
  91.     private $client
  92.     public function __construct()
  93.     {
  94.         $this->isActive false;
  95.         // may not be needed, see section on salt below
  96.         // $this->salt = md5(uniqid('', true));
  97.         $this->admin = new ArrayCollection();
  98.         $this->client = new ArrayCollection();
  99.     }
  100.     /** @see \Serializable::serialize() */
  101.     public function serialize()
  102.     {
  103.         return serialize(array(
  104.             $this->id,
  105.             $this->username,
  106.             $this->password,
  107.             // see section on salt below
  108.             // $this->salt,
  109.         ));
  110.     }
  111.     /** @see \Serializable::unserialize() */
  112.     public function unserialize($serialized)
  113.     {
  114.         list (
  115.             $this->id,
  116.             $this->username,
  117.             $this->password,
  118.             // see section on salt below
  119.             // $this->salt
  120.         ) = unserialize($serialized, array('allowed_classes' => false));
  121.     }
  122.     
  123.     private function _getFromCollection($propertyName)
  124.     {
  125.         if ($this->$propertyName && count($this->$propertyName))
  126.             return $this->$propertyName->first();
  127.         else
  128.             return null;        
  129.     }
  130.     
  131.     private function _addToCollection($propertyName$value)
  132.     {
  133.         if (!$this->$propertyName)
  134.             $this->$propertyName = new ArrayCollection();
  135.         elseif (count($this->$propertyName))
  136.             $this->$propertyName->removeElement($this->$propertyName->first());
  137.         
  138.         $value->setUser($this);
  139.         
  140.         $this->$propertyName[] = $value;
  141.         return $this;
  142.     }
  143.     
  144.     public function getName(): string
  145.     {
  146.         return $this->firstName ' ' $this->lastName;
  147.     }    
  148.     public function getAdmin(): ?UserAdmin
  149.     {
  150.         return $this->_getFromCollection('admin');
  151.     }
  152.     public function setAdmin(?UserAdmin $admin): self
  153.     {
  154.         return $this->_addToCollection('admin'$admin);
  155.     }    
  156.     public function getClient(): ?ContractClient
  157.     {
  158.         return $this->_getFromCollection('client');
  159.     }
  160.     public function setClient(?ContractClient $client): self
  161.     {
  162.         return $this->_addToCollection('client'$client);
  163.     }    
  164.     public function getUserIdentifier(): string
  165.     {
  166.         return $this->username;
  167.     }
  168.     public function getId(): ?int
  169.     {
  170.         return $this->id;
  171.     }
  172.     public function getEmail(): ?string
  173.     {
  174.         return $this->email;
  175.     }
  176.     public function setEmail(?string $email): self
  177.     {
  178.         $this->email $email;
  179.         return $this;
  180.     }
  181.     public function getIsActive(): ?bool
  182.     {
  183.         return $this->isActive;
  184.     }
  185.     public function setIsActive(bool $isActive): self
  186.     {
  187.         $this->isActive $isActive;
  188.         return $this;
  189.     }
  190.     public function setUsername(string $username): self
  191.     {
  192.         $this->username $username;
  193.         return $this;
  194.     }
  195.     public function getUsername()
  196.     {
  197.         return $this->username;
  198.     }
  199.     public function getSalt()
  200.     {
  201.         // you *may* need a real salt depending on your encoder
  202.         // see section on salt below
  203.         return null;
  204.     }
  205.     public function getPassword(): ?string
  206.     {
  207.         return $this->password;
  208.     }
  209.     public function setPassword(string $password): self
  210.     {
  211.         $this->password $password;
  212.         return $this;
  213.     }
  214.     public function getRoles(): array
  215.     {
  216.         switch($this->type)
  217.         {
  218.             case self::TYPE_APP_ADMIN:
  219.                 return ['ROLE_APP_ADMIN'];
  220.             case self::TYPE_SUPER_ADMIN:
  221.                 return ['ROLE_SUPER_ADMIN'];
  222.             case self::TYPE_ADMIN:
  223.                 return ['ROLE_ADMIN'];
  224.             case self::TYPE_HR_SPECIALIST:
  225.                 return ['ROLE_HR_SPECIALIST'];
  226.             case self::TYPE_ACCOUNTANT:
  227.                 return ['ROLE_ACCOUNTANT'];
  228.             case self::TYPE_CLIENT:
  229.                 return ['ROLE_CLIENT'];
  230.             default:
  231.                 throw new \Exception('Unknown user.');
  232.         }
  233.     }
  234.     public function eraseCredentials()
  235.     {
  236.     }
  237.     public function getType(): ?int
  238.     {
  239.         return $this->type;
  240.     }
  241.     public function setType(int $type): self
  242.     {
  243.         $this->type $type;
  244.         return $this;
  245.     }
  246.     public function getSelectedLang(): ?string
  247.     {
  248.         return $this->selectedLang;
  249.     }
  250.     public function setSelectedLang(?string $selectedLang): self
  251.     {
  252.         $this->selectedLang $selectedLang;
  253.         return $this;
  254.     }
  255.     public function isIsActive(): ?bool
  256.     {
  257.         return $this->isActive;
  258.     }
  259.     public function getAddedAt(): ?\DateTimeInterface
  260.     {
  261.         return $this->addedAt;
  262.     }
  263.     public function setAddedAt(\DateTimeInterface $addedAt): self
  264.     {
  265.         $this->addedAt $addedAt;
  266.         return $this;
  267.     }
  268.     public function getSystemId(): ?string
  269.     {
  270.         return $this->systemId;
  271.     }
  272.     public function setSystemId(?string $systemId): self
  273.     {
  274.         $this->systemId $systemId;
  275.         return $this;
  276.     }
  277.     public function getScreenLockerPin(): ?string
  278.     {
  279.         return $this->screenLockerPin;
  280.     }
  281.     public function setScreenLockerPin(?string $screenLockerPin): self
  282.     {
  283.         $this->screenLockerPin $screenLockerPin;
  284.         return $this;
  285.     }
  286.     public function getFirstName(): ?string
  287.     {
  288.         return $this->firstName;
  289.     }
  290.     public function setFirstName(?string $firstName): self
  291.     {
  292.         $this->firstName $firstName;
  293.         return $this;
  294.     }
  295.     public function getLastName(): ?string
  296.     {
  297.         return $this->lastName;
  298.     }
  299.     public function setLastName(?string $lastName): self
  300.     {
  301.         $this->lastName $lastName;
  302.         return $this;
  303.     }
  304. }