<?php
namespace App\Entity\User;
use App\Entity\Federation\Race;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Entity\Federation\Participation;
use function Symfony\Component\Translation\t;
/**
* User
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="App\Repository\User\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @Gedmo\Loggable(logEntryClass="App\Entity\History\History")
* @UniqueEntity(fields="email", message="inodia_user.email.unique", groups={"RegistrationUserInodia", "UpdateUserInodia"})
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*/
protected $plainPassword;
/**
* @var string
*
* @Gedmo\Versioned
* @ORM\Column(name="password", type="string")
* @Assert\NotBlank(groups={"RegistrationUserInodia"}, message="inodia_user.password.not_blank")
* @Assert\Regex(
* groups={"RegistrationUserInodia", "ChangePassword"},
* pattern="/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/",
* match=true,
* message="inodia_user.password.validation"
* )
*/
private $password;
/**
* @ORM\Column(type="json")
*/
protected $roles = array();
/**
* @var string
*
* @Gedmo\Versioned
* @ORM\Column(name="firstname", type="string", length=255, nullable=false)
* @Assert\NotBlank(groups={"RegistrationUserInodia", "UpdateUserInodia"})
*/
private $firstname;
/**
* @var string
*
* @Gedmo\Versioned
* @ORM\Column(name="lastname", type="string", length=255, nullable=false)
* @Assert\NotBlank(groups={"RegistrationUserInodia", "UpdateUserInodia"})
*/
private $lastname;
/**
* @Gedmo\Versioned
* @ORM\ManyToOne(targetEntity="App\Entity\User\Prefix")
*/
private $prefix;
/**
* @var string
*
* @Gedmo\Versioned
* @ORM\Column(name="job", type="string", length=255, nullable=true)
*/
private $job;
/**
* @var string
*
* @Gedmo\Versioned
* @ORM\Column(name="language", type="string", length=255, nullable=false)
* @Assert\NotBlank(groups={"RegistrationUserInodia", "UpdateUserInodia"})
*/
private $language;
/**
* @Gedmo\Versioned
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\Email(groups={"RegistrationUserInodia", "UpdateUserInodia"}, message="inodia_user.email.validation")
* @Assert\NotBlank(groups={"RegistrationUserInodia", "UpdateUserInodia"}, message="inodia_user.email.not_blank")
*/
protected $email;
/**
* @Gedmo\Versioned
* @ORM\Column(name="mobile_phone", type="string", nullable=true)
* @Assert\Expression(
* "this.getMobilePhone() != '' or this.getPhone() != ''",
* message="inodia_user.phone.one_required",
* groups={"RegistrationUserInodia", "UpdateUserInodia"}
* )
*/
private $mobilePhone;
/**
* @Gedmo\Versioned
* @ORM\Column(name="phone", type="string", nullable=true)
*/
private $phone;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="change", field={"password"})
* @ORM\Column(name="password_change_date", type="datetime", nullable=true)
*/
private $passwordChangeDate;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User\Group")
* @ORM\JoinTable(name="users_groups",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
/**
*
* @var \App\Entity\ThirdParty\ThirdParty
*
* @ORM\ManyToOne(targetEntity="App\Entity\ThirdParty\ThirdParty", inversedBy="users")
*/
protected $thirdParty;
protected $confirmation_token;
/**
* @var \DateTime
*
* @ORM\Column(name="last_login", type="datetime", nullable=true)
*/
private $lastLogin;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(name="created_date", type="datetime", nullable=false)
*/
private $createdDate;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(name="updated_date", type="datetime", nullable=true)
*/
private $updatedDate;
/**
* @var integer
*
* @ORM\Column(name="enabled", type="integer", nullable=false)
*/
private $enabled = 1;
/**
* @var integer
*
* @ORM\Column(name="status", type="integer", nullable=false)
*/
private $status = 1;
private $superAdmin = false;
/**
* @ORM\Column(type="boolean")
* @Assert\IsTrue(message="inodia_user.dataPrivacyPolicyAcceptation", groups={"RegistrationUserInodia"})
*/
private $dataPrivacyPolicyAcceptation = 0;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Federation\Participation", mappedBy="referents", cascade={"remove"})
*/
private $participations;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Site\AccessSite", mappedBy="user", cascade={"remove"})
*/
private $accessSites;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Media\Document", mappedBy="proofValidationUser")
*/
private $proofValidations;
/**
* @ORM\OneToMany(targetEntity="App\Entity\History\History", mappedBy="user", cascade={"remove"})
*/
private $histories;
public function __construct()
{
$this->groups = new ArrayCollection();
$this->participations = new ArrayCollection();
$this->proofValidations = new ArrayCollection();
$this->histories = new ArrayCollection();
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* The public representation of the user (e.g. a username, an email address, etc.)
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
if(!empty($this->getGroups()))
foreach($this->getGroups() as $group)
foreach($group->getRoles() as $role)
$roles[] = $role;
//$roles = $this->roles;
// guarantee every user at least has ROLE_USER
//$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Get firstname
*
* @return string
*/
public function getUsername()
{
return $this->email;
}
/**
* Set firstname
*
* @param string $firstname
*
* @return User
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
*
* @return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set prefix
*
* @param \App\Entity\User\Prefix $prefix
*
* @return User
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
return $this;
}
/**
* Get prefix
*
* @return \App\Entity\User\Prefix $prefix
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* Set job
*
* @param string $job
*
* @return User
*/
public function setJob($job)
{
$this->job = $job;
return $this;
}
/**
* Get job
*
* @return string
*/
public function getJob()
{
return $this->job;
}
/**
* Set language
*
* @param string $language
*
* @return User
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* Set mobilePhone
*
* @param string $mobilephone
*
* @return User
*/
public function setMobilePhone($mobilephone)
{
$this->mobilePhone = $mobilephone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set phone
*
* @param string $phone
*
* @return User
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get mobilePhone
*
* @return string
*/
public function getMobilePhone()
{
return $this->mobilePhone;
}
/**
* Get lastLogin
*
* @return \DateTime
*/
public function getLastLogin()
{
return $this->lastLogin;
}
/**
* Set phone
*
* @param \DateTime $lastLogin
*
* @return User
*/
public function setLastLogin($lastLogin)
{
$this->lastLogin = $lastLogin;
return $this;
}
/**
* Get createdDate
*
* @return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Get updatedDate
*
* @return \DateTime
*/
public function getUpdatedDate()
{
return $this->updatedDate;
}
/**
* Set enabled
*
* @param integer $enabled
*
* @return User
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
return $this;
}
/**
* Get enabled
*
* @return integer
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* Set status
*
* @param integer $status
*
* @return User
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set passwordChangeDate
*
* @param \DateTime $passwordChangeDate
*
* @return User
*/
public function setPasswordChangeDate($passwordChangeDate)
{
$this->passwordChangeDate = $passwordChangeDate;
return $this;
}
/**
* Get passwordChangeDate
*
* @return \DateTime
*/
public function getPasswordChangeDate()
{
return $this->passwordChangeDate;
}
/**
* Set thirdparty
*
* @param \App\Entity\ThirdParty\ThirdParty $thirdparty
*
* @return \App\Entity\ThirdParty\ThirdParty
*/
public function setThirdParty(\App\Entity\ThirdParty\ThirdParty $thirdParty = null)
{
$this->thirdParty = $thirdParty;
return $this;
}
/**
* Get thirdparty
*
* @return \App\Entity\ThirdParty\ThirdParty
*/
public function getThirdParty()
{
return $this->thirdParty;
}
public function getGroups()
{
return $this->groups;
}
public function addGroup(Group $group)
{
if(!$this->groups->contains($group))
$this->groups->add($group);
}
public function belongsToGroup($role)
{
$groups=$this->getGroups();
foreach ($groups as $group)
{
if($group->hasRole($role))
return true;
}
return false;
}
public function getDataPrivacyPolicyAcceptation(): ?bool
{
return $this->dataPrivacyPolicyAcceptation;
}
public function setDataPrivacyPolicyAcceptation(bool $dataPrivacyPolicyAcceptation): self
{
$this->dataPrivacyPolicyAcceptation = $dataPrivacyPolicyAcceptation;
return $this;
}
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* @return Collection|Participation[]
*/
public function getParticipations(): ?Collection
{
return $this->participations;
}
public function addParticipation(Participation $participation): self
{
if (!$this->participations->contains($participation)) {
$this->participations[] = $participation;
$participation->addTeam($this);
}
return $this;
}
public function removeParticipation(Participation $participation): self
{
if ($this->participations->contains($participation)) {
$this->participations->removeElement($participation);
$participation->removeTeam($this);
}
return $this;
}
public function getAccessSites()
{
return $this->accessSites;
}
public function checkAccessRace(Race $race): bool
{
foreach($this->getAccessSites() as $accessSite)
if($accessSite->getSite()->getRace()->getId() == $race->getId())
return true;
return false;
}
public function getAllowedRace()
{
$races = [];
foreach($this->getAllowedSite() as $site)
$races[] = $site->getRace();
return $races;
}
public function getAllowedSite()
{
$sites = [];
foreach($this->getAccessSites() as $accessSite)
if($accessSite->getStatus() == 2)
$sites[] = $accessSite->getSite();
return $sites;
}
public function getProofValidations()
{
return $this->proofValidations;
}
}