<?php
namespace App\Daikin\BaseBundle\Entity;
use App\Daikin\BaseBundle\Entity\EmergencyStock\Order;
use App\Daikin\BaseBundle\Entity\EmergencyStock\Warehouse;
use \DateTime;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use App\Daikin\BaseBundle\Validator\Constraints\UndeletedUniqueEntity as UniqueEntity;
use EHC\HTMLIdableBundle\Interfaces\HTMLIdableInterface;
use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass="App\Daikin\BaseBundle\Repository\UserRepository")
* @ORM\Table(name="user")
* @UniqueEntity("email")
*/
class User extends AbstractDeleteableEntity implements UserInterface, LegacyPasswordAuthenticatedUserInterface, HTMLIdableInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var integer $id
*/
protected $id;
/**
* @ORM\Column(type="integer", name="title")
*
* @var integer $title
*/
protected $title;
/**
* @ORM\Column(name="firstname")
*
* @var string $firstname
*/
protected $firstname;
/**
* @ORM\Column(name="lastname")
*
* @var string $lastname
*/
protected $lastname;
/**
* @ORM\Column(name="email")
* @var string $email
*/
protected $email;
/**
* @ORM\Column(name="password")
*
* @var string $password
*/
protected $password;
private $ppassword;
/**
* @ORM\Column(length=32)
*
* @var string $salt
*/
protected $salt;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="integer", name="created_at")
*
* @var integer $createdAt
*/
protected $createdAt;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="integer", name="updated_at")
*
* @var integer $updatedAt
*/
protected $updatedAt;
/**
* @ORM\Column(type="integer", name="approved_email")
*
* @var integer $approved_email
*/
protected $approved_email;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
* @var Collection|Role[]
*/
protected $userRoles;
/**
* @ORM\ManyToOne(targetEntity="Company", inversedBy="users")
* @ORM\JoinColumn(name="companyid", nullable=false, onDelete="CASCADE")
*/
protected $company;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $reset_requested_at;
/**
* @ORM\Column(length=32, nullable=true)
*/
protected $reset_request_hash;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $app_last_login;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $portal_last_login;
/**
* @var Order[]
*
* @ORM\OneToMany(targetEntity="App\Daikin\BaseBundle\Entity\EmergencyStock\Order", mappedBy="customer")
*/
private $orders;
/**
* @var Warehouse
*
* @ORM\ManyToOne(targetEntity="App\Daikin\BaseBundle\Entity\EmergencyStock\Warehouse")
* @ORM\JoinColumn()
*/
private $warehouse;
/**
* @var DateTime
*
* @ORM\Column(type="datetime", nullable=true)
*/
protected $converted_from_rotex;
/**
* @var int
*
* @ORM\Column(type="integer", nullable=true)
*/
protected $converted_from_rotex_user_id;
/**
* Constructs a new instance of User
*/
public function __construct()
{
parent::__construct();
$this->orders = new ArrayCollection();
$this->userRoles = new ArrayCollection();
$this->createdAt = time();
$this->updatedAt = time();
$this->approved_email = 0;
$this->title = 1;
$this->salt = md5( time() . uniqid() . rand() );
}
public function __toString()
{
return $this->firstname.' '.$this->lastname.' <'.$this->email.'>';
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
));
}
/**
* @see \Serializable::unserialize()
* @param string $serialized
*/
public function unserialize($serialized)
{
list (
$this->id,
) = unserialize($serialized);
}
/**
* Gets the id.
*
* @return integer The id
*/
public function getId()
{
return $this->id;
}
public function getHTMLId()
{
return 'user-'.$this->id;
}
/**
* Gets the id.
*
* @return integer The id
*/
public function getApprovedEmail()
{
return $this->approved_email;
}
/**
* has the email address been verified?
*
* @return boolean
*/
public function isEmailApproved()
{
return (bool)$this->approved_email;
}
/**
* Sets.
*
* @param integer $value
* @return User
*/
public function setApprovedEmail( $value ): self
{
$this->approved_email = $value;
return $this;
}
/**
* Gets the user's title.
*
* @return string The title
*/
public function getTitle()
{
return $this->title;
}
/**
* Sets the user's title.
*
* @param string $value The title
* @return User
*/
public function setTitle( $value )
{
$this->title = $value;
return $this;
}
/**
* Gets the user's first name.
*
* @return string The first name
*/
public function getFirstName()
{
return $this->firstname;
}
/**
* Sets the user's first name.
*
* @param string $value The first name
* @return User
*/
public function setFirstName( $value )
{
$this->firstname = ucfirst( $value );
return $this;
}
/**
* Gets the user's last name.
*
* @return string The last name
*/
public function getLastName()
{
return $this->lastname;
}
/**
* Sets the user's last name.
*
* @param string $value The last name
* @return User
*/
public function setLastName( $value )
{
$this->lastname = ucfirst( $value );
return $this;
}
/**
* Gets the user's email address.
*
* @return string The email
*/
public function getEmail()
{
return $this->email;
}
/**
* Sets the user's email address.
*
* @param string $value The email
* @return User
*/
public function setEmail($value)
{
$this->email = $value;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* Sets the user password.
*
* @param string $value The password.
* @return User
*/
public function setPassword($value)
{
$this->password = $value;
return $this;
}
/**
* set plain password
*
* @param string $ppass
* @return User
*/
public function setPPassword($ppass = null)
{
$this->ppassword = (null === $ppass) ? null : (string)$ppass;
return $this;
}
/**
* get plain password
* @return string
*/
public function getPPassword()
{
return $this->ppassword;
}
/**
* update the encrypted password from plain password if it's set
*
* @return User
*/
public function updateEncryptedPassword()
{
throw new \Exception('Fix me');
// if ($this->ppassword !== null)
// {
// $encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
// $this->password = $encoder->encodePassword($this->ppassword, $this->getSalt());
// }
return $this;
}
/**
* Gets the user salt.
*
*/
public function getSalt(): ?string
{
return $this->salt;
}
/**
* Gets an object representing the date and time the user was created.
*
* @return integer A DateTime object
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Gets an object representing the date and time the user was created.
*
* @return integer A DateTime object
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* Gets the full name of the user.
*
* @return string The full name
*/
public function getFullName()
{
return sprintf('%s %s', $this->firstname, $this->lastname);
}
/**
* @param Role $role
* @return $this
*/
public function setRole(Role $role)
{
$this->getUserRoles()->clear();
$this->getUserRoles()->add($role);
return $this;
}
/**
* @return Role
*/
public function getRole()
{
return $this->getUserRoles()->first();
}
/**
* Gets the user roles.
*
* @return Collection|Role[]
*/
public function getUserRoles()
{
return $this->userRoles;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = [];
// guarantee every user at least has ROLE_USER
$roles[] = Role::ROLE_USER;
$userRoles = $this->getUserRoles();
foreach ($userRoles as $userRole) {
$roles[] = $userRole->getName();
}
return array_unique($roles);
}
public function isAdmin(): bool
{
return in_array(Role::ROLE_ADMIN, $this->getRoles());
}
/**
* Gets an array of roles.
*
* @param bool|array $filterRoleNames
* @return array An array of Role objects
*/
public function getRoleIds($filterRoleNames=false)
{
$ids = [];
$noFilterMode = !is_array($filterRoleNames);
/** @var Role $role */
foreach ($this->getUserRoles() as $role) {
if ($noFilterMode || in_array($role->getName(), $filterRoleNames)) {
$ids[] = $role->getId();
}
}
return $ids;
}
/**
* this User has a given Role?
*
* @param mixed $role_names
* @return boolean
*/
public function hasRole( $role_names )
{
if ( ! is_array( $role_names ) )
$role_names = array( $role_names );
foreach( $this->getUserRoles() as $role )
{
if ( in_array( $role->getName(), $role_names ) )
return true;
}
return false;
}
/**
* Set createdAt
*
* @param integer $createdAt
* @return User
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Set updatedAt
*
* @param integer $updatedAt
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
}
/**
* Set company
*
* @param Company $company
* @return User
*/
public function setCompany( Company $company )
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* @return Company
*/
public function getCompany()
{
return $this->company;
}
/**
* set the DateTime a password reset request was made
*
* @param DateTime $rra
* @return User
*/
public function setResetRequestedAt( DateTime $rra )
{
$this->reset_requested_at = $rra;
return $this;
}
/**
* get the DateTime a password reset request was made
*
* @return DateTime
*/
public function getResetRequestedAt()
{
return $this->reset_requested_at;
}
/**
* set a hash required for a password reset
*
* @param string $hash
* @return User
*/
public function setResetRequestHash( $hash )
{
$this->reset_request_hash = $hash;
return $this;
}
/**
* get the hash required for a password reset
*
* @return string
*/
public function getResetRequestHash()
{
return $this->reset_request_hash;
}
/**
* reset the reset request (after success)
*
* @return User
*/
public function resetResetRequest()
{
$this->reset_requested_at = null;
$this->reset_request_hash = null;
return $this;
}
/**
* @see Symfony\Component\Security\Core\User\UserInterface::eraseCredentials()
*/
public function eraseCredentials()
{
$this->ppassword = null;
}
/**
* @see Symfony\Component\Security\Core\User\AdvancedUserInterface::isAccountNonExpired()
*/
public function isAccountNonExpired()
{
return true;
}
/**
* @see Symfony\Component\Security\Core\User\AdvancedUserInterface::isAccountNonLocked()
*/
public function isAccountNonLocked()
{
return !$this->isDeleted();
}
/**
* @see Symfony\Component\Security\Core\User\AdvancedUserInterface::isCredentialsNonExpired()
*/
public function isCredentialsNonExpired()
{
return true;
}
/**
* @see Symfony\Component\Security\Core\User\AdvancedUserInterface::isEnabled()
*/
public function isEnabled()
{
return $this->isEmailApproved();
}
/**
* set the time the user last logged in through the mobile app
*
* @param DateTime $app_last_login
* @return User
*/
public function setAppLastLogin( DateTime $app_last_login )
{
$this->app_last_login = $app_last_login;
return $this;
}
/**
* get the time the user last logged in through the mobile app
*
* @return DateTime
*/
public function getAppLastLogin()
{
return $this->app_last_login;
}
/**
* set the time the user last logged in to the portal
*
* @param DateTime $portal_last_login
* @return User
*/
public function setPortalLastLogin(DateTime $portal_last_login)
{
$this->portal_last_login = $portal_last_login;
return $this;
}
/**
* get the time the user last logged in to the portal
*
* @return DateTime
*/
public function getPortalLastLogin()
{
return $this->portal_last_login;
}
/**
* get the email address verification code
* This code is only used for resend-verification
*
* @return string
*/
public function getVerificationCode()
{
return md5($this->getPassword() . $this->getCreatedAt() . $this->getSalt());
//
// $md5 = md5($this->getEmail() . $this->getCreatedAt() . $this->getSalt());
// $offset = substr($this->getId(), -1, 1);
//
// return substr($md5, $offset, 10);
}
/**
* @return EmergencyStock\Order[]
*/
public function getOrders()
{
return $this->orders;
}
/**
* @param EmergencyStock\Order[] $orders
*/
public function setOrders($orders)
{
$this->orders = $orders;
}
public function addOrder(Order $order)
{
$this->orders->add($order);
}
public function removeOrder(Order $order)
{
$this->orders->removeElement($order);
}
/**
* @return Warehouse
*/
public function getWarehouse()
{
return $this->warehouse;
}
/**
* @param Warehouse $warehouse
*/
public function setWarehouse($warehouse)
{
$this->warehouse = $warehouse;
}
/**
* @return DateTime
*/
public function getConvertedFromRotex()
{
return $this->converted_from_rotex;
}
/**
* @param string $format
* @param string $default
* @return string
*/
public function getConvertedFromRotexFormatted($format='d.m.Y H:i', $default='unknown')
{
if ($this->converted_from_rotex instanceof DateTime) {
return $this->converted_from_rotex->format($format);
}
return $default;
}
/**
* @param DateTime $converted_from_rotex
* @return User
*/
public function setConvertedFromRotex(DateTime $converted_from_rotex)
{
$this->converted_from_rotex = $converted_from_rotex;
return $this;
}
/*
BOF Rotex Account Import 9810
Kann nach der Konvertierung eigentlich wieder raus
*/
/**
* @param string $salt
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* @return int
*/
public function getConvertedFromRotexUserId(): int
{
return $this->converted_from_rotex_user_id;
}
/**
* @return bool
*/
public function isConvertedFromRotexUserId(): bool
{
return (int)$this->converted_from_rotex_user_id > 0;
}
/**
* @param int $converted_from_rotex_user_id
* @return User
*/
public function setConvertedFromRotexUserId(int $converted_from_rotex_user_id): User
{
$this->converted_from_rotex_user_id = $converted_from_rotex_user_id;
return $this;
}
/* EOF Rotex Account Import 9810 */
}