<?php
namespace Diplix\KMGBundle\Entity;
use Diplix\KMGBundle\Entity\Accounting\CoopMember;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="users", indexes={@ORM\Index(name="be_deleted",columns={"be_deleted"})})
* @ORM\Entity(repositoryClass="Diplix\KMGBundle\Repository\UserRepository")
*/
class User extends BasicEntity implements UserInterface,
PasswordAuthenticatedUserInterface,
EquatableInterface,
\Serializable,
\JsonSerializable
{
const MAIL_ACTION_NONE = 0;
const MAIL_ACTION_REGISTER = 1;
const MAIL_ACTION_RESET_PASSWORD = 2;
public static $roles_as_object = false;
/**
* @ORM\Column(type="integer",name="id")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=128)
*/
private $username;
/**
* @ORM\Column(type="string", length=128)
*/
private $email;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="boolean")
*/
private $isActive = true;
/**
* @ORM\Column(type="boolean", options={"default" = 0})
*/
private $hidden = false;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
/**
* @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\Customer")
* @ORM\JoinColumn(nullable=true)
*/
protected $customer;
/**
* @ORM\ManyToMany(targetEntity="Diplix\KMGBundle\Entity\Customer")
*
*/
private $additionalVisibleCustomers;
/**
* @ORM\Column(type="string", length=60)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=60)
*/
private $lastName;
/**
* @ORM\Column(type="string", length=60)
*/
private $phone = "";
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private $mailActionHash = "";
/**
* @ORM\Column(type="integer")
*/
private $mailActionMode = 0;
/**
* @ORM\Column(type="string", length=32, nullable=false)
*/
protected $locale = "de_DE"; // sprache_LAND
/**
* @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\User")
*/
protected $currentSubstituteUser;
/**
* @ORM\Column(type="datetime",nullable=true)
*/
protected $lastPasswordChange;
/**
* A sys user should not be deleteable
* @ORM\Column(type="boolean", options={"default" = 0})
*/
protected $sysUser = false;
/**
* @ORM\Column(type="string", options={"default" = ""})
*/
protected $mailCC = "";
/**
* @ORM\Column(type="boolean", options={"default" = 1})
*/
protected $autoFillOrdererDetailsInNewOrder = true;
/**
* @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\Accounting\CoopMember")
* @ORM\JoinColumn(nullable=true)
*/
protected $member;
/**
* @ORM\Column(type="json")
* @deprecated
*/
protected $fcmTokens = [];
/**
* @ORM\OneToMany (targetEntity="Diplix\KMGBundle\Entity\DeviceToken", mappedBy="user", fetch="EAGER", cascade={"persist"})
* @ORM\JoinColumn()
*/
protected $deviceTokens;
/**
* @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\File")
*/
protected $profileImage;
/**
* @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\Address", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
protected $homeAddress;
/**
* @ORM\Column(type="string", options={"default" = ""})
*/
protected $telegramId = '';
/**
* A shared AES key wrapped with the users password-derived key - stored base64 encoded
* @ORM\Column(type="string", options={"default" = ""})
*/
protected $sharedKey = '';
public function __construct()
{
$this->roles = new ArrayCollection();
$this->additionalVisibleCustomers = new ArrayCollection();
$this->deviceTokens = new ArrayCollection();
}
public function __toString()
{
return sprintf("%d|%s",(is_null($this->id)?"0":$this->id),(is_null($this->username)?"???":$this->username));
}
public function getShortName()
{
$a = $this->getLastName();
if ($this->firstName != '')
{
$a = substr($this->firstName,0,1)."." . $a;
}
return $a;
}
/**
* Mark this entity as soft-soft-deleted (hide and inactivate)
*/
public function eraseMyself()
{
$this->setIsActive(false);
$this->setHidden(true);
$this->setFirstName("Deleted");
$this->setLastName("User");
$this->setPassword(uniqid("disabled", true));
$this->setMailActionMode(self::MAIL_ACTION_NONE);
$this->setUsername($this->getUsername()."disabled".uniqid('', true));
$this->setEmail( $this->getEmail().".disabled".uniqid('', true).".invalid" );
}
/**
* @return array|string[]|Role[]
*/
public function getRoles($asObject = false)
{
$roleList = $this->roles->toArray();
if ($asObject||self::$roles_as_object)
{
return $roleList;
}
return array_map( static function (Role $r) { return $r->getRole(); }, $roleList );
}
public function hasRole($roleName)
{
/** @var Role $one */
foreach ($this->roles as $one)
{
if ($one->getRole() === $roleName)
{
return true;
}
}
return false;
}
/**
* @inheritDoc
*/
public function getUsername()
{
return $this->username;
}
public function setUsername($un)
{
$this->username = $un;
}
/**
* @inheritDoc
*/
public function getSalt()
{
// we use bcrypt which creates a salt automatically when given null
// change this to a salt generation if the encoder gets changed
return null;
}
/**
* @inheritDoc
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
// apparently clearing the password here
// triggers doctrine to update the database record of the user
// with the new empty password
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->email,
$this->password,
$this->username,
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->email,
$this->password,
$this->username,
) = unserialize($serialized);
}
/**
* @inheritDoc
*/
public function isEqualTo(UserInterface $user)
{
return (
($user->getUsername() == $this->getUsername())
&&
($user->getPassword() == $this->getPassword())
);
}
// public function isAccountNonExpired()
// {
// return true;
// }
public function isAccountNonLocked()
{
return $this->isActive;
}
// public function isCredentialsNonExpired()
// {
// return true;
// }
public function isEnabled()
{
// a hidden account is basically pseudo-deleted
return !$this->hidden;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Set isActive
*
* @param boolean $isActive
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Add roles
*
* @param Role $roles
* @return User
*/
public function addRole(Role $roles)
{
$this->roles[] = $roles;
return $this;
}
/**
* Remove roles
*
* @param Role $roles
*/
public function removeRole(Role $roles)
{
$this->roles->removeElement($roles);
}
/**
* Set firstName
*
* @param string $firstName
* @return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string $lastName
* @return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set mailActionHash
*
* @param string $mailActionHash
* @return User
*/
public function setMailActionHash($mailActionHash)
{
$this->mailActionHash = $mailActionHash;
return $this;
}
/**
* Get mailActionHash
*
* @return string
*/
public function getMailActionHash()
{
return $this->mailActionHash;
}
/**
* Set mailActionMode
*
* @param integer $mailActionMode
* @return User
*/
public function setMailActionMode($mailActionMode)
{
$this->mailActionMode = $mailActionMode;
return $this;
}
/**
* Get mailActionMode
*
* @return integer
*/
public function getMailActionMode()
{
return $this->mailActionMode;
}
/**
* @return mixed
*/
public function getHidden()
{
return $this->hidden;
}
/**
* @param mixed $hidden
*/
public function setHidden($hidden)
{
$this->hidden = $hidden;
}
/**
* @return string
*/
public function getLocale()
{
return $this->locale;
}
/**
* @param string $locale
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
/**
* @return Customer
*/
public function getCustomer()
{
return $this->customer;
}
/**
* @param mixed $customer
*/
public function setCustomer($customer)
{
$this->customer = $customer;
}
/**
* @return mixed
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param mixed $phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
}
/**
* @return mixed
*/
public function getCurrentSubstituteUser()
{
return $this->currentSubstituteUser;
}
/**
* @param mixed $currentSubstituteUser
*/
public function setCurrentSubstituteUser($currentSubstituteUser)
{
$this->currentSubstituteUser = $currentSubstituteUser;
}
/**
* @return \DateTime
*/
public function getLastPasswordChange()
{
return $this->lastPasswordChange;
}
/**
* @param mixed $lastPasswordChange
*/
public function setLastPasswordChange($lastPasswordChange)
{
$this->lastPasswordChange = $lastPasswordChange;
}
/**
* @return mixed
*/
public function getSysUser()
{
return $this->sysUser;
}
/**
* @param mixed $sysUser
*/
public function setSysUser($sysUser)
{
$this->sysUser = $sysUser;
}
/**
* @return mixed
*/
public function getMailCC()
{
return $this->mailCC;
}
/**
* @param mixed $mailCC
*/
public function setMailCC($mailCC)
{
$this->mailCC = $mailCC;
}
/**
* @return mixed
*/
public function getAutoFillOrdererDetailsInNewOrder()
{
return $this->autoFillOrdererDetailsInNewOrder;
}
/**
* @param mixed $autoFillOrdererDetailsInNewOrder
*/
public function setAutoFillOrdererDetailsInNewOrder($autoFillOrdererDetailsInNewOrder)
{
$this->autoFillOrdererDetailsInNewOrder = $autoFillOrdererDetailsInNewOrder;
}
/**
* @return mixed
*/
public function getAdditionalVisibleCustomers()
{
return $this->additionalVisibleCustomers;
}
/**
* @param mixed $additionalVisibleCustomers
*/
public function setAdditionalVisibleCustomers($additionalVisibleCustomers)
{
$this->additionalVisibleCustomers = $additionalVisibleCustomers;
}
/**
* @return null|CoopMember
*/
public function getMember()
{
return $this->member;
}
/**
* @param mixed $member
*/
public function setMember($member)
{
$this->member = $member;
}
/**
* @return array
*/
public function getFcmTokens()
{
$ret = [];
foreach ($this->getDeviceTokens() as $dt)
{
if ($dt->getType()===DeviceToken::FCM) $ret[]= $dt->getToken();
}
return $ret;
}
/**
* @return Collection|DeviceToken[]
*/
public function getDeviceTokens(): Collection
{
return $this->deviceTokens;
}
/**
* @param Collection|DeviceToken[] $deviceTokens
*/
public function setDeviceTokens(ArrayCollection $deviceTokens): void
{
$this->deviceTokens = $deviceTokens;
}
/**
* @return File|null
*/
public function getProfileImage()
{
return $this->profileImage;
}
/**
* @param mixed $profileImage
*/
public function setProfileImage($profileImage): void
{
$this->profileImage = $profileImage;
}
/**
* @return Address|null
*/
public function getHomeAddress()
{
return $this->homeAddress;
}
/**
* @param Address|null $homeAddress
*/
public function setHomeAddress($homeAddress): void
{
$this->homeAddress = $homeAddress;
}
/**
* @return string
*/
public function getTelegramId(): string
{
return $this->telegramId;
}
/**
* @param string $telegramId
*/
public function setTelegramId(string $telegramId): void
{
$this->telegramId = $telegramId;
}
/**
* @param bool $binary set to true to get the key in binary form instead of base64
* @return string the base64-encoded shared key
*/
public function getSharedKey($binary=false): string
{
if ($binary)
{
return base64_decode($this->sharedKey);
}
return $this->sharedKey;
}
/**
* @param string $sharedKey
* @param bool $isBinaryKey
*/
public function setSharedKey(string $sharedKey, $isBinaryKey = false): void
{
if ($isBinaryKey)
{
$sharedKey = base64_encode($sharedKey);
}
$this->sharedKey = $sharedKey;
}
/////////////////////////////////////////////////////////////////////////////
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
#[\ReturnTypeWillChange]
function jsonSerialize()
{
return [
'id'=>$this->getId(),
'firstName'=>$this->getFirstName(),
'lastName' =>$this->getLastName(),
'shortCode'=> $this->getMember() !== null ? $this->getMember()->getShortCode() : 'null'
];
}
}