<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity("email")
*/
class User implements UserInterface, \Serializable
{
public const ROLE_ADMIN = 'ROLE_ADMIN';
public const ROLE_RH = 'ROLE_RH';
public const ROLE_USER_ACTIF = 'ROLE_USER_ACTIF';
public const ROLE_USER_INACTIF = 'ROLE_USER_INACTIF';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Assert\Length(max=50,min=6,minMessage="Le mot de passe doit faire minimum {{ limit }} caractères.")
*/
private $plainPassword;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
* @Assert\Email
*/
private $email;
/**
* @ORM\ManyToOne(targetEntity="Company", inversedBy="users", fetch="EAGER")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id")
*/
private $company;
/**
* @ORM\ManyToOne(targetEntity="Role", fetch="EAGER")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
*/
private $roles;
/**
* @ORM\Column(type="string", length=90, nullable=true)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=90, nullable=true)
*/
private $lastname;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $birth;
/**
* @ORM\Column(type="string", length=2, nullable=true)
*/
private $gender;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $weight = null;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $height = null;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $punctual_sport;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $work_handicapped = null;
/**
* @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
*/
private $updatedAt;
/**
* @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
*/
private $updatedDataConfirmedAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserActivity", mappedBy="user", cascade={"persist","remove"})
*/
private $userActivities;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Department")
*/
private $department;
/**
* @ORM\OneToMany(targetEntity=FormationRegistration::class, mappedBy="User",cascade={"remove"})
*/
private $formations;
/**
* @ORM\OneToMany(targetEntity=UserFavorites::class, mappedBy="user",cascade={"remove"})
*/
private $favorites;
/**
* @ORM\OneToMany(targetEntity=Notification::class, mappedBy="userTo",cascade={"remove"})
*/
private $notifications;
/**
* @ORM\Column(type="boolean", options={"default": "0"} )
*/
private $intro = false;
/**
* @ORM\Column(type="boolean", options={"default": "0"} )
*/
private $cgu = false;
/**
* @ORM\OneToMany(targetEntity=Log::class, mappedBy="user", cascade={"remove"})
*/
private $logs;
/**
* @ORM\ManyToOne(targetEntity=Job::class, inversedBy="user")
*/
private $job;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isContact;
/**
* @ORM\Column(type="datetime", nullable=true, options={"default": null})
*/
private $lastLoginAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $keycloakId;
/**
* @ORM\ManyToOne(targetEntity=Site::class, inversedBy="user")
*/
private $site;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $agreeTermsAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $agreePrivateDataAt;
public function __construct()
{
$this->userActivities = new ArrayCollection();
$this->formations = new ArrayCollection();
$this->notifications = new ArrayCollection();
$this->logs = new ArrayCollection();
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* @param mixed $firstname
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
/**
* @return mixed
*/
public function getLastname()
{
return $this->lastname;
}
/**
* @param mixed $lastname
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
/**
* @return mixed
*/
public function getBirth()
{
return $this->birth;
}
/**
* @param mixed $birth
*/
public function setBirth($birth)
{
$this->birth = $birth;
}
/**
* @return mixed
*/
public function getGender()
{
return $this->gender;
}
/**
* @param mixed $gender
*/
public function setGender($gender)
{
$this->gender = $gender;
}
/**
* @return mixed
*/
public function getWeight()
{
return $this->weight;
}
/**
* @param mixed $weight
*/
public function setWeight($weight)
{
$this->weight = $weight;
}
/**
* @return mixed
*/
public function getHeight()
{
return $this->height;
}
/**
* @param mixed $height
*/
public function setHeight($height)
{
$this->height = $height;
}
/**
* @return mixed
*/
public function getCompany()
{
return $this->company;
}
/**
* @param mixed $company
*/
public function setCompany($company)
{
$this->company = $company;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* Returns the roles granted to the user.
*
* <code>
* public function getRoles()
* {
* return array('ROLE_USER_ACTIF');
* }
* </code>
*
* Alternatively, the roles might be stored on a ``roles`` property,
* and populated in any number of different ways when the user object
* is created.
*
* @return (Role|string)[] The user roles
*/
public function getRoles()
{
return array($this->roles->getName());
}
public static function getStandardRoleNames(): array
{
return [
self::ROLE_USER_ACTIF,
self::ROLE_USER_INACTIF,
];
}
public static function getAssignableRoleNames(): array
{
return array_merge(self::getStandardRoleNames(), [self::ROLE_RH]);
}
public static function getStandardAndManagerRoleNames(): array
{
return array_merge(self::getStandardRoleNames(), [self::ROLE_RH]);
}
public function isStandardUser(): bool
{
return in_array($this->getRoles()[0], self::getStandardRoleNames(), true);
}
public function getRoleLabel(): string
{
switch ($this->getRoles()[0]) {
case self::ROLE_USER_ACTIF:
return 'Utilisateur actif';
case self::ROLE_USER_INACTIF:
return 'Utilisateur non actif';
case self::ROLE_RH:
return 'Gestionnaire';
case self::ROLE_ADMIN:
return 'Administrateur';
default:
return $this->getRoles()[0];
}
}
/**
* @param string|Role $role
* @return bool
*/
public function hasRole($role)
{
$roles = $this->getRoles();
foreach ($roles as $_role) {
if($_role == (is_string($role) ? $role : $role->getName()))
return true;
}
return false;
}
/**
* @return Collection|UserActivity[]
*/
public function getUserActivities(): Collection
{
return $this->userActivities;
}
public function addUserActivity(UserActivity $userActivity): self
{
if (!$this->userActivities->contains($userActivity)) {
$this->userActivities[] = $userActivity;
$userActivity->setUser($this);
}
return $this;
}
public function removeUserActivity(UserActivity $userActivity): self
{
if ($this->userActivities->contains($userActivity)) {
$this->userActivities->removeElement($userActivity);
// set the owning side to null (unless already changed)
if ($userActivity->getUser() === $this) {
$userActivity->setUser(null);
}
}
return $this;
}
/**
* @param mixed $roles
*/
public function setRoles(Role $roles)
{
$this->roles = $roles;
}
/**
* Returns the password used to authenticate the user.
*
* This should be the encoded password. On authentication, a plain-text
* password will be salted, encoded, and then compared to this value.
*
* @return string The password
*/
public function getPassword()
{
return $this->password;
}
/**
* Returns the salt that was originally used to encode the password.
*
* This can return null if the password was not encoded using a salt.
*
* @return string|null The salt
*/
public function getSalt()
{
return null;
}
/**
* Returns the username used to authenticate the user.
*
* @return string The username
*/
public function getUsername()
{
return $this->getEmail();
}
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @param mixed $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return mixed
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @param mixed $createdAt
*/
public function setCreatedAt($createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* @return mixed
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* @param mixed $updatedAt
*/
public function setUpdatedAt($updatedAt): void
{
$this->updatedAt = $updatedAt;
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
/**
* String representation of object
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
* @since 5.1.0
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->email,
$this->password,
$this->roles,
// see section on salt below
// $this->salt,
));
}
/**
* Constructs the object
* @link http://php.net/manual/en/serializable.unserialize.php
* @param string $serialized <p>
* The string representation of the object.
* </p>
* @return void
* @since 5.1.0
*/
public function unserialize($serialized)
{
list(
$this->id,
$this->email,
$this->password,
$this->roles,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
/** @ORM\PrePersist */
public function onPrePersist()
{
$this->createdAt = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
$this->updatedAt = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
}
/** @ORM\PreUpdate */
public function onPreUpdate()
{
$this->updatedAt = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
}
public function getDepartment(): ?Department
{
return $this->department;
}
public function setDepartment(?Department $department): self
{
$this->department = $department;
return $this;
}
/**
* @return mixed
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* @param mixed $plainPassword
*/
public function setPlainPassword($plainPassword): void
{
$this->plainPassword = $plainPassword;
}
/**
* @return Collection|FormationRegistration[]
*/
public function getFormations(): Collection
{
return $this->formations;
}
public function addFormation(FormationRegistration $formation): self
{
if (!$this->formations->contains($formation)) {
$this->formations[] = $formation;
$formation->setUser($this);
}
return $this;
}
public function removeFormation(FormationRegistration $formation): self
{
if ($this->formations->removeElement($formation)) {
// set the owning side to null (unless already changed)
if ($formation->getUser() === $this) {
$formation->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Notification[]
*/
public function getNotifications(): Collection
{
return $this->notifications;
}
public function addNotification(Notification $notification): self
{
if (!$this->notifications->contains($notification)) {
$this->notifications[] = $notification;
$notification->setUserTo($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->getUserTo() === $this) {
$notification->setUserTo(null);
}
}
return $this;
}
/**
* @return bool
*/
public function isIntro(): bool
{
return $this->intro;
}
/**
* @param bool $intro
*/
public function setIntro(bool $intro): void
{
$this->intro = $intro;
}
/**
* @return bool
*/
public function getCgu(): bool
{
return $this->cgu;
}
/**
* @param bool $cgu
*/
public function setCgu(bool $cgu): void
{
$this->cgu = $cgu;
}
/**
* @return null
*/
public function getWorkHandicapped()
{
return $this->work_handicapped;
}
/**
* @param null $work_handicapped
*/
public function setWorkHandicapped($work_handicapped): void
{
$this->work_handicapped = $work_handicapped;
}
/**
* @return
*/
public function getPunctualSport()
{
return $this->punctual_sport;
}
/**
* @param $punctual_sport
*/
public function setPunctualSport($punctual_sport): void
{
$this->punctual_sport = $punctual_sport;
}
/**
* @return mixed
*/
public function getUpdatedDataConfirmedAt()
{
return $this->updatedDataConfirmedAt;
}
/**
* @param mixed $updatedDataConfirmedAt
*/
public function setUpdatedDataConfirmedAt($updatedDataConfirmedAt): void
{
$this->updatedDataConfirmedAt = $updatedDataConfirmedAt;
}
public function getJob(): ?Job
{
return $this->job;
}
public function setJob(?Job $job): self
{
$this->job = $job;
return $this;
}
public function getIsContact(): ?bool
{
return $this->isContact;
}
public function setIsContact(?bool $isContact): self
{
$this->isContact = $isContact;
return $this;
}
/**
* @return mixed
*/
public function getLastLoginAt()
{
return $this->lastLoginAt;
}
/**
* @param mixed $lastLoginAt
*/
public function setLastLoginAt($lastLoginAt): void
{
$this->lastLoginAt = $lastLoginAt;
}
// /**
// * @ORM\PrePersist
// * @ORM\PreUpdate
// */
// public function updatedTimestamps(): void
// {
// $this->setUpdatedDataConfirmedAt(new \DateTime());
// }
/**
* @return mixed
*/
public function getKeycloakId()
{
return $this->keycloakId;
}
/**
* @param mixed $keycloakId
*/
public function setKeycloakId($keycloakId): void
{
$this->keycloakId = $keycloakId;
}
public function getSite(): ?Site
{
return $this->site;
}
public function setSite(?Site $site): self
{
$this->site = $site;
return $this;
}
/**
* @return mixed
*/
public function getAgreeTermsAt()
{
return $this->agreeTermsAt;
}
/**
* @param mixed $agreeTermsAt
*/
public function setAgreeTermsAt($agreeTermsAt): void
{
$this->agreeTermsAt = $agreeTermsAt;
}
/**
* @return mixed
*/
public function getAgreePrivateDataAt()
{
return $this->agreePrivateDataAt;
}
/**
* @param mixed $agreePrivateDataAt
*/
public function setAgreePrivateDataAt($agreePrivateDataAt): void
{
$this->agreePrivateDataAt = $agreePrivateDataAt;
}
}