src/Entity/User.php line 24

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\EquatableInterface;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  13. use App\Entity\UserPenalty;
  14. use App\Entity\Centers;
  15. #[ORM\Entity(repositoryClassUserRepository::class)]
  16. #[UniqueEntity(fields: ['username'])]
  17. #[UniqueEntity(fields: ['email'])]
  18. #[UniqueEntity(fields: ['dni'])]
  19. class User implements UserInterfaceEquatableInterfacePasswordAuthenticatedUserInterface
  20. {
  21.     #[ORM\Id]
  22.     #[ORM\GeneratedValue]
  23.     #[ORM\Column]
  24.     private ?int $id null;
  25.     #[ORM\Column(length180uniquetrue)]
  26.     #[Assert\NotBlank(message'El nombre de usuario es obligatorio')]
  27.     private ?string $username null;
  28.     #[ORM\Column(type'json')]
  29.     private array $roles = [];
  30.     #[ORM\Column(length50)]
  31.     #[Assert\NotBlank(message'El nombre es obligatorio')]
  32.     private ?string $nomCompletnull;
  33.     #[ORM\Column(length10uniquetrue)]
  34.     #[Assert\NotBlank(message'El DNI/NIE es obligarorio')]
  35.     private ?string $dninull;
  36.     #[ORM\Column(length250,nullabletrue)]
  37.     private ?string $addressnull;
  38.     #[ORM\Column(length50,nullabletrue)]
  39.     private ?string $phonenull;
  40.     #[ORM\Column(length50,nullabletrue)]
  41.     private ?string $notesnull;
  42.     #[ORM\Columnlength100uniquetrue)]
  43.     #[Assert\NotBlank(message'El campo correo no puede estar vacío')]
  44.     #[Assert\Email(message'Email incorrecto')]
  45.     private ?string $emailnull;
  46.     #[ORM\Column]
  47.     private ?bool $valid null;
  48.     #[ORM\Column]
  49.     private ?bool $deleted null;
  50.     #[ORM\Columnlength255)]
  51.     private ?string $password null;
  52.     #[ORM\Column(type'boolean')]
  53.     private ?bool $admin null;
  54.     #[ORM\OneToMany(mappedBy'userId'targetEntityRegistrations::class, orphanRemovaltrue)]
  55.     private Collection $registrations;
  56.     #[ORM\OneToMany(mappedBy'creatorUserId'targetEntityAttendance::class)]
  57.     private Collection $attendances;
  58.     #[ORM\ManyToMany(targetEntityEvents::class, mappedBy'courseUser')]
  59.     private Collection $events;
  60.     #[ORM\ManyToMany(targetEntityEvents::class, mappedBy'managerUsers')]
  61.     private Collection $managedEvents;
  62.     #[ORM\Column(length255nullabletrue)]
  63.     private ?string $surname null;
  64.     #[ORM\Column(length12nullabletrue)]
  65.     private ?string $customFilterYear null;
  66.     #[ORM\Column(type'string'length64nullabletrue)]
  67.     private ?string $activationToken null;
  68.     #[ORM\Column(type'boolean')]
  69.     private bool $active false;
  70.     #[ORM\ManyToOne(inversedBy'managementUsers')]
  71.     #[ORM\JoinColumn(nullabletrueonDelete'SET NULL')]
  72.     private ?Centers $managementCenter null;
  73.     #[ORM\Column(type'boolean'options: ['default' => false])]
  74.     private bool $canManageEventRequestsAndRegistrations false;
  75.     public function __construct()
  76.     {
  77.        
  78.         $this->registrations = new ArrayCollection();
  79.         $this->attendances = new ArrayCollection();
  80.         $this->events = new ArrayCollection();
  81.         $this->managedEvents = new ArrayCollection();
  82.         $this->eventRequests = new ArrayCollection();
  83.         $this->penalties = new ArrayCollection();
  84.     }
  85.     public function getId(): ?int
  86.     {
  87.         return $this->id;
  88.     }
  89.     /**
  90.      * A visual identifier that represents this user.
  91.      *
  92.      * @see UserInterface
  93.      */
  94.     public function getUsername(): string
  95.     {
  96.         return (string) $this->username;
  97.     }
  98.     public function getUserIdentifier(): string
  99.     {
  100.         return (string) $this->username;
  101.     }
  102.     public function setUsername($username): self
  103.     {
  104.         $this->username $username;
  105.         return $this;
  106.     }
  107.     /**
  108.      * @see UserInterface
  109.      */
  110.     public function getRoles(): array
  111.     {
  112.         return array_values(array_unique($this->roles));
  113.     }
  114.     public function setRoles(array $roles): self
  115.     {
  116.         $this->roles array_values(array_unique($roles));
  117.         return $this;
  118.     }
  119.     /**
  120.      * @see UserInterface
  121.      */
  122.     public function getPassword(): ?string
  123.     {
  124.         return $this->password;
  125.     }
  126.     /**
  127.      * @see UserInterface
  128.      */
  129.     public function eraseCredentials()
  130.     {
  131.         // If you store any temporary, sensitive data on the user, clear it here
  132.         // $this->plainPassword = null;
  133.     }
  134.     public function getNomComplet(): ?string
  135.     {
  136.         return $this->nomComplet;
  137.     }
  138.     public function setNomComplet($nomComplet): self
  139.     {
  140.         $this->nomComplet $nomComplet;
  141.         return $this;
  142.     }
  143.     public function getEmail(): ?string
  144.     {
  145.         return $this->email;
  146.     }
  147.     public function setEmail($email): self
  148.     {
  149.         $this->email $email;
  150.         return $this;
  151.     }
  152.     public function getPhone(): ?string
  153.     {
  154.         return $this->phone;
  155.     }
  156.     public function setPhone($phone): self
  157.     {
  158.         $this->phone $phone;
  159.         return $this;
  160.     }
  161.     public function getNotes(): ?string
  162.     {
  163.         return $this->notes;
  164.     }
  165.     public function setNotes($notes): self
  166.     {
  167.         $this->notes $notes;
  168.         
  169.         return $this;
  170.     }
  171.     public function getAddress(): ?string
  172.     {
  173.         return $this->address;
  174.     }
  175.     public function setAddress($address): self
  176.     {
  177.         $this->address $address;
  178.         
  179.         return $this;
  180.     }
  181.     public function getDni(): ?string
  182.     {
  183.         return $this->dni;
  184.     }
  185.     public function setDni($dni): self
  186.     {
  187.         $this->dni $dni;
  188.         return $this;
  189.     }
  190.     public function isValid(): ?bool
  191.     {
  192.         return $this->valid;
  193.     }
  194.     public function setValid(bool $valid): self
  195.     {
  196.         $this->valid $valid;
  197.         return $this;
  198.     }
  199.     public function isDeleted(): ?bool
  200.     {
  201.         return $this->deleted;
  202.     }
  203.     public function setDeleted(bool $deleted): self
  204.     {
  205.         $this->deleted $deleted;
  206.         return $this;
  207.     }
  208.     public function setPassword($password): self
  209.     {
  210.         $this->password $password;
  211.         return $this;
  212.     }
  213.     public function getAvatarUrl(): string
  214.     {
  215.         return "/avatar/".urlencode((string)$this->nomComplet);
  216.     }
  217.     public function getColorCode(): string
  218.     {
  219.         $code dechex(crc32($this->getUsername()));
  220.         $code substr($code06);
  221.         return '#'.$code;
  222.     }
  223.     #[Assert\Callback]
  224.     public function validate(ExecutionContextInterface $context$payload)
  225.     {
  226.         /*if (strlen($this->password)< 3){
  227.             $context->buildViolation('Mot de passe trop court')
  228.                 ->atPath('justpassword')
  229.                 ->addViolation();
  230.         }*/
  231.     }
  232.     public function __toString(): string
  233.     {
  234.         return "$this->nomComplet ($this->id)";
  235.     }
  236.     public function isAdmin(): ?bool
  237.     {
  238.         return $this->admin;
  239.     }
  240.     public function setAdmin(bool $admin): self
  241.     {
  242.         $this->admin $admin;
  243.         return $this;
  244.     }
  245.     public function isEqualTo(UserInterface $user): bool
  246.     {
  247.         if ($user instanceof User) {
  248.             return $this->isValid() && !$this->isDeleted() && $this->getPassword() == $user->getPassword() && $this->getUsername() == $user->getUsername()
  249.                 && $this->getEmail() == $user->getEmail();
  250.         }
  251.         return false;
  252.     }
  253.     /**
  254.      * @return mixed
  255.      */
  256.     public function getSalt(): ?string
  257.     {
  258.         //not used here
  259.         return null;
  260.     }
  261.     /**
  262.      * @return Collection<int, Registrations>
  263.      */
  264.     public function getRegistrations(): Collection
  265.     {
  266.         return $this->registrations;
  267.     }
  268.     /**
  269.      * @return Collection<int, Registrations>
  270.      */
  271.     public function getActiveRegistrations(): Collection
  272.     {
  273.         return $this->registrations->filter(static function (Registrations $registration): bool {
  274.             return (bool) $registration->isActive();
  275.         });
  276.     }
  277.     public function addRegistration(Registrations $registration): static
  278.     {
  279.         if (!$this->registrations->contains($registration)) {
  280.             $this->registrations->add($registration);
  281.             $registration->setUserId($this);
  282.         }
  283.         return $this;
  284.     }
  285.     public function removeRegistration(Registrations $registration): static
  286.     {
  287.         if ($this->registrations->removeElement($registration)) {
  288.             // set the owning side to null (unless already changed)
  289.             if ($registration->getUserId() === $this) {
  290.                 $registration->setUserId(null);
  291.             }
  292.         }
  293.         return $this;
  294.     }
  295.     /**
  296.      * @return Collection<int, Attendance>
  297.      */
  298.     public function getAttendances(): Collection
  299.     {
  300.         return $this->attendances;
  301.     }
  302.     public function addAttendance(Attendance $attendance): static
  303.     {
  304.         if (!$this->attendances->contains($attendance)) {
  305.             $this->attendances->add($attendance);
  306.             $attendance->setCreatorUserId($this);
  307.         }
  308.         return $this;
  309.     }
  310.     public function removeAttendance(Attendance $attendance): static
  311.     {
  312.         if ($this->attendances->removeElement($attendance)) {
  313.             // set the owning side to null (unless already changed)
  314.             if ($attendance->getCreatorUserId() === $this) {
  315.                 $attendance->setCreatorUserId(null);
  316.             }
  317.         }
  318.         return $this;
  319.     }
  320.     /**
  321.      * @return Collection<int, Events>
  322.      */
  323.     public function getEvents(): Collection
  324.     {
  325.         return $this->events;
  326.     }
  327.     public function addEvent(Events $event): static
  328.     {
  329.         if (!$this->events->contains($event)) {
  330.             $this->events->add($event);
  331.             $event->addCourseUser($this);
  332.         }
  333.         return $this;
  334.     }
  335.     public function removeEvent(Events $event): static
  336.     {
  337.         if ($this->events->removeElement($event)) {
  338.             $event->removeCourseUser($this);
  339.         }
  340.         return $this;
  341.     }
  342.     /**
  343.      * @return Collection<int, Events>
  344.      */
  345.     public function getManagedEvents(): Collection
  346.     {
  347.         return $this->managedEvents;
  348.     }
  349.     public function addManagedEvent(Events $managedEvent): static
  350.     {
  351.         if (!$this->managedEvents->contains($managedEvent)) {
  352.             $this->managedEvents->add($managedEvent);
  353.             $managedEvent->addManagerUser($this);
  354.         }
  355.         return $this;
  356.     }
  357.     public function removeManagedEvent(Events $managedEvent): static
  358.     {
  359.         if ($this->managedEvents->removeElement($managedEvent)) {
  360.             $managedEvent->removeManagerUser($this);
  361.         }
  362.         return $this;
  363.     }
  364.     public function getSurname(): ?string
  365.     {
  366.         return $this->surname;
  367.     }
  368.     public function setSurname(?string $surname): static
  369.     {
  370.         $this->surname $surname;
  371.         return $this;
  372.     }
  373.     public function getCustomFilterYear(): ?string
  374.     {
  375.         return $this->customFilterYear;
  376.     }
  377.     public function setCustomFilterYear(?string $customFilterYear): static
  378.     {
  379.         $this->customFilterYear $customFilterYear;
  380.         return $this;
  381.     }
  382.     #[ORM\OneToOne(mappedBy'user'cascade: ['persist''remove'])]
  383.     private ?UserProfile $profile null;
  384.     #[ORM\OneToMany(mappedBy'user'targetEntityEventRequest::class, orphanRemovaltrue)]
  385.     private Collection $eventRequests;
  386.     #[ORM\OneToMany(mappedBy'user'targetEntityUserPenalty::class, orphanRemovaltrue)]
  387.     private Collection $penalties;
  388.     public function getProfile(): ?UserProfile
  389.     {
  390.         return $this->profile;
  391.     }
  392.     public function setProfile(?UserProfile $profile): self
  393.     {
  394.         $this->profile $profile;
  395.         if ($profile && $profile->getUser() !== $this) {
  396.             $profile->setUser($this);
  397.         }
  398.         return $this;
  399.     }
  400.     /**
  401.      * @return Collection<int, EventRequest>
  402.      */
  403.     public function getEventRequests(): Collection
  404.     {
  405.         return $this->eventRequests;
  406.     }
  407.     /**
  408.      * @return Collection<int, UserPenalty>
  409.      */
  410.     public function getPenalties(): Collection
  411.     {
  412.         return $this->penalties;
  413.     }
  414.     public function addEventRequest(EventRequest $eventRequest): static
  415.     {
  416.         if (!$this->eventRequests->contains($eventRequest)) {
  417.             $this->eventRequests->add($eventRequest);
  418.             $eventRequest->setUser($this);
  419.         }
  420.         return $this;
  421.     }
  422.     public function removeEventRequest(EventRequest $eventRequest): static
  423.     {
  424.         if ($this->eventRequests->removeElement($eventRequest)) {
  425.             // set the owning side to null (unless already changed)
  426.             if ($eventRequest->getUser() === $this) {
  427.                 $eventRequest->setUser(null);
  428.             }
  429.         }
  430.         return $this;
  431.     }
  432.     public function isActive(): bool
  433.     {
  434.         return $this->active;
  435.     }
  436.     public function setActive(bool $active): self
  437.     {
  438.         $this->active $active;
  439.         return $this;
  440.     }
  441.     public function getActivationToken(): ?string
  442.     {
  443.         return $this->activationToken;
  444.     }
  445.     public function setActivationToken(?string $activationToken): self
  446.     {
  447.         $this->activationToken $activationToken;
  448.         return $this;
  449.     }
  450.     public function isEnabled(): bool
  451.     {
  452.         return $this->active && !$this->deleted;
  453.     }
  454.     public function getManagementCenter(): ?Centers
  455.     {
  456.         return $this->managementCenter;
  457.     }
  458.     public function setManagementCenter(?Centers $managementCenter): self
  459.     {
  460.         $this->managementCenter $managementCenter;
  461.         return $this;
  462.     }
  463.     public function canManageEventRequestsAndRegistrations(): bool
  464.     {
  465.         return $this->canManageEventRequestsAndRegistrations;
  466.     }
  467.     public function isCanManageEventRequestsAndRegistrations(): bool
  468.     {
  469.         return $this->canManageEventRequestsAndRegistrations;
  470.     }
  471.     public function setCanManageEventRequestsAndRegistrations(bool $canManageEventRequestsAndRegistrations): self
  472.     {
  473.         $this->canManageEventRequestsAndRegistrations $canManageEventRequestsAndRegistrations;
  474.         return $this;
  475.     }
  476. }