<?php
namespace App\Entity;
use App\Utils\Commons;
use App\Utils\Media;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
use Scheb\TwoFactorBundle\Model\BackupCodeInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(fields={"username"}, message="Username already exists")
* @UniqueEntity(fields={"email"}, message="Email already exists")
*/
class User extends Media implements UserInterface, TwoFactorInterface, BackupCodeInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=50, unique=true)
* @Assert\NotBlank(message="Please provide email")
* @Assert\Email(
* message = "Please enter valid email."
* )
*/
private $email;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $oldEmail = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $emailRecoveryCode = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $emailRecoveryCodeExpireDateTime = null;
/**
* @ORM\Column(type="json")
*/
private $roles;
/**
* @var string The hashed password
* @ORM\Column(type="string", nullable=true)
* @Assert\Length(max = 4096)
*/
private $password;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $prename;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $surname;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $phone;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $info;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $parent;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $memberSince;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $lastLogin;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $status;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $language;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $pageLimit;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\UserGroup", inversedBy="users")
*/
private $userGroups;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\BackendGroup", inversedBy="users")
*/
private $backendGroups;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Team", inversedBy="users")
*/
private $teams;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Team", inversedBy="customers")
*/
private $customerTeam;
/**
* @ORM\OneToMany(targetEntity="App\Entity\FileGroup", mappedBy="user")
*/
private $fileGroups;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Support", mappedBy="user")
*/
private $supports;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Workspace", inversedBy="users")
*/
private $workspace;
/**
* @ORM\OneToOne(targetEntity="App\Entity\CustomerDetail", mappedBy="customer", cascade={"all"})
*/
private $customerDetail;
/**
* @ORM\OneToMany(targetEntity="App\Entity\WishListLike", mappedBy="user")
*/
private $userLikes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserPopUpMessage", mappedBy="user",cascade={"persist","remove"} )
*/
private $popUpMessages;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $timezone = 'Europe/Berlin';
/**
* @ORM\Column(name="googleAuthenticatorSecret", type="string", nullable=true)
*/
private $googleAuthenticatorSecret;
/**
* @ORM\Column(type="json")
*/
private $backupCodes = [];
/**
* @ORM\Column(type="integer", options={"default": 0})
*/
private $loginCounter = 0;
/**
* @ORM\Column(type="boolean", options={"default" : false}))
*/
private $deleted = false;
/**
* @ORM\OneToOne(targetEntity="App\Entity\ResetPassword", mappedBy="user")
*/
private $resetPassword;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Dashboard", mappedBy="user")
*/
private $dashboard;
public function __construct()
{
$this->memberSince = new DateTime();
$this->roles = [Commons::ROLE_USER];
$this->userGroups = new ArrayCollection();
$this->fileGroups = new ArrayCollection();
$this->backendGroups = new ArrayCollection();
$this->teams = new ArrayCollection();
$this->userLikes = new ArrayCollection();
$this->supports = new ArrayCollection();
$this->popUpMessages = new ArrayCollection();
$this->status = 1;
$this->backupCodes = [];
}
public function __toString(): string
{
return $this->getPrename() . ' ' . $this->getSurname();
}
/**
* @ORM\PostLoad
*/
public function init()
{
$this->type = Media::USER;
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string)$this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
return $this->roles;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string)$this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getPrename(): ?string
{
return $this->prename;
}
public function setPrename(?string $prename): self
{
$this->prename = $prename;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(?string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getMemberSince(): ?DateTimeInterface
{
return $this->memberSince;
}
public function setMemberSince(?DateTimeInterface $memberSince): self
{
$this->memberSince = $memberSince;
return $this;
}
public function getLastLogin(): ?DateTimeInterface
{
return $this->lastLogin;
}
public function setLastLogin(?DateTimeInterface $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
public function getInfo(): ?string
{
return $this->info;
}
public function setInfo(?string $info): self
{
$this->info = $info;
return $this;
}
public function getParent(): ?string
{
return $this->parent;
}
public function setParent(?string $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection|UserGroup[]
*/
public function getUserGroups(): Collection
{
return $this->userGroups;
}
public function addUserGroup(UserGroup $userGroup): self
{
if (!$this->userGroups->contains($userGroup)) {
$this->userGroups[] = $userGroup;
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): self
{
if ($this->userGroups->contains($userGroup)) {
$this->userGroups->removeElement($userGroup);
}
return $this;
}
/**
* @return Collection|FileGroup[]
*/
public function setFileGroups(): Collection
{
return $this->fileGroups;
}
public function addFileGroup(FileGroup $fileGroup): self
{
if (!$this->fileGroups->contains($fileGroup)) {
$this->fileGroups[] = $fileGroup;
}
return $this;
}
public function removeFileGroup(FileGroup $fileGroup): self
{
if ($this->fileGroups->contains($fileGroup)) {
$this->userGroups->removeElement($fileGroup);
}
return $this;
}
/**
* @return Collection|FileGroup[]
*/
public function getFileGroups(): Collection
{
return $this->fileGroups;
}
public function getWorkspace(): ?Workspace
{
return $this->workspace;
}
public function setWorkspace(?Workspace $workspace): self
{
$this->workspace = $workspace;
return $this;
}
/**
* @return Collection|BackendGroup[]
*/
public function getBackendGroups(): Collection
{
return $this->backendGroups;
}
public function addBackendGroup(BackendGroup $backendGroup): self
{
if (!$this->backendGroups->contains($backendGroup)) {
$this->backendGroups[] = $backendGroup;
}
return $this;
}
public function removeBackendGroup(BackendGroup $backendGroup): self
{
if ($this->backendGroups->contains($backendGroup)) {
$this->backendGroups->removeElement($backendGroup);
}
return $this;
}
public function getStatus(): ?bool
{
return $this->status;
}
public function setStatus(?bool $status): self
{
$this->status = $status;
return $this;
}
/**
* @return Collection|Team[]
*/
public function getTeams(): Collection
{
return $this->teams;
}
public function addTeam(Team $team): self
{
if (!$this->teams->contains($team)) {
$this->teams[] = $team;
}
return $this;
}
public function removeTeam(Team $team): self
{
if ($this->teams->contains($team)) {
$this->teams->removeElement($team);
}
return $this;
}
public function getCustomerTeam(): ?Team
{
return $this->customerTeam;
}
public function setCustomerTeam(?Team $customerTeam): self
{
$this->customerTeam = $customerTeam;
return $this;
}
public function getCustomerDetail(): ?CustomerDetail
{
return $this->customerDetail;
}
public function setCustomerDetail(?CustomerDetail $customerDetail): self
{
$this->customerDetail = $customerDetail;
// set (or unset) the owning side of the relation if necessary
$newCustomer = null === $customerDetail ? null : $this;
if ($customerDetail->getCustomer() !== $newCustomer) {
$customerDetail->setCustomer($newCustomer);
}
return $this;
}
public function getLanguage(): ?string
{
return $this->language;
}
public function setLanguage(?string $language): self
{
$this->language = $language;
return $this;
}
public function getPageLimit(): ?int
{
return $this->pageLimit;
}
public function setPageLimit(?int $pageLimit): self
{
$this->pageLimit = $pageLimit;
return $this;
}
/**
* @param WishListLike $wishListLike
*
* @return $this
*/
public function addUserLike(WishListLike $wishListLike): self
{
if (!$this->userLikes->contains($wishListLike)) {
$this->userLikes[] = $wishListLike;
}
return $this;
}
/**
* @param WishListLike $wishListLike
*
* @return $this
*/
public function removeUserLike(WishListLike $wishListLike): self
{
if ($this->userLikes->contains($wishListLike)) {
$this->userLikes->removeElement($wishListLike);
}
return $this;
}
/**
* @return Collection|WishListLike[]
*/
public function getUserLikes(): Collection
{
return $this->userLikes;
}
/**
* @return Collection|Support[]
*/
public function getSupports(): Collection
{
return $this->supports;
}
/**
* @param Support $support
*
* @return $this
*/
public function addSupport(Support $support): self
{
if (!$this->supports->contains($support)) {
$this->supports->add($support);
$support->setUser($this);
}
return $this;
}
/**
* @param Support $support
*
* @return $this
*/
public function removeSupport(Support $support): self
{
if ($this->supports->contains($support)) {
$this->supports->removeElement($support);
if ($support->getWorkspace() === $this) {
$support->setUser(null);
}
}
return $this;
}
/**
* @return string|null
*/
public function getTimezone(): ?string
{
return $this->timezone;
}
/**
* @param mixed $timezone
*/
public function setTimezone($timezone): void
{
$this->timezone = $timezone;
}
/**
* @return mixed
*/
public function getPopUpMessages()
{
return $this->popUpMessages;
}
public function isGoogleAuthenticatorEnabled(): bool
{
return $this->googleAuthenticatorSecret ? true : false;
}
public function getGoogleAuthenticatorUsername(): string
{
return $this->username;
}
public function getGoogleAuthenticatorSecret(): ?string
{
return $this->googleAuthenticatorSecret;
}
public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
{
$this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
}
/**
* Check if it is a valid backup code.
*
* @param string $code
*
* @return bool
*/
public function isBackupCode(string $code): bool
{
return in_array($code, $this->getBackupCodes());
}
/**
* Invalidate a backup code
*
* @param string $code
*/
public function invalidateBackupCode(string $code): void
{
$key = array_search($code, $this->getBackupCodes());
if ($key !== false) {
unset($this->backupCodes[$key]);
$codes = $this->backupCodes;
$this->backupCodes = array_values($codes);
}
}
/**
* Add a backup code
*
* @param string $code
*/
public function addBackupCode(string $code): void
{
$key = array_search($code, $this->getBackupCodes());
if ($key === false) {
$codes = $this->getBackupCodes();
$codes[] = $code;
$this->backupCodes = array_values($codes);
}
}
/**
* @return array
*/
public function getBackupCodes(): array
{
if ($this->backupCodes === null) {
$this->backupCodes = [];
}
return $this->backupCodes;
}
public function resetBackupCodes()
{
$this->backupCodes = [];
}
/**
* @param int $length
*/
public function generateRandomNumber($length = 32)
{
$random = "";
srand((double)microtime() * 1000000);
$data = "123456123456789071234567890890";
// $data .= "aBCdefghijklmn123opq45rs67tuv89wxyz"; // if you need alphabatic also
for ($i = 0; $i < $length; $i++) {
$random .= substr($data, (rand() % (strlen($data))), 1);
}
$this->addBackupCode($random);
}
/**
* @param int $length
* @return string
*/
public function generateRandomPassword($length = 32): string
{
$random = "";
srand((double)microtime() * 1000000);
$data = "123456123456789071234567890890";
$data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";
for ($i = 0; $i < $length; $i++) {
$random .= substr($data, (rand() % (strlen($data))), 1);
}
return $random;
}
/**
* @return int
*/
public function getLoginCounter(): int
{
return $this->loginCounter;
}
/**
* @return int
*/
public function incrementLoginCounter(): int
{
$this->loginCounter++;
return $this->loginCounter;
}
/**
* @param int $loginCounter
*/
public function setLoginCounter(int $loginCounter): void
{
$this->loginCounter = $loginCounter;
}
/**
* @return bool
*/
public function isDeleted(): bool
{
return $this->deleted;
}
/**
* @param bool $deleted
*/
public function setDeleted(bool $deleted): void
{
$this->deleted = $deleted;
}
/**
* @return null|string
*/
public function getOldEmail(): ?string
{
return $this->oldEmail;
}
/**
* @param string|null $oldEmail
*/
public function setOldEmail(?string $oldEmail): void
{
$this->oldEmail = $oldEmail;
}
/**
* @return string|null
*/
public function getEmailRecoveryCode(): ?string
{
return $this->emailRecoveryCode;
}
/**
* @param string| null $emailRecoveryCode
*/
public function setEmailRecoveryCode(?string $emailRecoveryCode): void
{
$this->emailRecoveryCode = $emailRecoveryCode;
}
/**
* @return DateTimeInterface|null
*/
public function getEmailRecoveryCodeExpireDateTime(): ?DateTimeInterface
{
return $this->emailRecoveryCodeExpireDateTime;
}
/**
* @param DateTimeInterface| null $emailRecoveryCodeExpireDateTime
*/
public function setEmailRecoveryCodeExpireDateTime(?DateTimeInterface $emailRecoveryCodeExpireDateTime): void
{
$this->emailRecoveryCodeExpireDateTime = $emailRecoveryCodeExpireDateTime;
}
/**
* @return null|ResetPassword
*/
public function getResetPassword(): ?ResetPassword
{
return $this->resetPassword;
}
/**
* @param null|ResetPassword $resetPassword
*/
public function setResetPassword(?ResetPassword $resetPassword): void
{
$this->resetPassword = $resetPassword;
}
/**
* @return mixed
*/
public function getDashboard()
{
return $this->dashboard;
}
/**
* @param mixed $dashboard
*/
public function setDashboard($dashboard): void
{
$this->dashboard = $dashboard;
}
}