<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Psr\Log\LoggerInterface;
use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
use Bundles\Messenger\Entity\Event;
use Bundles\Messenger\Entity\Notification;
use Bundles\Messenger\Entity\WebpushSubscription;
use Bundles\Portfolios\Entity\Portfolio;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*
* @RollerworksPassword\PasswordRequirements(requireLetters=true, requireNumbers=true, requireCaseDiff=true)
*/
private $password;
/**
* @ORM\Column(type="boolean")
*/
private $enabled = false;
/**
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastLoginAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="users")
*/
private $customer;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isConsultant = false;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $accessRemoteSystems = false;
/**
* @ORM\ManyToMany(targetEntity=Customer::class)
* @ORM\JoinTable(name="user_consultant")
*
*/
private $consultants;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $passwRestoreKey;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $passwRestoreKeyValidTil;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $passwChanged;
/**
* @ORM\Column(type="smallint", nullable=true)
*/
private $passwChangeCount;
/**
* @ORM\Column(type="string", length=20)
*/
private $adminPhone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $apiKey;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $allowedIps;
/**
* @ORM\OneToMany(targetEntity=Event::class, mappedBy="User")
*/
private $events;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $notifications = [];
/**
* @ORM\Column(type="json", nullable=true)
*/
private $notificationTypes = [];
/**
* @ORM\OneToMany(targetEntity=WebpushSubscription::class, mappedBy="User", orphanRemoval=true)
*/
private $webpushSubscriptions;
/**
* @ORM\ManyToMany(targetEntity=Portfolio::class)
* @ORM\JoinTable(name="user_portfolios")
*/
private $userPortfolios;
/**
* @var bool
*/
private $sendInvitation;
private $customers;
public function __construct() {
$this->setCreated(new \DateTime());
$this->events = new ArrayCollection();
$this->webpushSubscriptions = new ArrayCollection();
$this->consultants = new ArrayCollection();
$this->customers = new ArrayCollection();
$this->userPortfolios = new ArrayCollection();
}
public function getLogger() {
return $this->logger;
}
public function getId(): ?int {
return $this->id;
}
public function setId($id): ?int {
return $this->id;
}
public function getEmail(): ?string {
return $this->email;
}
public function setEmail(string $email): self {
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getUserIdentifier(): string {
return (string)$this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): ?array {
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
// $roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles($roles): self {
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string {
return (string)$this->password;
}
public function setPassword($password): self {
if (!empty($password)) {
$this->password = $password;
}
return $this;
}
/**
* @return string|null
*/
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
}
public function __toString() {
return (string)$this->email;
}
public function getEnabled(): ?bool {
return $this->enabled;
}
public function setEnabled(bool $enabled): self {
$this->enabled = $enabled;
return $this;
}
public function getCreated(): ?\DateTimeInterface {
return $this->created;
}
public function setCreated(\DateTimeInterface $created): self {
$this->created = $created;
return $this;
}
public function getLastLoginAt(): ?\DateTimeInterface {
return $this->lastLoginAt;
}
public function setLastLoginAt(?\DateTimeInterface $lastLoginAt): self {
$this->lastLoginAt = $lastLoginAt;
return $this;
}
public function getCustomer() {
return $this->customer;
}
public function setCustomer($customer) {
$this->customer = $customer;
return $this;
}
public function isLimitedUser() {
return $this->isUser() && $this->userPortfolios->count() > 0 ;
}
public function isUser() {
$roles = $this->getRoles();
$userRoles = array_unique($roles);
return count($userRoles) === 1 && in_array('ROLE_USER', $userRoles, true);
}
public function isManager() {
$roles = $this->getRoles();
return in_array('ROLE_MANAGER', $roles);
}
public function isAdmin() {
$roles = $this->getRoles();
return (in_array('ROLE_ADMIN', $roles) || in_array('ROLE_SUPER_ADMIN', $roles));
}
public function isSuperAdmin() {
$roles = $this->getRoles();
return in_array('ROLE_SUPER_ADMIN', $roles);
}
public function getPasswRestoreKey(): ?string {
return $this->passwRestoreKey;
}
public function setPasswRestoreKey(?string $passwRestoreKey): self {
$this->passwRestoreKey = $passwRestoreKey;
return $this;
}
public function getPasswRestoreKeyValidTil(): ?\DateTimeInterface {
return $this->passwRestoreKeyValidTil;
}
public function setPasswRestoreKeyValidTil(?\DateTimeInterface $passwRestoreKeyValidTil): self {
$this->passwRestoreKeyValidTil = $passwRestoreKeyValidTil;
return $this;
}
public function getPasswChanged(): ?\DateTimeInterface {
return $this->passwChanged;
}
public function setPasswChanged(?\DateTimeInterface $passwChanged): self {
$this->passwChanged = $passwChanged;
return $this;
}
public function getPasswChangeCount(): ?int {
return $this->passwChangeCount;
}
public function setPasswChangeCount(?int $passwChangeCount): self {
$this->passwChangeCount = $passwChangeCount;
return $this;
}
public function getAdminPhone(): ?string {
return $this->adminPhone;
}
public function setAdminPhone(string $adminPhone): self {
$this->adminPhone = $adminPhone;
return $this;
}
public function getApiKey(): ?string {
return $this->apiKey;
}
public function setApiKey(?string $apiKey): self {
$this->apiKey = $apiKey;
return $this;
}
public function getAllowedIps(): ?string {
return $this->allowedIps;
}
public function setAllowedIps(?string $allowedIps): self {
$this->allowedIps = $allowedIps;
return $this;
}
/**
* @return Collection<int, Event>
*/
public function getEvents(): Collection {
return $this->events;
}
public function addEvent(Event $event): self {
if (!$this->events->contains($event)) {
$this->events[] = $event;
$event->setUser($this);
}
return $this;
}
public function removeEvent(Event $event): self {
if ($this->events->removeElement($event)) {
// set the owning side to null (unless already changed)
if ($event->getUser() === $this) {
$event->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Notification>
*/
public function getNotifications() {
return $this->notifications;
}
public function setNotifications($notifications) {
return $this->notifications = $notifications;
}
/**
* @return Collection<int, WebpushSubscription>
*/
public function getWebpushSubscriptions(): Collection {
return $this->webpushSubscriptions;
}
public function addWebpushSubscription(WebpushSubscription $webpushSubscription): self {
if (!$this->webpushSubscriptions->contains($webpushSubscription)) {
$this->webpushSubscriptions[] = $webpushSubscription;
$webpushSubscription->setUser($this);
}
return $this;
}
public function removeWebpushSubscription(WebpushSubscription $webpushSubscription): self {
if ($this->webpushSubscriptions->removeElement($webpushSubscription)) {
// set the owning side to null (unless already changed)
if ($webpushSubscription->getUser() === $this) {
$webpushSubscription->setUser(null);
}
}
return $this;
}
public function getUsername() {
return $this->email;
}
public function getSendInvitation() {
return $this->sendInvitation;
}
public function setCustomers($customers) {
$this->customers = $customers;
}
public function getCustomers() {
return $this->customers;
}
public function setSendInvitation($sendInvitation) {
$this->sendInvitation = $sendInvitation;
}
public function getConsultants(): Collection|array {
return $this->consultants;
}
public function setConsultants(Collection|array $consultants): void {
$this->consultants = $consultants;
}
public function addConsultants(Customer $consultants): self {
if (!$this->consultants->contains($consultants)) {
$this->consultants[] = $consultants;
}
return $this;
}
public function removeConsultants(Collection $consultants): self {
$this->consultants->removeElement($consultants);
return $this;
}
public function isConsultant(): ?bool {
return !empty($this->consultants);
}
public function setIsConsultant(?bool $isConsultant): void {
$this->isConsultant = $isConsultant;
}
public function getNotificationTypes() {
return $this->notificationTypes;
}
public function setNotificationTypes(array $notificationTypes): void {
$this->notificationTypes = $notificationTypes;
}
public function isAccessRemoteSystems(): ?bool {
return $this->accessRemoteSystems;
}
public function setAccessRemoteSystems(?bool $accessRemoteSystems): void {
$this->accessRemoteSystems = $accessRemoteSystems;
}
public function getUserPortfolios(): Collection {
return $this->userPortfolios;
}
public function setUserPortfolios(Collection $portfolios = null): self {
$this->userPortfolios = new ArrayCollection();
foreach ($portfolios as $portfolio) {
$this->userPortfolios[] = $portfolio;
}
return $this;
}
public function removeUserPortfolio(Portfolio $portfolio): self
{
$this->userPortfolios->removeElement($portfolio);
return $this;
}
}