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

  1. <?php
  2. namespace App\Modules\User\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  8. use App\Admin\Modules\User\Entity\UserAdmin;
  9. use App\Admin\Modules\Partner\Entity\UserPartner;
  10. use App\Admin\Modules\Voucher\Entity\VoucherPackageVoucher;
  11. use App\Admin\Modules\Contract\Entity\UserContractTrader;
  12. use App\Admin\Modules\Contract\Entity\UserContractSubstantiveDepartmentEmployee;
  13. use App\Admin\SuperAdmin\Modules\Trader\Entity\UserTrader;
  14. /**
  15.  * @ORM\Table(name="user", indexes={@ORM\Index(name="username", columns={"username"}), @ORM\Index(name="email", columns={"email"}), @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="api_key", columns={"api_key"}), @ORM\Index(name="api_auth_token", columns={"api_auth_token"}), @ORM\Index(name="added_at", columns={"added_at"})})
  16.  * @ORM\Entity(repositoryClass="App\Modules\User\Repository\UserRepository")
  17.  */
  18. class User implements UserInterfacePasswordAuthenticatedUserInterface\Serializable
  19. {
  20.     const TYPE_SUPER_ADMIN 1;
  21.     const TYPE_ADMIN 2;
  22.     const TYPE_PARTNER 3;
  23.     const TYPE_USER 4;
  24.     const TYPE_TRADER 5;
  25.     const TYPE_CONTRACT_TRADER 6;
  26.     const TYPE_CONTRACT_SUBSTANTIVE_DEPARTMENT_EMPLOYEE 7;
  27.     
  28.     const SYSTEM_ADMIN_SYSTEM_ID 'sysonline';
  29.     const SYSTEM_VOUCHER_ADMIN_SYSTEM_ID 'voucher';
  30.     /**
  31.      * @ORM\Column(type="integer")
  32.      * @ORM\Id
  33.      * @ORM\GeneratedValue(strategy="AUTO")
  34.      */
  35.     private $id;
  36.     /**
  37.      * @ORM\Column(type="string", length=255)
  38.      */
  39.     private $username;
  40.     /**
  41.      * @ORM\Column(type="string", length=64)
  42.      */
  43.     private $password;
  44.     /**
  45.      * @ORM\Column(type="string", length=255)
  46.      */
  47.     private $email;
  48.     /**
  49.      * @var \App\Admin\Modules\Voucher\Entity\VoucherPackageVoucher
  50.      *
  51.      * @ORM\OneToOne(targetEntity="App\Admin\Modules\Voucher\Entity\VoucherPackageVoucher", mappedBy="user")
  52.      * @ORM\JoinColumns({
  53.      *   @ORM\JoinColumn(name="id_voucher_package_voucher", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  54.      * })
  55.      */
  56.     private $voucher;
  57.     /**
  58.      * @ORM\Column(name="api_key", type="string", length=100, nullable=true)
  59.      */
  60.     private $apiKey;
  61.     
  62.     /**
  63.      * @ORM\Column(name="api_auth_token", type="string", length=100, nullable=true)
  64.      */
  65.     private $apiAuthToken;
  66.     /**
  67.      * @ORM\Column(name="selected_lang", type="string", length=2, nullable=true)
  68.      */
  69.     private $selectedLang;
  70.     /**
  71.      * @var int
  72.      *
  73.      * @ORM\Column(name="type", type="smallint", nullable=false)
  74.      */
  75.     private $type;
  76.  
  77.     /**
  78.      * @ORM\Column(name="system_id", type="string", length=255, nullable=true)
  79.      */
  80.     private $systemId;
  81.     /**
  82.      * @ORM\Column(name="is_active", type="boolean")
  83.      */
  84.     private $isActive;
  85.     /**
  86.      * @var \DateTime
  87.      *
  88.      * @ORM\Column(name="added_at", type="datetime", nullable=false)
  89.      * @Gedmo\Timestampable(on="create")
  90.      */
  91.     private $addedAt;
  92.     /**
  93.      * @var \App\Admin\Modules\User\Entity\UserAdmin
  94.      *
  95.      * @ORM\OneToMany(targetEntity="App\Admin\Modules\User\Entity\UserAdmin", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  96.      */
  97.     private $admin
  98.     /**
  99.      * @var \App\Admin\Modules\Partner\Entity\UserPartner
  100.      *
  101.      * @ORM\OneToMany(targetEntity="App\Admin\Modules\Partner\Entity\UserPartner", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  102.      */
  103.     private $partner
  104.     /**
  105.      * @var \App\Admin\SuperAdmin\Modules\Trader\Entity\UserTrader
  106.      *
  107.      * @ORM\OneToMany(targetEntity="App\Admin\SuperAdmin\Modules\Trader\Entity\UserTrader", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  108.      */
  109.     private $trader
  110.     /**
  111.      * @var \App\Admin\Modules\Contract\Entity\UserContractTrader
  112.      *
  113.      * @ORM\OneToMany(targetEntity="App\Admin\Modules\Contract\Entity\UserContractTrader", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  114.      */
  115.     private $contractTrader
  116.     /**
  117.      * @var \App\Admin\Modules\Contract\Entity\UserContractSubstantiveDepartmentEmployee
  118.      *
  119.      * @ORM\OneToMany(targetEntity="App\Admin\Modules\Contract\Entity\UserContractSubstantiveDepartmentEmployee", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  120.      */
  121.     private $contractSubstantiveDepartmentEmployee
  122.     public function __construct()
  123.     {
  124.         $this->isActive false;
  125.         // may not be needed, see section on salt below
  126.         // $this->salt = md5(uniqid('', true));
  127.         $this->admin = new ArrayCollection();
  128.         $this->partner = new ArrayCollection();
  129.         $this->trader = new ArrayCollection();
  130.         $this->contractTrader = new ArrayCollection();
  131.         $this->contractSubstantiveDepartmentEmployee = new ArrayCollection();
  132.     }
  133.     /** @see \Serializable::serialize() */
  134.     public function serialize()
  135.     {
  136.         return serialize(array(
  137.             $this->id,
  138.             $this->username,
  139.             $this->password,
  140.             // see section on salt below
  141.             // $this->salt,
  142.         ));
  143.     }
  144.     /** @see \Serializable::unserialize() */
  145.     public function unserialize($serialized)
  146.     {
  147.         list (
  148.             $this->id,
  149.             $this->username,
  150.             $this->password,
  151.             // see section on salt below
  152.             // $this->salt
  153.         ) = unserialize($serialized, array('allowed_classes' => false));
  154.     }
  155.     
  156.     private function _getFromCollection($propertyName)
  157.     {
  158.         if ($this->$propertyName && count($this->$propertyName))
  159.             return $this->$propertyName->first();
  160.         else
  161.             return null;        
  162.     }
  163.     
  164.     private function _addToCollection($propertyName$value)
  165.     {
  166.         if (!$this->$propertyName)
  167.             $this->$propertyName = new ArrayCollection();
  168.         elseif (count($this->$propertyName))
  169.             $this->$propertyName->removeElement($this->$propertyName->first());
  170.         
  171.         $value->setUser($this);
  172.         
  173.         $this->$propertyName[] = $value;
  174.         return $this;
  175.     }
  176.     public function getAdmin(): ?UserAdmin
  177.     {
  178.         return $this->_getFromCollection('admin');
  179.     }
  180.     public function setAdmin(?UserAdmin $admin): self
  181.     {
  182.         return $this->_addToCollection('admin'$admin);
  183.     }    
  184.     public function getPartner(): ?UserPartner
  185.     {
  186.         return $this->_getFromCollection('partner');
  187.     }
  188.     public function setPartner(?UserPartner $partner): self
  189.     {
  190.         return $this->_addToCollection('partner'$partner);
  191.     }    
  192.     
  193.     public function getTrader(): ?UserTrader
  194.     {
  195.         return $this->_getFromCollection('trader');
  196.     }
  197.     public function setTrader(?UserTrader $trader): self
  198.     {
  199.         return $this->_addToCollection('trader'$trader);
  200.     }
  201.     public function getContractTrader(): ?UserContractTrader
  202.     {
  203.         return $this->_getFromCollection('contractTrader');
  204.     }
  205.     public function setContractTrader(?UserContractTrader $trader): self
  206.     {
  207.         return $this->_addToCollection('contractTrader'$trader);
  208.     }
  209.     
  210.     public function getContractTraderNameWithId()
  211.     {
  212.         return $this->contractTrader[0]->getNameWithId();
  213.     }
  214.     public function getContractSubstantiveDepartmentEmployee(): ?UserContractSubstantiveDepartmentEmployee
  215.     {
  216.         return $this->_getFromCollection('contractSubstantiveDepartmentEmployee');
  217.     }
  218.     public function setContractSubstantiveDepartmentEmployee(?UserContractSubstantiveDepartmentEmployee $employee): self
  219.     {
  220.         return $this->_addToCollection('contractSubstantiveDepartmentEmployee'$employee);
  221.     }
  222.     public function getContractSubstantiveDepartmentEmployeeNameWithId()
  223.     {
  224.         return $this->contractSubstantiveDepartmentEmployee[0]->getNameWithId();
  225.     }
  226.     public function getUserIdentifier(): string
  227.     {
  228.         return $this->username;
  229.     }
  230.     public function getId(): ?int
  231.     {
  232.         return $this->id;
  233.     }
  234.     
  235.     public function getDisplayName()
  236.     {
  237.         switch($this->type)
  238.         {
  239.             case self::TYPE_ADMIN:
  240.                 return $this->getAdmin()->getCompanyName();
  241.             case self::TYPE_PARTNER:
  242.                 return $this->getPartner()->getName();
  243.             case self::TYPE_TRADER:
  244.                 return $this->getTrader()->getFirstName() . ' ' $this->getTrader()->getLastName();
  245.             case self::TYPE_CONTRACT_TRADER:
  246.                 return $this->getContractTrader()->getFirstName() . ' ' $this->getContractTrader()->getLastName();
  247.             case self::TYPE_CONTRACT_SUBSTANTIVE_DEPARTMENT_EMPLOYEE:
  248.                 return $this->getContractSubstantiveDepartmentEmployee()->getFirstName() . ' ' $this->getContractSubstantiveDepartmentEmployee()->getFirstName();
  249.             default:
  250.                 return $this->username;
  251.         }        
  252.     }
  253.     
  254.     public function getFullApiKey()
  255.     {
  256.         if ($this->apiKey != '')
  257.             return $this->id '.' $this->apiKey;
  258.     }
  259.     public function getEmail(): ?string
  260.     {
  261.         return $this->email;
  262.     }
  263.     public function setEmail(string $email): self
  264.     {
  265.         $this->email $email;
  266.         return $this;
  267.     }
  268.     public function getIsActive(): ?bool
  269.     {
  270.         return $this->isActive;
  271.     }
  272.     public function setIsActive(?bool $isActive): self
  273.     {
  274.         $this->isActive $isActive;
  275.         return $this;
  276.     }
  277.     public function setUsername(string $username): self
  278.     {
  279.         $this->username $username;
  280.         return $this;
  281.     }
  282.     public function getUsername()
  283.     {
  284.         return $this->username;
  285.     }
  286.     public function getSalt()
  287.     {
  288.         // you *may* need a real salt depending on your encoder
  289.         // see section on salt below
  290.         return null;
  291.     }
  292.     public function getPassword(): ?string
  293.     {
  294.         return $this->password;
  295.     }
  296.     public function setPassword(string $password): self
  297.     {
  298.         $this->password $password;
  299.         return $this;
  300.     }
  301.     public function getRoles(): array
  302.     {
  303.         switch($this->type)
  304.         {
  305.             case self::TYPE_SUPER_ADMIN:
  306.                 return ['ROLE_SUPER_ADMIN'];
  307.             case self::TYPE_ADMIN:
  308.                 return ['ROLE_ADMIN'];
  309.             case self::TYPE_PARTNER:
  310.                 return ['ROLE_PARTNER'];
  311.             case self::TYPE_TRADER:
  312.                 return ['ROLE_TRADER'];
  313.             case self::TYPE_CONTRACT_TRADER:
  314.                 return ['ROLE_CONTRACT_TRADER'];
  315.             case self::TYPE_CONTRACT_SUBSTANTIVE_DEPARTMENT_EMPLOYEE:
  316.                 return ['ROLE_CONTRACT_SUBSTANTIVE_DEPARTMENT_EMPLOYEE'];
  317.             case self::TYPE_USER:
  318.                 return ['ROLE_USER'];
  319.             default:
  320.                 throw new \Exception('Unknown user.');
  321.         }
  322.     }
  323.     public function eraseCredentials()
  324.     {
  325.     }
  326.     public function getType(): ?int
  327.     {
  328.         return $this->type;
  329.     }
  330.     public function setType(int $type): self
  331.     {
  332.         $this->type $type;
  333.         return $this;
  334.     }
  335.     public function getSelectedLang(): ?string
  336.     {
  337.         return $this->selectedLang;
  338.     }
  339.     public function setSelectedLang(?string $selectedLang): self
  340.     {
  341.         $this->selectedLang $selectedLang;
  342.         return $this;
  343.     }
  344.     public function isIsActive(): ?bool
  345.     {
  346.         return $this->isActive;
  347.     }
  348.     public function getVoucher(): ?VoucherPackageVoucher
  349.     {
  350.         return $this->voucher;
  351.     }
  352.     public function setVoucher(?VoucherPackageVoucher $voucherbool $setVoucherUser true): self
  353.     {
  354.         if ($voucher && $setVoucherUser)
  355.             $voucher->setUser($thisfalse);
  356.         $this->voucher $voucher;
  357.         
  358.         return $this;
  359.     }
  360.     public function getAddedAt(): ?\DateTimeInterface
  361.     {
  362.         return $this->addedAt;
  363.     }
  364.     public function setAddedAt(\DateTimeInterface $addedAt): self
  365.     {
  366.         $this->addedAt $addedAt;
  367.         return $this;
  368.     }
  369.     public function getSystemId(): ?string
  370.     {
  371.         return $this->systemId;
  372.     }
  373.     public function setSystemId(?string $systemId): self
  374.     {
  375.         $this->systemId $systemId;
  376.         return $this;
  377.     }
  378.     public function getApiKey(): ?string
  379.     {
  380.         return $this->apiKey;
  381.     }
  382.     public function setApiKey(?string $apiKey): self
  383.     {
  384.         $this->apiKey $apiKey;
  385.         return $this;
  386.     }
  387.     public function getApiAuthToken(): ?string
  388.     {
  389.         return $this->apiAuthToken;
  390.     }
  391.     public function setApiAuthToken(?string $apiAuthToken): self
  392.     {
  393.         $this->apiAuthToken $apiAuthToken;
  394.         return $this;
  395.     }
  396. }