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