src/Diplix/KMGBundle/Entity/User.php line 20

Open in your IDE?
  1. <?php
  2. namespace Diplix\KMGBundle\Entity;
  3. use Diplix\KMGBundle\Entity\Accounting\CoopMember;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  9. use Symfony\Component\Security\Core\User\EquatableInterface;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. /**
  12.  * @ORM\Table(name="users", indexes={@ORM\Index(name="be_deleted",columns={"be_deleted"})})
  13.  * @ORM\Entity(repositoryClass="Diplix\KMGBundle\Repository\UserRepository")
  14.  */
  15. class User extends BasicEntity implements     UserInterface,
  16.                                             PasswordAuthenticatedUserInterface,
  17.                                             EquatableInterface,
  18.                                             \Serializable,
  19.                                             \JsonSerializable
  20. {
  21.     const MAIL_ACTION_NONE 0;
  22.     const MAIL_ACTION_REGISTER 1;
  23.     const MAIL_ACTION_RESET_PASSWORD 2;
  24.     public static $roles_as_object false;
  25.     /**
  26.      * @ORM\Column(type="integer",name="id")
  27.      * @ORM\Id
  28.      * @ORM\GeneratedValue(strategy="AUTO")
  29.      */
  30.     private $id;
  31.     /**
  32.      * @ORM\Column(type="string", length=128)
  33.      */
  34.     private $username;
  35.     /**
  36.      * @ORM\Column(type="string", length=128)
  37.      */
  38.     private $email;
  39.     /**
  40.      * @ORM\Column(type="string", length=64)
  41.      */
  42.     private $password;
  43.     /**
  44.      * @ORM\Column(type="boolean")
  45.      */
  46.     private $isActive true;
  47.     /**
  48.      * @ORM\Column(type="boolean", options={"default" = 0})
  49.      */
  50.     private $hidden false;
  51.     /**
  52.      * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
  53.      *
  54.      */
  55.     private $roles;
  56.     /**
  57.      * @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\Customer")
  58.      * @ORM\JoinColumn(nullable=true)
  59.      */
  60.     protected $customer;
  61.     /**
  62.      * @ORM\ManyToMany(targetEntity="Diplix\KMGBundle\Entity\Customer")
  63.      *
  64.      */
  65.     private $additionalVisibleCustomers;
  66.     /**
  67.      * @ORM\Column(type="string", length=60)
  68.      */
  69.     private $firstName;
  70.     /**
  71.      * @ORM\Column(type="string", length=60)
  72.      */
  73.     private $lastName;
  74.     /**
  75.      * @ORM\Column(type="string", length=60)
  76.      */
  77.     private $phone "";
  78.     /**
  79.      * @ORM\Column(type="string", length=128, nullable=true)
  80.      */
  81.     private $mailActionHash "";
  82.     /**
  83.      * @ORM\Column(type="integer")
  84.      */
  85.     private $mailActionMode 0;
  86.     /**
  87.      * @ORM\Column(type="string", length=32, nullable=false)
  88.      */
  89.     protected $locale "de_DE"// sprache_LAND
  90.     /**
  91.      * @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\User")
  92.      */
  93.     protected $currentSubstituteUser;
  94.     /**
  95.      * @ORM\Column(type="datetime",nullable=true)
  96.      */
  97.     protected $lastPasswordChange;
  98.     /**
  99.      * A sys user should not be deleteable
  100.      * @ORM\Column(type="boolean", options={"default" = 0})
  101.      */
  102.     protected $sysUser false;
  103.     /**
  104.      * @ORM\Column(type="string", options={"default" = ""})
  105.      */
  106.     protected $mailCC "";
  107.     /**
  108.      * @ORM\Column(type="boolean", options={"default" = 1})
  109.      */
  110.     protected $autoFillOrdererDetailsInNewOrder true;
  111.     /**
  112.      * @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\Accounting\CoopMember")
  113.      * @ORM\JoinColumn(nullable=true)
  114.      */
  115.     protected $member;
  116.     /**
  117.      * @ORM\Column(type="json")
  118.      * @deprecated
  119.      */
  120.     protected $fcmTokens = [];
  121.     /**
  122.      * @ORM\OneToMany (targetEntity="Diplix\KMGBundle\Entity\DeviceToken", mappedBy="user", fetch="EAGER", cascade={"persist"})
  123.      * @ORM\JoinColumn()
  124.      */
  125.     protected $deviceTokens;
  126.     /**
  127.      * @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\File")
  128.      */
  129.     protected $profileImage;
  130.     /**
  131.      * @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\Address", cascade={"persist"})
  132.      * @ORM\JoinColumn(nullable=true)
  133.      */
  134.     protected $homeAddress;
  135.     /**
  136.      * @ORM\Column(type="string", options={"default" = ""})
  137.      */
  138.     protected $telegramId '';
  139.     /**
  140.      * A shared AES key wrapped with the users password-derived key - stored base64 encoded
  141.      * @ORM\Column(type="string", options={"default" = ""})
  142.      */
  143.     protected $sharedKey '';
  144.     public function __construct()
  145.     {
  146.         $this->roles = new ArrayCollection();
  147.         $this->additionalVisibleCustomers = new ArrayCollection();
  148.         $this->deviceTokens = new ArrayCollection();
  149.     }
  150.     public function __toString()
  151.     {
  152.         return sprintf("%d|%s",(is_null($this->id)?"0":$this->id),(is_null($this->username)?"???":$this->username));
  153.     }
  154.     public function getShortName()
  155.     {
  156.         $a $this->getLastName();
  157.         if ($this->firstName != '')
  158.         {
  159.             $a substr($this->firstName,0,1)."." $a;
  160.         }
  161.         return $a;
  162.     }
  163.     /**
  164.      * Mark this entity as soft-soft-deleted (hide and inactivate)
  165.      */
  166.     public function eraseMyself()
  167.     {
  168.         $this->setIsActive(false);
  169.         $this->setHidden(true);
  170.         $this->setFirstName("Deleted");
  171.         $this->setLastName("User");
  172.         $this->setPassword(uniqid("disabled"true));
  173.         $this->setMailActionMode(self::MAIL_ACTION_NONE);
  174.         $this->setUsername($this->getUsername()."disabled".uniqid(''true));
  175.         $this->setEmail$this->getEmail().".disabled".uniqid(''true).".invalid" );
  176.     }
  177.     /**
  178.      * @return array|string[]|Role[]
  179.      */
  180.     public function getRoles($asObject false)
  181.     {
  182.         $roleList $this->roles->toArray();
  183.         if ($asObject||self::$roles_as_object)
  184.         {
  185.             return $roleList;
  186.         }
  187.         return array_map( static function (Role $r) { return $r->getRole(); }, $roleList );
  188.     }
  189.     public function hasRole($roleName)
  190.     {
  191.         /** @var Role $one */
  192.         foreach ($this->roles as $one)
  193.         {
  194.             if ($one->getRole() === $roleName)
  195.             {
  196.                 return true;
  197.             }
  198.         }
  199.         return false;
  200.     }
  201.     /**
  202.      * @inheritDoc
  203.      */
  204.     public function getUsername()
  205.     {
  206.         return $this->username;
  207.     }
  208.     public function setUsername($un)
  209.     {
  210.         $this->username $un;
  211.     }
  212.     /**
  213.      * @inheritDoc
  214.      */
  215.     public function getSalt()
  216.     {
  217.         // we use bcrypt which creates a salt automatically when given null
  218.         // change this to a salt generation if the encoder gets changed
  219.         return null;
  220.     }
  221.     /**
  222.      * @inheritDoc
  223.      */
  224.     public function getPassword(): ?string
  225.     {
  226.         return $this->password;
  227.     }
  228.     /**
  229.      * @inheritDoc
  230.      */
  231.     public function eraseCredentials()
  232.     {
  233.         // apparently clearing the password here
  234.         // triggers doctrine to update the database record of the user
  235.         // with the new empty password
  236.     }
  237.     /**
  238.      * @see \Serializable::serialize()
  239.      */
  240.     public function serialize()
  241.     {
  242.         return serialize(array(
  243.                 $this->id,
  244.                 $this->email,
  245.                 $this->password,
  246.                 $this->username,
  247.         ));
  248.     }
  249.     /**
  250.      * @see \Serializable::unserialize()
  251.      */
  252.     public function unserialize($serialized)
  253.     {
  254.         list (
  255.                 $this->id,
  256.                 $this->email,
  257.                 $this->password,
  258.                 $this->username,
  259.         ) = unserialize($serialized);
  260.     }
  261.     /**
  262.      * @inheritDoc
  263.      */
  264.     public function isEqualTo(UserInterface $user)
  265.     {
  266.         return (
  267.                 ($user->getUsername() == $this->getUsername())
  268.                  &&
  269.                 ($user->getPassword() == $this->getPassword())
  270.                 );
  271.     }
  272. //    public function isAccountNonExpired()
  273. //    {
  274. //        return true;
  275. //    }
  276.     public function isAccountNonLocked()
  277.     {
  278.         return $this->isActive;
  279.     }
  280. //    public function isCredentialsNonExpired()
  281. //    {
  282. //        return true;
  283. //    }
  284.     public function isEnabled()
  285.     {
  286.         // a hidden account is basically pseudo-deleted
  287.         return !$this->hidden;
  288.     }
  289.     /**
  290.      * Get id
  291.      *
  292.      * @return integer
  293.      */
  294.     public function getId()
  295.     {
  296.         return $this->id;
  297.     }
  298.     /**
  299.      * Set email
  300.      *
  301.      * @param string $email
  302.      * @return User
  303.      */
  304.     public function setEmail($email)
  305.     {
  306.         $this->email $email;
  307.         return $this;
  308.     }
  309.     /**
  310.      * Get email
  311.      *
  312.      * @return string
  313.      */
  314.     public function getEmail()
  315.     {
  316.         return $this->email;
  317.     }
  318.     /**
  319.      * Set password
  320.      *
  321.      * @param string $password
  322.      * @return User
  323.      */
  324.     public function setPassword($password)
  325.     {
  326.         $this->password $password;
  327.         return $this;
  328.     }
  329.     /**
  330.      * Set isActive
  331.      *
  332.      * @param boolean $isActive
  333.      * @return User
  334.      */
  335.     public function setIsActive($isActive)
  336.     {
  337.         $this->isActive $isActive;
  338.         return $this;
  339.     }
  340.     /**
  341.      * Get isActive
  342.      *
  343.      * @return boolean
  344.      */
  345.     public function getIsActive()
  346.     {
  347.         return $this->isActive;
  348.     }
  349.     /**
  350.      * Add roles
  351.      *
  352.      * @param Role $roles
  353.      * @return User
  354.      */
  355.     public function addRole(Role $roles)
  356.     {
  357.         $this->roles[] = $roles;
  358.         return $this;
  359.     }
  360.     /**
  361.      * Remove roles
  362.      *
  363.      * @param Role $roles
  364.      */
  365.     public function removeRole(Role $roles)
  366.     {
  367.         $this->roles->removeElement($roles);
  368.     }
  369.     /**
  370.      * Set firstName
  371.      *
  372.      * @param string $firstName
  373.      * @return User
  374.      */
  375.     public function setFirstName($firstName)
  376.     {
  377.         $this->firstName $firstName;
  378.         return $this;
  379.     }
  380.     /**
  381.      * Get firstName
  382.      *
  383.      * @return string
  384.      */
  385.     public function getFirstName()
  386.     {
  387.         return $this->firstName;
  388.     }
  389.     /**
  390.      * Set lastName
  391.      *
  392.      * @param string $lastName
  393.      * @return User
  394.      */
  395.     public function setLastName($lastName)
  396.     {
  397.         $this->lastName $lastName;
  398.         return $this;
  399.     }
  400.     /**
  401.      * Get lastName
  402.      *
  403.      * @return string
  404.      */
  405.     public function getLastName()
  406.     {
  407.         return $this->lastName;
  408.     }
  409.     /**
  410.      * Set mailActionHash
  411.      *
  412.      * @param string $mailActionHash
  413.      * @return User
  414.      */
  415.     public function setMailActionHash($mailActionHash)
  416.     {
  417.         $this->mailActionHash $mailActionHash;
  418.         return $this;
  419.     }
  420.     /**
  421.      * Get mailActionHash
  422.      *
  423.      * @return string
  424.      */
  425.     public function getMailActionHash()
  426.     {
  427.         return $this->mailActionHash;
  428.     }
  429.     /**
  430.      * Set mailActionMode
  431.      *
  432.      * @param integer $mailActionMode
  433.      * @return User
  434.      */
  435.     public function setMailActionMode($mailActionMode)
  436.     {
  437.         $this->mailActionMode $mailActionMode;
  438.         return $this;
  439.     }
  440.     /**
  441.      * Get mailActionMode
  442.      *
  443.      * @return integer
  444.      */
  445.     public function getMailActionMode()
  446.     {
  447.         return $this->mailActionMode;
  448.     }
  449.     /**
  450.      * @return mixed
  451.      */
  452.     public function getHidden()
  453.     {
  454.         return $this->hidden;
  455.     }
  456.     /**
  457.      * @param mixed $hidden
  458.      */
  459.     public function setHidden($hidden)
  460.     {
  461.         $this->hidden $hidden;
  462.     }
  463.     /**
  464.      * @return string
  465.      */
  466.     public function getLocale()
  467.     {
  468.         return $this->locale;
  469.     }
  470.     /**
  471.      * @param string $locale
  472.      */
  473.     public function setLocale($locale)
  474.     {
  475.         $this->locale $locale;
  476.     }
  477.     /**
  478.      * @return Customer
  479.      */
  480.     public function getCustomer()
  481.     {
  482.         return $this->customer;
  483.     }
  484.     /**
  485.      * @param mixed $customer
  486.      */
  487.     public function setCustomer($customer)
  488.     {
  489.         $this->customer $customer;
  490.     }
  491.     /**
  492.      * @return mixed
  493.      */
  494.     public function getPhone()
  495.     {
  496.         return $this->phone;
  497.     }
  498.     /**
  499.      * @param mixed $phone
  500.      */
  501.     public function setPhone($phone)
  502.     {
  503.         $this->phone $phone;
  504.     }
  505.     /**
  506.      * @return mixed
  507.      */
  508.     public function getCurrentSubstituteUser()
  509.     {
  510.         return $this->currentSubstituteUser;
  511.     }
  512.     /**
  513.      * @param mixed $currentSubstituteUser
  514.      */
  515.     public function setCurrentSubstituteUser($currentSubstituteUser)
  516.     {
  517.         $this->currentSubstituteUser $currentSubstituteUser;
  518.     }
  519.     /**
  520.      * @return \DateTime
  521.      */
  522.     public function getLastPasswordChange()
  523.     {
  524.         return $this->lastPasswordChange;
  525.     }
  526.     /**
  527.      * @param mixed $lastPasswordChange
  528.      */
  529.     public function setLastPasswordChange($lastPasswordChange)
  530.     {
  531.         $this->lastPasswordChange $lastPasswordChange;
  532.     }
  533.     /**
  534.      * @return mixed
  535.      */
  536.     public function getSysUser()
  537.     {
  538.         return $this->sysUser;
  539.     }
  540.     /**
  541.      * @param mixed $sysUser
  542.      */
  543.     public function setSysUser($sysUser)
  544.     {
  545.         $this->sysUser $sysUser;
  546.     }
  547.     /**
  548.      * @return mixed
  549.      */
  550.     public function getMailCC()
  551.     {
  552.         return $this->mailCC;
  553.     }
  554.     /**
  555.      * @param mixed $mailCC
  556.      */
  557.     public function setMailCC($mailCC)
  558.     {
  559.         $this->mailCC $mailCC;
  560.     }
  561.     /**
  562.      * @return mixed
  563.      */
  564.     public function getAutoFillOrdererDetailsInNewOrder()
  565.     {
  566.         return $this->autoFillOrdererDetailsInNewOrder;
  567.     }
  568.     /**
  569.      * @param mixed $autoFillOrdererDetailsInNewOrder
  570.      */
  571.     public function setAutoFillOrdererDetailsInNewOrder($autoFillOrdererDetailsInNewOrder)
  572.     {
  573.         $this->autoFillOrdererDetailsInNewOrder $autoFillOrdererDetailsInNewOrder;
  574.     }
  575.     /**
  576.      * @return mixed
  577.      */
  578.     public function getAdditionalVisibleCustomers()
  579.     {
  580.         return $this->additionalVisibleCustomers;
  581.     }
  582.     /**
  583.      * @param mixed $additionalVisibleCustomers
  584.      */
  585.     public function setAdditionalVisibleCustomers($additionalVisibleCustomers)
  586.     {
  587.         $this->additionalVisibleCustomers $additionalVisibleCustomers;
  588.     }
  589.     /**
  590.      * @return null|CoopMember
  591.      */
  592.     public function getMember()
  593.     {
  594.         return $this->member;
  595.     }
  596.     /**
  597.      * @param mixed $member
  598.      */
  599.     public function setMember($member)
  600.     {
  601.         $this->member $member;
  602.     }
  603.     /**
  604.      * @return array
  605.      */
  606.     public function getFcmTokens()
  607.     {
  608.         $ret = [];
  609.         foreach ($this->getDeviceTokens() as $dt)
  610.         {
  611.             if ($dt->getType()===DeviceToken::FCM$ret[]= $dt->getToken();
  612.         }
  613.         return $ret;
  614.     }
  615.     /**
  616.      * @return Collection|DeviceToken[]
  617.      */
  618.     public function getDeviceTokens(): Collection
  619.     {
  620.         return $this->deviceTokens;
  621.     }
  622.     /**
  623.      * @param Collection|DeviceToken[] $deviceTokens
  624.      */
  625.     public function setDeviceTokens(ArrayCollection $deviceTokens): void
  626.     {
  627.         $this->deviceTokens $deviceTokens;
  628.     }
  629.     /**
  630.      * @return File|null
  631.      */
  632.     public function getProfileImage()
  633.     {
  634.         return $this->profileImage;
  635.     }
  636.     /**
  637.      * @param mixed $profileImage
  638.      */
  639.     public function setProfileImage($profileImage): void
  640.     {
  641.         $this->profileImage $profileImage;
  642.     }
  643.     /**
  644.      * @return Address|null
  645.      */
  646.     public function getHomeAddress()
  647.     {
  648.         return $this->homeAddress;
  649.     }
  650.     /**
  651.      * @param Address|null $homeAddress
  652.      */
  653.     public function setHomeAddress($homeAddress): void
  654.     {
  655.         $this->homeAddress $homeAddress;
  656.     }
  657.     /**
  658.      * @return string
  659.      */
  660.     public function getTelegramId(): string
  661.     {
  662.         return $this->telegramId;
  663.     }
  664.     /**
  665.      * @param string $telegramId
  666.      */
  667.     public function setTelegramId(string $telegramId): void
  668.     {
  669.         $this->telegramId $telegramId;
  670.     }
  671.     /**
  672.      * @param bool $binary set to true to get the key in binary form instead of base64
  673.      * @return string the base64-encoded shared key
  674.      */
  675.     public function getSharedKey($binary=false): string
  676.     {
  677.         if ($binary)
  678.         {
  679.             return base64_decode($this->sharedKey);
  680.         }
  681.         return $this->sharedKey;
  682.     }
  683.     /**
  684.      * @param string $sharedKey
  685.      * @param bool $isBinaryKey
  686.      */
  687.     public function setSharedKey(string $sharedKey$isBinaryKey false): void
  688.     {
  689.         if ($isBinaryKey)
  690.         {
  691.             $sharedKey base64_encode($sharedKey);
  692.         }
  693.         $this->sharedKey $sharedKey;
  694.     }
  695.     /////////////////////////////////////////////////////////////////////////////
  696.     /**
  697.      * Specify data which should be serialized to JSON
  698.      * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
  699.      * @return mixed data which can be serialized by <b>json_encode</b>,
  700.      * which is a value of any type other than a resource.
  701.      * @since 5.4.0
  702.      */
  703.     #[\ReturnTypeWillChange]
  704.     function jsonSerialize()
  705.     {
  706.         return [
  707.             'id'=>$this->getId(),
  708.             'firstName'=>$this->getFirstName(),
  709.             'lastName' =>$this->getLastName(),
  710.             'shortCode'=> $this->getMember() !== null $this->getMember()->getShortCode() : 'null'
  711.         ];
  712.     }
  713. }