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