<?php
namespace App\Entity;
use App\Enums\CompanyRoleEnum;
use App\Enums\UserRoleEnum;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
use Serializable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Validator\Constraints as Validation;
use Doctrine\ORM\Mapping\AttributeOverrides;
use Doctrine\ORM\Mapping\AttributeOverride;
/**
* @ORM\Table(name="fos_user")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
#[UniqueEntity(
fields: ['username', 'email'],
message: 'Nalog sa ovim korisničkim imenom već postoji!',
errorPath: 'email'
),
UniqueEntity(
fields: ['phoneNumber'],
message: 'Ovaj broj telefona već postoji!',
errorPath: 'phoneNumber'
)
]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public const VRTIC = 1798;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private int $id;
/**
* @ORM\Column(name="username", type="string", length=180)
*/
private string $username;
/**
* @ORM\Column(name="username_canonical", type="string", length=180, unique=true)
*/
private string $usernameCanonical;
/**
* @ORM\Column(name="email", type="string", length=180)
*/
private string $email;
/**
* @ORM\Column(name="email_canonical", type="string", length=180, unique=true)
*/
private string $emailCanonical;
/**
* @ORM\Column(name="enabled", type="boolean")
*/
private bool $enabled;
/**
* The salt to use for hashing.
* @ORM\Column(name="salt", type="string", nullable=true)
*/
private string|null $salt;
/**
* Encrypted password. Must be persisted.
* @ORM\Column(name="password", type="string")
*/
private string $password;
/**
* @ORM\Column(name="last_login", type="datetime", nullable=true)
*/
private DateTime|null $lastLogin;
/**
* Random string sent to the user email address in order to verify it.
* @ORM\Column(name="confirmation_token", type="string", length=180, unique=true, nullable=true)
*/
private string|null $confirmationToken;
/**
* @ORM\Column(name="password_requested_at", type="datetime", nullable=true)
*/
private DateTime|null $passwordRequestedAt;
/**
* @var array<string>
* @ORM\Column(name="roles", type="array")
*/
private array $roles;
/**
* @Validation\NotBlank(message="PLEASE_ENTER_FIRSTNAME", groups={"RequireData"})
* @Validation\Length(
* min="2",
* max="32",
* minMessage="FIRST_NAME_IS_TOO_SHORT",
* maxMessage="FIRST_NAME_IS_TOO_LONG",
* groups={"RequireData"}
* )
*/
private string|null $plainPassword = null;
/**
* @Validation\NotBlank(message="PLEASE_ENTER_FIRSTNAME", groups={"RequireData"})
* @Validation\Length(
* min="2",
* max="32",
* minMessage="FIRST_NAME_IS_TOO_SHORT",
* maxMessage="FIRST_NAME_IS_TOO_LONG",
* groups={"RequireData"}
* )
* @ORM\Column(name="firstname", type="string", length=255, nullable=true)
*/
private string|null $firstname;
/**
* @Validation\NotBlank(message="PLEASE_ENTER_LASTNAME", groups={"RequireData"})
* @Validation\Length(
* min="2",
* max="32",
* minMessage="LAST_NAME_IS_TOO_SHORT",
* maxMessage="LAST_NAME_IS_TOO_LONG",
* groups={"RequireData"}
* )
* @ORM\Column(name="lastname", type="string", length=255, nullable=true)
*/
private string|null $lastname;
/**
* @Validation\NotBlank(message="MUST_ACCEPT_PRIVACY_POLICY", groups={"RequireData"})
* @ORM\Column(name="has_accepted_terms", type="boolean")
*/
private bool $hasAcceptedTerms;
/**
* @Validation\Length(max="50", maxMessage="ADDRESS_TOO_LONG")
* @ORM\Column(name="address", type="string", nullable=true)
* @Validation\NotBlank(message="FIELD_MUST_NOT_BE_EMPTY", groups={"RequireData"})
*/
private string|null $address = null;
/**
* @Validation\Length(max="4", maxMessage="STREETNUMBER_TOO_LONG")
* @ORM\Column(name="streetnumber", type="string", nullable=true)
* @Validation\NotBlank(message="FIELD_MUST_NOT_BE_EMPTY", groups={"RequireData"})
*/
private string|null $streetNumber;
/**
* @Validation\Length(max="8", maxMessage="ZIP_TOO_LONG")
* @ORM\Column(name="zip", type="string", nullable=true)
* @Validation\NotBlank(message="FIELD_MUST_NOT_BE_EMPTY", groups={"RequireData"})
*/
private string|null $zip;
/**
* @Validation\Length(max="100", maxMessage="CITY_IS_TOO_LONG", groups={"RequireData"})
* @ORM\Column(name="city", type="string", length=255, nullable=true)
* @Validation\NotBlank(message="FIELD_MUST_NOT_BE_EMPTY", groups={"RequireData"})
*/
private string|null $city = null;
/**
* @ORM\Column(name="phone_number", type="string", length=255, nullable=true, unique=true)
*/
private string|null $phoneNumber = null;
/**
* @ORM\Column(name="company_name", type="string", length=255, nullable=true, unique=true)
*/
private string|null $companyName = null;
/**
* @ORM\Column(name="created_at", type="datetime")
*/
private DateTime|null $createdAt = null;
/**
* @ORM\Column(name="updated_at", type="datetime")
*/
private DateTime|null $updatedAt = null;
/**
* @ORM\Column(name="mat_id", type="string", length=64, nullable=true)
*/
private string|null $matId = null;
/**
* @ORM\Column(name="pib", type="string", length=64, nullable=true)
*/
private string|null $pib = null;
/**
* @ORM\Column(name="company_title", type="string", length=255, nullable=true)
*/
private string|null $companyTitle;
/**
* @ORM\Column(name="license_id", type="string", length=64, nullable=true)
*/
private string|null $licenseId;
/**
* @ORM\Column(name="bank_account", type="string", length=128, nullable=true)
*/
private string|null $bankAccount = null;
/**
* @ORM\Column(name="enable_qr", type="boolean", options={"default" : true})
*/
private bool $enableQr = true;
/**
* @var Collection<int, Obligation>
* @ORM\OneToMany(targetEntity="App\Entity\Obligation", mappedBy="user")
*/
#[Ignore]
private Collection $obligations;
/**
* @ORM\Column(name="task_notification_email", type="string", length=180, nullable=true)
*/
private string|null $taskNotificationEmail;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="main_user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* })
*/
#[Ignore]
private User|null $mainUser = null;
/**
* @var Collection<int, ActionLog>
* @ORM\OneToMany(targetEntity=ActionLog::class, mappedBy="user")
*/
#[Ignore]
private Collection $actionLogs;
public function __construct()
{
$this->enabled = false;
$this->roles = array();
$this->obligations = new ArrayCollection();
$this->actionLogs = new ArrayCollection();
$this->incrementCreatedAt();
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return string
*/
public function getUsername(): string
{
return $this->username;
}
/**
* @param string $username
*/
public function setUsername(string $username): void
{
$this->username = $username;
}
/**
* @return string
*/
public function getUsernameCanonical(): string
{
return $this->usernameCanonical;
}
/**
* @param string $usernameCanonical
*/
public function setUsernameCanonical(string $usernameCanonical): void
{
$this->usernameCanonical = $usernameCanonical;
}
/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}
/**
* @param string $email
*/
public function setEmail(string $email): void
{
$this->setUsername($email);
$this->email = $email;
}
/**
* @return string
*/
public function getEmailCanonical(): string
{
return $this->emailCanonical;
}
/**
* @param string $emailCanonical
*/
public function setEmailCanonical(string $emailCanonical): void
{
$this->emailCanonical = $emailCanonical;
}
/**
* @return bool
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* @return bool
*/
public function isSuperAdmin(): bool
{
return $this->hasRole(UserRoleEnum::ROLE_SUPER_ADMIN->value);
}
/**
* @param bool $enabled
*/
public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}
/**
* @return string|null
*/
public function getSalt(): ?string
{
return $this->salt;
}
/**
* @param string|null $salt
*/
public function setSalt(?string $salt): void
{
$this->salt = $salt;
}
/**
* @return string
*/
public function getPassword(): string
{
return $this->password;
}
/**
* @param string $password
*/
public function setPassword(string $password): void
{
$this->password = $password;
}
/**
* @return DateTime|null
*/
public function getLastLogin(): ?DateTime
{
return $this->lastLogin;
}
/**
* @param DateTime|null $lastLogin
*/
public function setLastLogin(?DateTime $lastLogin): void
{
$this->lastLogin = $lastLogin;
}
/**
* @return string|null
*/
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
/**
* @param string|null $confirmationToken
*/
public function setConfirmationToken(?string $confirmationToken): void
{
$this->confirmationToken = $confirmationToken;
}
/**
* @return DateTime|null
*/
public function getPasswordRequestedAt(): ?DateTime
{
return $this->passwordRequestedAt;
}
/**
* @param DateTime|null $passwordRequestedAt
*/
public function setPasswordRequestedAt(?DateTime $passwordRequestedAt): void
{
$this->passwordRequestedAt = $passwordRequestedAt;
}
/**
* @return string[]
*/
public function getRoles(): array
{
$roles = $this->roles;
// we need to make sure to have at least one role
$roles[] = UserRoleEnum::ROLE_DEFAULT->value;
return array_unique($roles);
}
/**
* @param string $role
* @return bool
*/
#[Ignore]
public function hasRole(string $role): bool
{
return in_array(strtoupper($role), $this->getRoles(), true);
}
/**
* @param string[] $roles
* @return $this
*/
public function setRoles(array $roles): self
{
$this->roles = array();
foreach ($roles as $role) {
$this->addRole($role);
}
return $this;
}
/**
* @param int $ttl
* @return bool
*/
#[Ignore]
public function isPasswordRequestNonExpired(int $ttl): bool
{
return $this->getPasswordRequestedAt() instanceof \DateTime &&
$this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
}
/**
* @return string|null
*/
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
/**
* @param string|null $plainPassword
*/
public function setPlainPassword(?string $plainPassword): void
{
$this->plainPassword = $plainPassword;
}
/**
* @return string|null
*/
public function getFirstname(): ?string
{
return $this->firstname;
}
/**
* @param string|null $firstname
*/
public function setFirstname(?string $firstname): void
{
$this->firstname = $firstname;
}
/**
* @return string|null
*/
public function getLastname(): ?string
{
return $this->lastname;
}
/**
* @param string|null $lastname
*/
public function setLastname(?string $lastname): void
{
$this->lastname = $lastname;
}
/**
* @return bool
*/
public function getHasAcceptedTerms(): bool
{
return $this->hasAcceptedTerms;
}
/**
* @param bool $hasAcceptedTerms
*/
public function setHasAcceptedTerms(bool $hasAcceptedTerms): void
{
$this->hasAcceptedTerms = $hasAcceptedTerms;
}
/**
* @return string|null
*/
public function getAddress(): ?string
{
return $this->address;
}
/**
* @param string|null $address
*/
public function setAddress(?string $address): void
{
$this->address = $address;
}
/**
* @return string|null
*/
public function getStreetnumber(): ?string
{
return $this->streetNumber;
}
/**
* @param string|null $streetNumber
*/
public function setStreetNumber(?string $streetNumber): void
{
$this->streetNumber = $streetNumber;
}
/**
* @return string|null
*/
public function getZip(): ?string
{
return $this->zip;
}
/**
* @param string|null $zip
*/
public function setZip(?string $zip): void
{
$this->zip = $zip;
}
/**
* @return string|null
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* @param string|null $city
*/
public function setCity(?string $city): void
{
$this->city = $city;
}
/**
* @return string|null
*/
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
/**
* @param string|null $phoneNumber
*/
public function setPhoneNumber(?string $phoneNumber): void
{
$this->phoneNumber = $phoneNumber;
}
/**
* @return string|null
*/
public function getCompanyName(): ?string
{
return $this->companyName;
}
/**
* @param string|null $companyName
*/
public function setCompanyName(?string $companyName): void
{
$this->companyName = $companyName;
}
/**
* @return DateTime|null
*/
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
/**
* @param DateTime $createdAt
*/
public function setCreatedAt(DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* @return DateTime
*/
public function getUpdatedAt(): DateTime
{
return $this->updatedAt;
}
/**
* @param DateTime $updatedAt
*/
public function setUpdatedAt(DateTime $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
/**
* @ORM\PrePersist
*/
public function incrementCreatedAt(): void
{
if ($this->createdAt === null) {
$this->createdAt = new DateTime();
}
$this->updatedAt = new DateTime();
}
/**
* @ORM\PreUpdate
*/
public function incrementUpdatedAt(): void
{
$this->updatedAt = new DateTime();
}
/**
* @return string|null
*/
public function getMatId(): ?string
{
return $this->matId;
}
/**
* @param string|null $matId
*/
public function setMatId(?string $matId): void
{
$this->matId = $matId;
}
/**
* @return string|null
*/
public function getPib(): ?string
{
return $this->pib;
}
/**
* @param string|null $pib
*/
public function setPib(?string $pib): void
{
$this->pib = $pib;
}
/**
* @return string|null
*/
public function getCompanyTitle(): ?string
{
return $this->companyTitle;
}
/**
* @param string|null $companyTitle
*/
public function setCompanyTitle(?string $companyTitle): void
{
$this->companyTitle = $companyTitle;
}
/**
* @return string|null
*/
public function getLicenseId(): ?string
{
return $this->licenseId;
}
/**
* @param string|null $licenseId
*/
public function setLicenseId(?string $licenseId): void
{
$this->licenseId = $licenseId;
}
/**
* @return string|null
*/
public function getBankAccount(): ?string
{
return $this->bankAccount;
}
/**
* @param string|null $bankAccount
*/
public function setBankAccount(?string $bankAccount): void
{
$this->bankAccount = $bankAccount;
}
/**
* @return bool
*/
public function getEnableQr(): bool
{
return $this->enableQr;
}
/**
* @param bool $enableQr
*/
public function setEnableQr(bool $enableQr): void
{
$this->enableQr = $enableQr;
}
/**
* @return Collection<int, Obligation>
*/
#[Ignore]
public function getObligations(): Collection
{
return $this->obligations;
}
/**
* @param Obligation $obligation
* @return $this
*/
public function addObligation(Obligation $obligation): self
{
if (!$this->obligations->contains($obligation)) {
$this->obligations[] = $obligation;
$obligation->setUser($this);
}
return $this;
}
/**
* @param Obligation $obligation
* @return $this
*/
public function removeObligation(Obligation $obligation): self
{
if ($this->obligations->contains($obligation)) {
$this->obligations->removeElement($obligation);
if ($obligation->getUser() === $this) {
$obligation->setUser(null);
}
}
return $this;
}
/**
* @return string|null
*/
public function getTaskNotificationEmail(): ?string
{
return $this->taskNotificationEmail;
}
/**
* @param string|null $taskNotificationEmail
*/
public function setTaskNotificationEmail(?string $taskNotificationEmail): void
{
$this->taskNotificationEmail = $taskNotificationEmail;
}
/**
* @return User|null
*/
#[Ignore]
public function getMainUser(): ?User
{
return $this->mainUser;
}
/**
* @param User|null $mainUser
*/
#[Ignore]
public function setMainUser(?User $mainUser): void
{
$this->mainUser = $mainUser;
}
/**
* Returns company role or null
* @return string|null
*/
public function getCompanyRole(): ?string
{
foreach ($this->getRoles() as $role) {
if (in_array($role, CompanyRoleEnum::getAllValues(), true)) {
return $role;
}
}
return null;
}
/**
* Returns property which is used for createdBy fields of other tables
*
* @return string
*/
public function getCreatedByValue(): string
{
return sprintf('%s.%s.', mb_substr($this->getFirstname(), 0, 1), mb_substr($this->getLastname(), 0, 1));
}
/**
* @return User
*/
#[Ignore]
public function getSharedCompanyUser(): User
{
return $this->mainUser !== null ? $this->mainUser : $this;
}
/**
* @return array<User>
*/
public function getUserAndMainUser(): array
{
$users = [];
$users[] = $this;
if ($this->mainUser) {
$users[] = $this->mainUser;
}
return $users;
}
/**
* @param string $role
* @return $this
*/
public function addRole(string $role): self
{
$role = strtoupper($role);
if ($role === UserRoleEnum::ROLE_DEFAULT->value) {
return $this;
}
if (!in_array($role, $this->roles, true)) {
if (in_array($role, CompanyRoleEnum::getAllValues(), true)) {
foreach (CompanyRoleEnum::getAllValues() as $companyRole) {
$this->removeRole($companyRole);
}
}
$this->roles[] = $role;
}
return $this;
}
/**
* @param string $role
* @return $this
*/
public function removeRole(string $role): self
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
/**
* @return string
*/
public function getFullName(): string
{
return sprintf('%s %s', $this->getFirstname(), $this->getLastname());
}
/**
* @return Collection<int, ActionLog>
*/
#[Ignore]
public function getActionLogs(): Collection
{
return $this->actionLogs;
}
/**
* @param ActionLog $actionLog
* @return $this
*/
public function addActionLog(ActionLog $actionLog): self
{
if (!$this->actionLogs->contains($actionLog)) {
$this->actionLogs[] = $actionLog;
$actionLog->setUser($this);
}
return $this;
}
/**
* @param ActionLog $actionLog
* @return $this
*/
public function removeActionLog(ActionLog $actionLog): self
{
if ($this->actionLogs->removeElement($actionLog)) {
// set the owning side to null (unless already changed)
if ($actionLog->getUser() === $this) {
$actionLog->setUser(null);
}
}
return $this;
}
/**
* @return string
*/
public function __toString(): string
{
return (string) $this->getUsername();
}
//
// /**
// * @return string
// */
// public function serialize(): string
// {
// return serialize(array(
// $this->password,
// $this->getSalt(),
// $this->usernameCanonical,
// $this->username,
// $this->enabled,
// $this->id,
// $this->email,
// $this->emailCanonical,
// ));
// }
//
// /**
// * @param string $serialized
// */
// public function unserialize($serialized): void
// {
// $data = unserialize($serialized);
//
// if (13 === count($data)) {
// // Unserializing a User object from 1.3.x
// unset($data[4], $data[5], $data[6], $data[9], $data[10]);
// $data = array_values($data);
// } elseif (11 === count($data)) {
// // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
// unset($data[4], $data[7], $data[8]);
// $data = array_values($data);
// }
//
// list(
// $this->password,
// $this->salt,
// $this->usernameCanonical,
// $this->username,
// $this->enabled,
// $this->id,
// $this->email,
// $this->emailCanonical
// ) = $data;
// }
public function eraseCredentials(): void
{
$this->setPlainPassword(null);
}
/**
* @param bool $boolean
* @return $this
*/
public function setSuperAdmin(bool $boolean): self
{
if ($boolean === true) {
$this->addRole(UserRoleEnum::ROLE_SUPER_ADMIN->value);
} else {
$this->removeRole(UserRoleEnum::ROLE_SUPER_ADMIN->value);
}
return $this;
}
/**
* @return string
*/
public function getUserIdentifier(): string
{
return $this->getUsername();
}
/**
* @return bool
*/
public function isUserCyrillic(): bool
{
$userArray = ['draganaDjordjevic'];
if (in_array($this->getCompanyName(), $userArray)) {
return true;
}
return false;
}
}