<?php
namespace App\Entity\Master;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\Master\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="eaml_m_user")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public function __toString()
{
return $this->name." ".$this->surname;
}
public function getCanEditOffice($officeId){
foreach($this->offices as $jt){
if($jt->getOffice()->getId() == $officeId){
$jtou = $jt;
break;
}
}
if($this->getRole() == "ROLE_ADMIN"){
return true;
}
elseif($jtou->getUser()->getRole() == "ROLE_USER"){
if($this->isAdminActive && $jtou->getOffice() != null && $jtou->getOffice()->getIsActive())
return true;
}
elseif($this->getRole() == "ROLE_RESELLER"){
if($this->isAdminActive && $this->reseller != null && $this->reseller->getIsAdminActive() && $jtou->getOffice()->getIsActive() && $jtou->getOffice()->getAllowResellerAccess())
return true;
}
return false;
}
public function getCanConfigureOffice($officeId){
foreach($this->offices as $jt){
if($jt->getOffice()->getId() == $officeId){
$jtou = $jt;
break;
}
}
if($this->getRole() == "ROLE_ADMIN"){
return true;
}
elseif($jtou->getRole()->getSlug() == "responsible"){
if($this->isAdminActive && $jtou->getOffice() != null && $jtou->getOffice()->getIsActive())
return true;
}
elseif($jtou->getRole()->getSlug() == "on_charge" || $jtou->getRole()->getSlug() == "delegate"){
if($this->isAdminActive && $jtou->getOffice() != null && $jtou->getOffice()->getIsActive() && $jtou->getOffice()->getIsConfigurationCompleted())
return true;
}
elseif($this->getRole() == "ROLE_RESELLER"){
if($this->isAdminActive && $this->reseller != null && $this->reseller->getIsAdminActive() && $jtou->getOffice() != null && $jtou->getOffice()->getIsActive() && $jtou->getOffice()->getAllowResellerAccess())
return true;
}
return false;
}
public function getJtRole($officeId){
foreach($this->offices as $jt){
if($jt->getOffice()->getId() == $officeId){
$jtou = $jt;
break;
}
}
return $jtou->getRole()->getValue();
}
/**
* @ORM\Column(name="id", type="bigint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="position", type="string", nullable=true)
*/
protected $position;
/**
* @ORM\Column(name="email", type="string", unique=true)
*/
protected $email;
/**
* @ORM\Column(name="name", type="string")
*/
protected $name;
/**
* @ORM\Column(name="surname", type="string")
*/
protected $surname;
/**
* @ORM\Column(name="password", type="string", nullable=true)
*/
protected $password;
/**
* @ORM\Column(name="role", type="string")
*/
protected $role = "ROLE_USER";
/**
* @ORM\Column(name="one_time_code", type="string", nullable=true)
*/
protected $oneTimeCode;
/**
* @ORM\Column(name="expiration_one_time_code", type="datetime", nullable=true)
*/
protected $expirationOneTimeCode;
/**
* @ORM\Column(name="is_tutorial_active", type="boolean")
*/
protected $isTutorialActive = true;
/**
* @ORM\Column(name="is_admin_active", type="boolean")
*/
protected $isAdminActive = true;
// ONE TO MANY
/**
* @ORM\OneToMany(targetEntity="App\Entity\Master\Notification", mappedBy="user")
*/
private $notifications;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Master\JoinTableOfficeUser", mappedBy="user")
*/
private $offices;
//
// MANY TO ONE
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Master\Reseller", inversedBy="users")
* @ORM\JoinColumn(name="reseller_id", referencedColumnName="id", nullable=true)
*/
private $reseller;
public function __construct()
{
$this->notifications = new ArrayCollection();
$this->offices = new ArrayCollection();
}
//
public function getUserIdentifier(): string
{
return (string) $this->email;
}
public function getRoles(): array
{
switch($this->role){
case 'ROLE_USER': return array('ROLE_USER');
case 'ROLE_RESELLER': return array('ROLE_RESELLER');
case 'ROLE_ADMIN': return array('ROLE_ADMIN');
};
}
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getSalt(): ?string
{
return null;
}
public function eraseCredentials() {
return;
}
public function getUsername(): string
{
return $this->email;
}
public function getId(): ?string
{
return $this->id;
}
public function getPosition(): ?string
{
return $this->position;
}
public function setPosition(?string $position): self
{
$this->position = $position;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getRole(): ?string
{
return $this->role;
}
public function setRole(string $role): self
{
$this->role = $role;
return $this;
}
public function getOneTimeCode(): ?string
{
return $this->oneTimeCode;
}
public function setOneTimeCode(?string $oneTimeCode): self
{
$this->oneTimeCode = $oneTimeCode;
return $this;
}
public function getExpirationOneTimeCode(): ?\DateTimeInterface
{
return $this->expirationOneTimeCode;
}
public function setExpirationOneTimeCode(?\DateTimeInterface $expirationOneTimeCode): self
{
$this->expirationOneTimeCode = $expirationOneTimeCode;
return $this;
}
public function isIsTutorialActive(): ?bool
{
return $this->isTutorialActive;
}
public function setIsTutorialActive(bool $isTutorialActive): self
{
$this->isTutorialActive = $isTutorialActive;
return $this;
}
public function isIsAdminActive(): ?bool
{
return $this->isAdminActive;
}
public function setIsAdminActive(bool $isAdminActive): self
{
$this->isAdminActive = $isAdminActive;
return $this;
}
/**
* @return Collection<int, Notification>
*/
public function getNotifications(): Collection
{
return $this->notifications;
}
public function addNotification(Notification $notification): self
{
if (!$this->notifications->contains($notification)) {
$this->notifications[] = $notification;
$notification->setUser($this);
}
return $this;
}
public function removeNotification(Notification $notification): self
{
if ($this->notifications->removeElement($notification)) {
// set the owning side to null (unless already changed)
if ($notification->getUser() === $this) {
$notification->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, JoinTableOfficeUser>
*/
public function getOffices(): Collection
{
return $this->offices;
}
public function addOffice(JoinTableOfficeUser $office): self
{
if (!$this->offices->contains($office)) {
$this->offices[] = $office;
$office->setUser($this);
}
return $this;
}
public function removeOffice(JoinTableOfficeUser $office): self
{
if ($this->offices->removeElement($office)) {
// set the owning side to null (unless already changed)
if ($office->getUser() === $this) {
$office->setUser(null);
}
}
return $this;
}
public function getReseller(): ?Reseller
{
return $this->reseller;
}
public function setReseller(?Reseller $reseller): self
{
$this->reseller = $reseller;
return $this;
}
}