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.     /**
  145.      * @ORM\Column(type="boolean", options={"default" = 0})
  146.      */
  147.     protected bool $enableTimeTracking false;
  148.     /**
  149.      * @ORM\Column(type="string", options={"default" = ""})
  150.      */
  151.     protected $shortCode '';
  152.     /**
  153.      * @ORM\Column(type="json")
  154.      */
  155.     protected ?array $dashboards = [];
  156.     public function __construct()
  157.     {
  158.         $this->roles = new ArrayCollection();
  159.         $this->additionalVisibleCustomers = new ArrayCollection();
  160.         $this->deviceTokens = new ArrayCollection();
  161.     }
  162.     public function __toString()
  163.     {
  164.         return sprintf("%d|%s",(is_null($this->id)?"0":$this->id),(is_null($this->username)?"???":$this->username));
  165.     }
  166.     public function getShortName()
  167.     {
  168.         $a $this->getLastName();
  169.         if ($this->firstName != '')
  170.         {
  171.             $a substr($this->firstName,0,1)."." $a;
  172.         }
  173.         return $a;
  174.     }
  175.     /**
  176.      * Mark this entity as soft-soft-deleted (hide and inactivate)
  177.      */
  178.     public function eraseMyself()
  179.     {
  180.         $this->setIsActive(false);
  181.         $this->setHidden(true);
  182.         $this->setFirstName("Deleted");
  183.         $this->setLastName("User");
  184.         $this->setPassword(uniqid("disabled"true));
  185.         $this->setMailActionMode(self::MAIL_ACTION_NONE);
  186.         $this->setUsername($this->getUsername()."disabled".uniqid(''true));
  187.         $this->setEmail$this->getEmail().".disabled".uniqid(''true).".invalid" );
  188.     }
  189.     /**
  190.      * @return array|string[]|Role[]
  191.      */
  192.     public function getRoles($asObject false)
  193.     {
  194.         $roleList $this->roles->toArray();
  195.         if ($asObject||self::$roles_as_object)
  196.         {
  197.             return $roleList;
  198.         }
  199.         return array_map( static function (Role $r) { return $r->getRole(); }, $roleList );
  200.     }
  201.     public function hasRole($roleName)
  202.     {
  203.         /** @var Role $one */
  204.         foreach ($this->roles as $one)
  205.         {
  206.             if ($one->getRole() === $roleName)
  207.             {
  208.                 return true;
  209.             }
  210.         }
  211.         return false;
  212.     }
  213.     /**
  214.      * @inheritDoc
  215.      */
  216.     public function getUsername()
  217.     {
  218.         return $this->username;
  219.     }
  220.     public function setUsername($un)
  221.     {
  222.         $this->username $un;
  223.     }
  224.     /**
  225.      * @inheritDoc
  226.      */
  227.     public function getSalt()
  228.     {
  229.         // we use bcrypt which creates a salt automatically when given null
  230.         // change this to a salt generation if the encoder gets changed
  231.         return null;
  232.     }
  233.     /**
  234.      * @inheritDoc
  235.      */
  236.     public function getPassword(): ?string
  237.     {
  238.         return $this->password;
  239.     }
  240.     /**
  241.      * @inheritDoc
  242.      */
  243.     public function eraseCredentials()
  244.     {
  245.         // apparently clearing the password here
  246.         // triggers doctrine to update the database record of the user
  247.         // with the new empty password
  248.     }
  249.     /**
  250.      * @see \Serializable::serialize()
  251.      */
  252.     public function serialize()
  253.     {
  254.         return serialize(array(
  255.                 $this->id,
  256.                 $this->email,
  257.                 $this->password,
  258.                 $this->username,
  259.         ));
  260.     }
  261.     /**
  262.      * @see \Serializable::unserialize()
  263.      */
  264.     public function unserialize($serialized)
  265.     {
  266.         list (
  267.                 $this->id,
  268.                 $this->email,
  269.                 $this->password,
  270.                 $this->username,
  271.         ) = unserialize($serialized);
  272.     }
  273.     /**
  274.      * @inheritDoc
  275.      */
  276.     public function isEqualTo(UserInterface $user)
  277.     {
  278.         return (
  279.                 ($user->getUsername() == $this->getUsername())
  280.                  &&
  281.                 ($user->getPassword() == $this->getPassword())
  282.                 );
  283.     }
  284. //    public function isAccountNonExpired()
  285. //    {
  286. //        return true;
  287. //    }
  288.     public function isAccountNonLocked()
  289.     {
  290.         return $this->isActive;
  291.     }
  292. //    public function isCredentialsNonExpired()
  293. //    {
  294. //        return true;
  295. //    }
  296.     public function isEnabled()
  297.     {
  298.         // a hidden account is basically pseudo-deleted
  299.         return !$this->hidden;
  300.     }
  301.     /**
  302.      * Get id
  303.      *
  304.      * @return integer
  305.      */
  306.     public function getId()
  307.     {
  308.         return $this->id;
  309.     }
  310.     /**
  311.      * Set email
  312.      *
  313.      * @param string $email
  314.      * @return User
  315.      */
  316.     public function setEmail($email)
  317.     {
  318.         $this->email $email;
  319.         return $this;
  320.     }
  321.     /**
  322.      * Get email
  323.      *
  324.      * @return string
  325.      */
  326.     public function getEmail()
  327.     {
  328.         return $this->email;
  329.     }
  330.     /**
  331.      * Set password
  332.      *
  333.      * @param string $password
  334.      * @return User
  335.      */
  336.     public function setPassword($password)
  337.     {
  338.         $this->password $password;
  339.         return $this;
  340.     }
  341.     /**
  342.      * Set isActive
  343.      *
  344.      * @param boolean $isActive
  345.      * @return User
  346.      */
  347.     public function setIsActive($isActive)
  348.     {
  349.         $this->isActive $isActive;
  350.         return $this;
  351.     }
  352.     /**
  353.      * Get isActive
  354.      *
  355.      * @return boolean
  356.      */
  357.     public function getIsActive()
  358.     {
  359.         return $this->isActive;
  360.     }
  361.     /**
  362.      * Add roles
  363.      *
  364.      * @param Role $roles
  365.      * @return User
  366.      */
  367.     public function addRole(Role $roles)
  368.     {
  369.         $this->roles[] = $roles;
  370.         return $this;
  371.     }
  372.     /**
  373.      * Remove roles
  374.      *
  375.      * @param Role $roles
  376.      */
  377.     public function removeRole(Role $roles)
  378.     {
  379.         $this->roles->removeElement($roles);
  380.     }
  381.     /**
  382.      * Set firstName
  383.      *
  384.      * @param string $firstName
  385.      * @return User
  386.      */
  387.     public function setFirstName($firstName)
  388.     {
  389.         $this->firstName $firstName;
  390.         return $this;
  391.     }
  392.     /**
  393.      * Get firstName
  394.      *
  395.      * @return string
  396.      */
  397.     public function getFirstName()
  398.     {
  399.         return $this->firstName;
  400.     }
  401.     /**
  402.      * Set lastName
  403.      *
  404.      * @param string $lastName
  405.      * @return User
  406.      */
  407.     public function setLastName($lastName)
  408.     {
  409.         $this->lastName $lastName;
  410.         return $this;
  411.     }
  412.     /**
  413.      * Get lastName
  414.      *
  415.      * @return string
  416.      */
  417.     public function getLastName()
  418.     {
  419.         return $this->lastName;
  420.     }
  421.     /**
  422.      * Set mailActionHash
  423.      *
  424.      * @param string $mailActionHash
  425.      * @return User
  426.      */
  427.     public function setMailActionHash($mailActionHash)
  428.     {
  429.         $this->mailActionHash $mailActionHash;
  430.         return $this;
  431.     }
  432.     /**
  433.      * Get mailActionHash
  434.      *
  435.      * @return string
  436.      */
  437.     public function getMailActionHash()
  438.     {
  439.         return $this->mailActionHash;
  440.     }
  441.     /**
  442.      * Set mailActionMode
  443.      *
  444.      * @param integer $mailActionMode
  445.      * @return User
  446.      */
  447.     public function setMailActionMode($mailActionMode)
  448.     {
  449.         $this->mailActionMode $mailActionMode;
  450.         return $this;
  451.     }
  452.     /**
  453.      * Get mailActionMode
  454.      *
  455.      * @return integer
  456.      */
  457.     public function getMailActionMode()
  458.     {
  459.         return $this->mailActionMode;
  460.     }
  461.     /**
  462.      * @return mixed
  463.      */
  464.     public function getHidden()
  465.     {
  466.         return $this->hidden;
  467.     }
  468.     /**
  469.      * @param mixed $hidden
  470.      */
  471.     public function setHidden($hidden)
  472.     {
  473.         $this->hidden $hidden;
  474.     }
  475.     /**
  476.      * @return string
  477.      */
  478.     public function getLocale()
  479.     {
  480.         return $this->locale;
  481.     }
  482.     /**
  483.      * @param string $locale
  484.      */
  485.     public function setLocale($locale)
  486.     {
  487.         $this->locale $locale;
  488.     }
  489.     /**
  490.      * @return Customer
  491.      */
  492.     public function getCustomer()
  493.     {
  494.         return $this->customer;
  495.     }
  496.     /**
  497.      * @param mixed $customer
  498.      */
  499.     public function setCustomer($customer)
  500.     {
  501.         $this->customer $customer;
  502.     }
  503.     /**
  504.      * @return mixed
  505.      */
  506.     public function getPhone()
  507.     {
  508.         return $this->phone;
  509.     }
  510.     /**
  511.      * @param mixed $phone
  512.      */
  513.     public function setPhone($phone)
  514.     {
  515.         $this->phone $phone;
  516.     }
  517.     /**
  518.      * @return mixed
  519.      */
  520.     public function getCurrentSubstituteUser()
  521.     {
  522.         return $this->currentSubstituteUser;
  523.     }
  524.     /**
  525.      * @param mixed $currentSubstituteUser
  526.      */
  527.     public function setCurrentSubstituteUser($currentSubstituteUser)
  528.     {
  529.         $this->currentSubstituteUser $currentSubstituteUser;
  530.     }
  531.     /**
  532.      * @return \DateTime
  533.      */
  534.     public function getLastPasswordChange()
  535.     {
  536.         return $this->lastPasswordChange;
  537.     }
  538.     /**
  539.      * @param mixed $lastPasswordChange
  540.      */
  541.     public function setLastPasswordChange($lastPasswordChange)
  542.     {
  543.         $this->lastPasswordChange $lastPasswordChange;
  544.     }
  545.     /**
  546.      * @return mixed
  547.      */
  548.     public function getSysUser()
  549.     {
  550.         return $this->sysUser;
  551.     }
  552.     /**
  553.      * @param mixed $sysUser
  554.      */
  555.     public function setSysUser($sysUser)
  556.     {
  557.         $this->sysUser $sysUser;
  558.     }
  559.     /**
  560.      * @return mixed
  561.      */
  562.     public function getMailCC()
  563.     {
  564.         return $this->mailCC;
  565.     }
  566.     /**
  567.      * @param mixed $mailCC
  568.      */
  569.     public function setMailCC($mailCC)
  570.     {
  571.         $this->mailCC $mailCC;
  572.     }
  573.     /**
  574.      * @return mixed
  575.      */
  576.     public function getAutoFillOrdererDetailsInNewOrder()
  577.     {
  578.         return $this->autoFillOrdererDetailsInNewOrder;
  579.     }
  580.     /**
  581.      * @param mixed $autoFillOrdererDetailsInNewOrder
  582.      */
  583.     public function setAutoFillOrdererDetailsInNewOrder($autoFillOrdererDetailsInNewOrder)
  584.     {
  585.         $this->autoFillOrdererDetailsInNewOrder $autoFillOrdererDetailsInNewOrder;
  586.     }
  587.     /**
  588.      * @return mixed
  589.      */
  590.     public function getAdditionalVisibleCustomers()
  591.     {
  592.         return $this->additionalVisibleCustomers;
  593.     }
  594.     /**
  595.      * @param mixed $additionalVisibleCustomers
  596.      */
  597.     public function setAdditionalVisibleCustomers($additionalVisibleCustomers)
  598.     {
  599.         $this->additionalVisibleCustomers $additionalVisibleCustomers;
  600.     }
  601.     /**
  602.      * @return null|CoopMember
  603.      */
  604.     public function getMember()
  605.     {
  606.         return $this->member;
  607.     }
  608.     /**
  609.      * @param mixed $member
  610.      */
  611.     public function setMember($member)
  612.     {
  613.         $this->member $member;
  614.     }
  615.     /**
  616.      * @return array
  617.      */
  618.     public function getFcmTokens()
  619.     {
  620.         $ret = [];
  621.         foreach ($this->getDeviceTokens() as $dt)
  622.         {
  623.             if ($dt->getType()===DeviceToken::FCM$ret[]= $dt->getToken();
  624.         }
  625.         return $ret;
  626.     }
  627.     /**
  628.      * @return Collection|DeviceToken[]
  629.      */
  630.     public function getDeviceTokens(): Collection
  631.     {
  632.         return $this->deviceTokens;
  633.     }
  634.     /**
  635.      * @param Collection|DeviceToken[] $deviceTokens
  636.      */
  637.     public function setDeviceTokens(ArrayCollection $deviceTokens): void
  638.     {
  639.         $this->deviceTokens $deviceTokens;
  640.     }
  641.     /**
  642.      * @return File|null
  643.      */
  644.     public function getProfileImage()
  645.     {
  646.         return $this->profileImage;
  647.     }
  648.     /**
  649.      * @param mixed $profileImage
  650.      */
  651.     public function setProfileImage($profileImage): void
  652.     {
  653.         $this->profileImage $profileImage;
  654.     }
  655.     /**
  656.      * @return Address|null
  657.      */
  658.     public function getHomeAddress()
  659.     {
  660.         return $this->homeAddress;
  661.     }
  662.     /**
  663.      * @param Address|null $homeAddress
  664.      */
  665.     public function setHomeAddress($homeAddress): void
  666.     {
  667.         $this->homeAddress $homeAddress;
  668.     }
  669.     /**
  670.      * @return string
  671.      */
  672.     public function getTelegramId(): string
  673.     {
  674.         return $this->telegramId;
  675.     }
  676.     /**
  677.      * @param string $telegramId
  678.      */
  679.     public function setTelegramId(string $telegramId): void
  680.     {
  681.         $this->telegramId $telegramId;
  682.     }
  683.     /**
  684.      * @param bool $binary set to true to get the key in binary form instead of base64
  685.      * @return string the base64-encoded shared key
  686.      */
  687.     public function getSharedKey($binary=false): string
  688.     {
  689.         if ($binary)
  690.         {
  691.             return base64_decode($this->sharedKey);
  692.         }
  693.         return $this->sharedKey;
  694.     }
  695.     /**
  696.      * @param string $sharedKey
  697.      * @param bool $isBinaryKey
  698.      */
  699.     public function setSharedKey(string $sharedKey$isBinaryKey false): void
  700.     {
  701.         if ($isBinaryKey)
  702.         {
  703.             $sharedKey base64_encode($sharedKey);
  704.         }
  705.         $this->sharedKey $sharedKey;
  706.     }
  707.     public function isEnableTimeTracking(): bool
  708.     {
  709.         return $this->enableTimeTracking;
  710.     }
  711.     public function setEnableTimeTracking(bool $enableTimeTracking): void
  712.     {
  713.         $this->enableTimeTracking $enableTimeTracking;
  714.     }
  715.     public function getShortCode(): string
  716.     {
  717.         if (empty($this->shortCode))
  718.         {
  719.             return strtoupper(substr($this->getFirstName(),0,1).substr($this->getLastName(),0,1));
  720.         }
  721.         return $this->shortCode;
  722.     }
  723.     public function setShortCode(string $shortCode): void
  724.     {
  725.         $this->shortCode $shortCode;
  726.     }
  727.     public function getDashboards(): array
  728.     {
  729.         return $this->dashboards ?? [];
  730.     }
  731.     public function setDashboards(array $dashboards): void
  732.     {
  733.         $this->dashboards $dashboards;
  734.     }
  735.     /////////////////////////////////////////////////////////////////////////////
  736.     /**
  737.      * Specify data which should be serialized to JSON
  738.      * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
  739.      * @return mixed data which can be serialized by <b>json_encode</b>,
  740.      * which is a value of any type other than a resource.
  741.      * @since 5.4.0
  742.      */
  743.     #[\ReturnTypeWillChange]
  744.     function jsonSerialize()
  745.     {
  746.         return [
  747.             'id'=>$this->getId(),
  748.             'firstName'=>$this->getFirstName(),
  749.             'lastName' =>$this->getLastName(),
  750.             'shortCode'=> $this->getMember() !== null $this->getMember()->getShortCode() : 'null'
  751.         ];
  752.     }
  753. }