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.     public function __toString()
  101.     {
  102.         return $this->getName();
  103.     }
  104.     /** @see \Serializable::serialize() */
  105.     public function serialize()
  106.     {
  107.         return serialize(array(
  108.             $this->id,
  109.             $this->username,
  110.             $this->password,
  111.             // see section on salt below
  112.             // $this->salt,
  113.         ));
  114.     }
  115.     /** @see \Serializable::unserialize() */
  116.     public function unserialize($serialized)
  117.     {
  118.         list (
  119.             $this->id,
  120.             $this->username,
  121.             $this->password,
  122.             // see section on salt below
  123.             // $this->salt
  124.         ) = unserialize($serialized, array('allowed_classes' => false));
  125.     }
  126.     
  127.     private function _getFromCollection($propertyName)
  128.     {
  129.         if ($this->$propertyName && count($this->$propertyName))
  130.             return $this->$propertyName->first();
  131.         else
  132.             return null;        
  133.     }
  134.     
  135.     private function _addToCollection($propertyName$value)
  136.     {
  137.         if (!$this->$propertyName)
  138.             $this->$propertyName = new ArrayCollection();
  139.         elseif (count($this->$propertyName))
  140.             $this->$propertyName->removeElement($this->$propertyName->first());
  141.         
  142.         $value->setUser($this);
  143.         
  144.         $this->$propertyName[] = $value;
  145.         return $this;
  146.     }
  147.     
  148.     public function getName(): string
  149.     {
  150.         return $this->firstName ' ' $this->lastName;
  151.     }    
  152.     public function getAdmin(): ?UserAdmin
  153.     {
  154.         return $this->_getFromCollection('admin');
  155.     }
  156.     public function setAdmin(?UserAdmin $admin): self
  157.     {
  158.         return $this->_addToCollection('admin'$admin);
  159.     }    
  160.     public function getClient(): ?ContractClient
  161.     {
  162.         return $this->_getFromCollection('client');
  163.     }
  164.     public function setClient(?ContractClient $client): self
  165.     {
  166.         return $this->_addToCollection('client'$client);
  167.     }    
  168.     public function getUserIdentifier(): string
  169.     {
  170.         return $this->username;
  171.     }
  172.     public function getId(): ?int
  173.     {
  174.         return $this->id;
  175.     }
  176.     public function getEmail(): ?string
  177.     {
  178.         return $this->email;
  179.     }
  180.     public function setEmail(?string $email): self
  181.     {
  182.         $this->email $email;
  183.         return $this;
  184.     }
  185.     public function getIsActive(): ?bool
  186.     {
  187.         return $this->isActive;
  188.     }
  189.     public function setIsActive(bool $isActive): self
  190.     {
  191.         $this->isActive $isActive;
  192.         return $this;
  193.     }
  194.     public function setUsername(string $username): self
  195.     {
  196.         $this->username $username;
  197.         return $this;
  198.     }
  199.     public function getUsername()
  200.     {
  201.         return $this->username;
  202.     }
  203.     public function getSalt()
  204.     {
  205.         // you *may* need a real salt depending on your encoder
  206.         // see section on salt below
  207.         return null;
  208.     }
  209.     public function getPassword(): ?string
  210.     {
  211.         return $this->password;
  212.     }
  213.     public function setPassword(string $password): self
  214.     {
  215.         $this->password $password;
  216.         return $this;
  217.     }
  218.     public function getRoles(): array
  219.     {
  220.         switch($this->type)
  221.         {
  222.             case self::TYPE_APP_ADMIN:
  223.                 return ['ROLE_APP_ADMIN'];
  224.             case self::TYPE_SUPER_ADMIN:
  225.                 return ['ROLE_SUPER_ADMIN'];
  226.             case self::TYPE_ADMIN:
  227.                 return ['ROLE_ADMIN'];
  228.             case self::TYPE_HR_SPECIALIST:
  229.                 return ['ROLE_HR_SPECIALIST'];
  230.             case self::TYPE_ACCOUNTANT:
  231.                 return ['ROLE_ACCOUNTANT'];
  232.             case self::TYPE_CLIENT:
  233.                 return ['ROLE_CLIENT'];
  234.             default:
  235.                 throw new \Exception('Unknown user.');
  236.         }
  237.     }
  238.     public function eraseCredentials()
  239.     {
  240.     }
  241.     public function getType(): ?int
  242.     {
  243.         return $this->type;
  244.     }
  245.     public function setType(int $type): self
  246.     {
  247.         $this->type $type;
  248.         return $this;
  249.     }
  250.     public function getSelectedLang(): ?string
  251.     {
  252.         return $this->selectedLang;
  253.     }
  254.     public function setSelectedLang(?string $selectedLang): self
  255.     {
  256.         $this->selectedLang $selectedLang;
  257.         return $this;
  258.     }
  259.     public function isIsActive(): ?bool
  260.     {
  261.         return $this->isActive;
  262.     }
  263.     public function getAddedAt(): ?\DateTimeInterface
  264.     {
  265.         return $this->addedAt;
  266.     }
  267.     public function setAddedAt(\DateTimeInterface $addedAt): self
  268.     {
  269.         $this->addedAt $addedAt;
  270.         return $this;
  271.     }
  272.     public function getSystemId(): ?string
  273.     {
  274.         return $this->systemId;
  275.     }
  276.     public function setSystemId(?string $systemId): self
  277.     {
  278.         $this->systemId $systemId;
  279.         return $this;
  280.     }
  281.     public function getScreenLockerPin(): ?string
  282.     {
  283.         return $this->screenLockerPin;
  284.     }
  285.     public function setScreenLockerPin(?string $screenLockerPin): self
  286.     {
  287.         $this->screenLockerPin $screenLockerPin;
  288.         return $this;
  289.     }
  290.     public function getFirstName(): ?string
  291.     {
  292.         return $this->firstName;
  293.     }
  294.     public function setFirstName(?string $firstName): self
  295.     {
  296.         $this->firstName $firstName;
  297.         return $this;
  298.     }
  299.     public function getLastName(): ?string
  300.     {
  301.         return $this->lastName;
  302.     }
  303.     public function setLastName(?string $lastName): self
  304.     {
  305.         $this->lastName $lastName;
  306.         return $this;
  307.     }
  308. }