Advertisement
Guest User

Untitled

a guest
May 13th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Entity;
  4.  
  5. use App\Repository\UserRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11.  
  12. #[ORM\Entity(repositoryClass: UserRepository::class)]
  13. #[ORM\Table(name: '`user`')]
  14. #[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
  15. class User implements UserInterface, PasswordAuthenticatedUserInterface
  16. {
  17.     #[ORM\Id]
  18.    #[ORM\GeneratedValue]
  19.    #[ORM\Column]
  20.    private ?int $id = null;
  21.  
  22.     #[ORM\Column(length: 180)]
  23.    private ?string $email = null;
  24.  
  25.     /**
  26.      * @var list<string> The user roles
  27.      */
  28.     #[ORM\Column]
  29.    private array $roles = [];
  30.  
  31.     /**
  32.      * @var string The hashed password
  33.      */
  34.     #[ORM\Column]
  35.    private ?string $password = null;
  36.  
  37.     /**
  38.      * @var Collection<int, Group>
  39.      */
  40.     #[ORM\ManyToMany(targetEntity: Group::class, mappedBy: 'users')]
  41.    private Collection $groups;
  42.  
  43.     public function __construct()
  44.     {
  45.         $this->groups = new ArrayCollection();
  46.     }
  47.  
  48.     public function getId(): ?int
  49.     {
  50.         return $this->id;
  51.     }
  52.  
  53.     public function getEmail(): ?string
  54.     {
  55.         return $this->email;
  56.     }
  57.  
  58.     public function setEmail(string $email): static
  59.     {
  60.         $this->email = $email;
  61.  
  62.         return $this;
  63.     }
  64.  
  65.     /**
  66.      * A visual identifier that represents this user.
  67.      *
  68.      * @see UserInterface
  69.      */
  70.     public function getUserIdentifier(): string
  71.     {
  72.         return (string) $this->email;
  73.     }
  74.  
  75.     /**
  76.      * @see UserInterface
  77.      *
  78.      * @return list<string>
  79.      */
  80.     public function getRoles(): array
  81.     {
  82.         $roles = $this->roles;
  83.         // guarantee every user at least has ROLE_USER
  84.         $roles[] = 'ROLE_USER';
  85.  
  86.         return array_unique($roles);
  87.     }
  88.  
  89.     /**
  90.      * @param list<string> $roles
  91.      */
  92.     public function setRoles(array $roles): static
  93.     {
  94.         $this->roles = $roles;
  95.  
  96.         return $this;
  97.     }
  98.  
  99.     /**
  100.      * @see PasswordAuthenticatedUserInterface
  101.      */
  102.     public function getPassword(): string
  103.     {
  104.         return $this->password;
  105.     }
  106.  
  107.     public function setPassword(string $password): static
  108.     {
  109.         $this->password = $password;
  110.  
  111.         return $this;
  112.     }
  113.  
  114.     /**
  115.      * @see UserInterface
  116.      */
  117.     public function eraseCredentials(): void
  118.     {
  119.         // If you store any temporary, sensitive data on the user, clear it here
  120.         // $this->plainPassword = null;
  121.     }
  122.  
  123.     /**
  124.      * @return Collection<int, Group>
  125.      */
  126.     public function getGroups(): Collection
  127.     {
  128.         return $this->groups;
  129.     }
  130.  
  131.     public function addGroup(Group $group): static
  132.     {
  133.         if (!$this->groups->contains($group)) {
  134.             $this->groups->add($group);
  135.             $group->addUser($this);
  136.         }
  137.  
  138.         return $this;
  139.     }
  140.  
  141.     public function removeGroup(Group $group): static
  142.     {
  143.         if ($this->groups->removeElement($group)) {
  144.             $group->removeUser($this);
  145.         }
  146.  
  147.         return $this;
  148.     }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement