<?php
namespace App\Entity;
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\Table(name="fs_user")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks()
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public function __toString()
{
return $this->name." ".$this->surname;
}
public function canConfigureAgency($agencyId){
foreach($this->agencies as $jt){
if($jt->getAgency()->getId() == $agencyId){
$jtou = $jt;
break;
}
}
if($this->getRole() == "ROLE_ADMIN"){
return true;
}
elseif($jtou->getRole()->getSlug() == "responsible"){
if($this->adminActive && $jtou->getAgency() != null && $jtou->getAgency()->getIsActive())
return true;
}
elseif($jtou->getRole()->getSlug() == "on_charge" || $jtou->getRole()->getSlug() == "delegate"){
if($this->adminActive && $jtou->getAgency() != null && $jtou->getAgency()->getIsActive() && $jtou->getAgency()->getIsConfigurationCompleted())
return true;
}
return false;
}
public function canEditAgency($agencyId)
{
if($this->getRole() == "ROLE_ADMIN")
return true;
$jtou = null;
foreach($this->agencies as $jt){
if($jt->getAgency()->getId() == $agencyId){
$jtou = $jt;
break;
}
}
if($jtou != null && $jtou->getUser()->getRole() == "ROLE_USER"){
if($this->adminActive && $jtou->getAgency() != null && $jtou->getAgency()->getIsActive())
return true;
}
return false;
}
public function displayName()
{
return $this->name." ".$this->surname;
}
/**
* @ORM\Column(name="id", type="bigint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @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="active", type="boolean")
*/
protected $active = true;
/**
* @ORM\Column(name="admin_active", type="boolean")
*/
protected $adminActive = true;
/**
* @ORM\Column(name="directory_path", type="string", length=191, nullable=true)
*/
protected $directoryPath;
// ONE TO ONE
/**
* @ORM\OneToOne(targetEntity="App\Entity\UserDetail", mappedBy="user")
*/
private $userDetail;
//
// ONE TO MANY
/**
* @ORM\OneToMany(targetEntity="App\Entity\JoinTableAgencyUser", mappedBy="user")
*/
private $agencies;
/**
* @ORM\OneToMany(targetEntity="App\Entity\JoinTableDealerUser", mappedBy="user")
*/
private $dealers;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Reservation", mappedBy="user")
*/
private $reservations;
//
public function __construct()
{
$this->agencies = new ArrayCollection();
$this->reservations = new ArrayCollection();
$this->dealers = 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_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 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 isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function isAdminActive(): ?bool
{
return $this->adminActive;
}
public function setAdminActive(bool $adminActive): self
{
$this->adminActive = $adminActive;
return $this;
}
public function getDirectoryPath(): ?string
{
return $this->directoryPath;
}
public function setDirectoryPath(?string $directoryPath): self
{
$this->directoryPath = $directoryPath;
return $this;
}
public function getUserDetail(): ?UserDetail
{
return $this->userDetail;
}
public function setUserDetail(?UserDetail $userDetail): self
{
// unset the owning side of the relation if necessary
if ($userDetail === null && $this->userDetail !== null) {
$this->userDetail->setUser(null);
}
// set the owning side of the relation if necessary
if ($userDetail !== null && $userDetail->getUser() !== $this) {
$userDetail->setUser($this);
}
$this->userDetail = $userDetail;
return $this;
}
/**
* @return Collection<int, JoinTableAgencyUser>
*/
public function getAgencies(): Collection
{
return $this->agencies;
}
public function addAgency(JoinTableAgencyUser $agency): self
{
if (!$this->agencies->contains($agency)) {
$this->agencies->add($agency);
$agency->setUser($this);
}
return $this;
}
public function removeAgency(JoinTableAgencyUser $agency): self
{
if ($this->agencies->removeElement($agency)) {
// set the owning side to null (unless already changed)
if ($agency->getUser() === $this) {
$agency->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Reservation>
*/
public function getReservations(): Collection
{
return $this->reservations;
}
public function addReservation(Reservation $reservation): self
{
if (!$this->reservations->contains($reservation)) {
$this->reservations->add($reservation);
$reservation->setUser($this);
}
return $this;
}
public function removeReservation(Reservation $reservation): self
{
if ($this->reservations->removeElement($reservation)) {
// set the owning side to null (unless already changed)
if ($reservation->getUser() === $this) {
$reservation->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, JoinTableDealerUser>
*/
public function getDealers(): Collection
{
return $this->dealers;
}
public function addDealer(JoinTableDealerUser $dealer): self
{
if (!$this->dealers->contains($dealer)) {
$this->dealers->add($dealer);
$dealer->setUser($this);
}
return $this;
}
public function removeDealer(JoinTableDealerUser $dealer): self
{
if ($this->dealers->removeElement($dealer)) {
// set the owning side to null (unless already changed)
if ($dealer->getUser() === $this) {
$dealer->setUser(null);
}
}
return $this;
}
}