src/Entity/User.php line 22

Open in your IDE?
  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\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * @ORM\Entity(repositoryClass=UserRepository::class)
  13.  * @UniqueEntity(fields={"username"}, message="Dieser Benutzername wird bereits verwendet")
  14.  * @UniqueEntity(
  15.  *     fields={"email"},
  16.  *     message="This email already exists!"
  17.  * )
  18.  */
  19. class User implements UserInterfacePasswordAuthenticatedUserInterface
  20. {
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\Column(type="string", length=180, unique=true)
  29.      */
  30.     private $username;
  31.     /**
  32.      * @ORM\Column(type="json")
  33.      */
  34.     private $roles = [];
  35.     /**
  36.      * @var string The hashed password
  37.      * @ORM\Column(type="string")
  38.      */
  39.     private $password;
  40.     /**
  41.      * @ORM\Column(type="string", length=255)
  42.      */
  43.     private $firstname;
  44.     /**
  45.      * @ORM\Column(type="string", length=255)
  46.      */
  47.     private $lastname;
  48.     /**
  49.      * @ORM\Column(type="datetime_immutable")
  50.      */
  51.     private $createdAt;
  52.     /**
  53.      * @ORM\Column(type="string", length=255)
  54.      */
  55.     private $email;
  56.     /**
  57.      * @ORM\Column(type="boolean")
  58.      */
  59.     private $isVerified false;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity=District::class, inversedBy="user")
  62.      */
  63.     private $district;
  64.     /**
  65.      * @ORM\Column(type="integer")
  66.      */
  67.     private $salutation;
  68.     /**
  69.      * @ORM\Column(type="string", length=255, nullable=true)
  70.      */
  71.     private $title;
  72.     /**
  73.      * @ORM\Column(type="boolean", nullable=true)
  74.      */
  75.     private $contactAllowed;
  76.     /**
  77.      * @ORM\Column(type="boolean", nullable=true)
  78.      */
  79.     private $activated false;
  80.     /**
  81.      * @ORM\Column(type="datetime_immutable", nullable=true)
  82.      */
  83.     private $activatedAt;
  84.     /**
  85.      * @ORM\OneToMany(targetEntity=JuryMember::class, mappedBy="user")
  86.      */
  87.     private $juryMembers;
  88.     /**
  89.      * @ORM\OneToMany(targetEntity=Rating::class, mappedBy="user")
  90.      */
  91.     private $ratings;
  92.     /**
  93.      * @ORM\Column(type="string", length=255, nullable=true)
  94.      */
  95.     private $function;
  96.     /**
  97.      * @ORM\Column(type="string", length=255, nullable=true)
  98.      */
  99.     private $institution;
  100.     /**
  101.      * @ORM\Column(type="string", length=50, nullable=true)
  102.      */
  103.     private $phone;
  104.     /**
  105.      * @ORM\Column(type="string", length=50, nullable=true)
  106.      */
  107.     private $mobile;
  108.     /**
  109.      * @ORM\Column(type="boolean", nullable=true)
  110.      */
  111.     private $notify;
  112.     public function __construct()
  113.     {
  114.         $this->juryMembers = new ArrayCollection();
  115.         $this->ratings = new ArrayCollection();
  116.     }
  117.     public function getId(): ?int
  118.     {
  119.         return $this->id;
  120.     }
  121.     /**
  122.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  123.      */
  124.     public function getUsername(): string
  125.     {
  126.         return (string)$this->username;
  127.     }
  128.     public function setUsername(?string $username): self
  129.     {
  130.         $this->username $username;
  131.         return $this;
  132.     }
  133.     /**
  134.      * A visual identifier that represents this user.
  135.      *
  136.      * @see UserInterface
  137.      */
  138.     public function getUserIdentifier(): string
  139.     {
  140.         return (string)$this->username;
  141.     }
  142.     /**
  143.      * @see UserInterface
  144.      */
  145.     public function getRoles(): array
  146.     {
  147.         $roles $this->roles;
  148.         // guarantee every user at least has ROLE_USER
  149.         $roles[] = 'ROLE_USER';
  150.         try {
  151.             $roles array_unique($roles);
  152.         } catch (\Exception $e) {
  153.             dd($roles);
  154.         }
  155.         return $roles;
  156.     }
  157.     public function setRoles(array $roles): self
  158.     {
  159.         $roles array_diff($roles, ["ROLE_USER"]);
  160.         $this->roles $roles;
  161.         return $this;
  162.     }
  163.     /**
  164.      * @see PasswordAuthenticatedUserInterface
  165.      */
  166.     public function getPassword(): string
  167.     {
  168.         return $this->password;
  169.     }
  170.     public function setPassword(string $password): self
  171.     {
  172.         $this->password $password;
  173.         return $this;
  174.     }
  175.     /**
  176.      * Returning a salt is only needed, if you are not using a modern
  177.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  178.      *
  179.      * @see UserInterface
  180.      */
  181.     public function getSalt(): ?string
  182.     {
  183.         return null;
  184.     }
  185.     /**
  186.      * @see UserInterface
  187.      */
  188.     public function eraseCredentials()
  189.     {
  190.         // If you store any temporary, sensitive data on the user, clear it here
  191.         // $this->plainPassword = null;
  192.     }
  193.     /**
  194.      * @Assert\NotBlank(message="Bitte geben Sie einen Vornamen an")
  195.      */
  196.     public function getFirstname(): ?string
  197.     {
  198.         return $this->firstname;
  199.     }
  200.     public function setFirstname(?string $firstname): self
  201.     {
  202.         $this->firstname $firstname;
  203.         return $this;
  204.     }
  205.     /**
  206.      * @Assert\NotBlank(message="Bitte geben Sie einen Nachnamen an")
  207.      */
  208.     public function getLastname(): ?string
  209.     {
  210.         return $this->lastname;
  211.     }
  212.     public function setLastname(?string $lastname): self
  213.     {
  214.         $this->lastname $lastname;
  215.         return $this;
  216.     }
  217.     public function getCreatedAt(): ?\DateTimeImmutable
  218.     {
  219.         return $this->createdAt;
  220.     }
  221.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  222.     {
  223.         $this->createdAt $createdAt;
  224.         return $this;
  225.     }
  226.     /**
  227.      * @Assert\NotBlank(message="Bitte geben Sie eine E-Mail-Adresse an")
  228.      * @Assert\Email(
  229.      *     message="Bitte geben Sie eine gültige E-Mail-Adresse an"
  230.      *     )
  231.      */
  232.     public function getEmail(): ?string
  233.     {
  234.         return $this->email;
  235.     }
  236.     public function setEmail(?string $email): self
  237.     {
  238.         $this->email $email;
  239.         return $this;
  240.     }
  241.     public function isVerified(): bool
  242.     {
  243.         return $this->isVerified;
  244.     }
  245.     public function setIsVerified(bool $isVerified): self
  246.     {
  247.         $this->isVerified $isVerified;
  248.         return $this;
  249.     }
  250.     public function getDistrict(): ?District
  251.     {
  252.         return $this->district;
  253.     }
  254.     public function setDistrict(?District $district): self
  255.     {
  256.         $this->district $district;
  257.         return $this;
  258.     }
  259.     public function getSalutation(): ?int
  260.     {
  261.         return $this->salutation;
  262.     }
  263.     public function setSalutation(?int $salutation): self
  264.     {
  265.         $this->salutation $salutation;
  266.         return $this;
  267.     }
  268.     public function getTitle(): ?string
  269.     {
  270.         return $this->title;
  271.     }
  272.     public function setTitle(?string $title): self
  273.     {
  274.         $this->title $title;
  275.         return $this;
  276.     }
  277.     public function isContactAllowed(): ?bool
  278.     {
  279.         return $this->contactAllowed;
  280.     }
  281.     public function setContactAllowed(?bool $contactAllowed): self
  282.     {
  283.         $this->contactAllowed $contactAllowed;
  284.         return $this;
  285.     }
  286.     public function isActivated(): ?bool
  287.     {
  288.         return $this->activated;
  289.     }
  290.     public function setActivated(?bool $activated): self
  291.     {
  292.         $this->activated $activated;
  293.         return $this;
  294.     }
  295.     public function getActivatedAt(): ?\DateTimeImmutable
  296.     {
  297.         return $this->activatedAt;
  298.     }
  299.     public function setActivatedAt(?\DateTimeImmutable $activatedAt): self
  300.     {
  301.         $this->activatedAt $activatedAt;
  302.         return $this;
  303.     }
  304.     // Helper Functions
  305.     public function isDistrictUser(): bool
  306.     {
  307.         return in_array('ROLE_DISTRICT'$this->getRoles(), true);
  308.     }
  309.     public function isManagerUser(): bool
  310.     {
  311.         return in_array('ROLE_MANAGER'$this->getRoles(), true);
  312.     }
  313.     public function isJuryUser(): ?bool
  314.     {
  315.         return in_array('ROLE_JURY'$this->getRoles(), true);
  316.     }
  317.     public function setJuryUser(?bool $juryUser): self
  318.     {
  319.         $roles $this->getRoles();
  320.         if ($juryUser === true) {
  321.             if (!$this->isJuryUser()) {
  322.                 $roles[] = 'ROLE_JURY';
  323.                 $this->setRoles($roles);
  324.             }
  325.         } else {
  326.             if ($this->isJuryUser()) {
  327.                 $roles array_diff($roles, ['ROLE_JURY']);
  328.                 $this->setRoles($roles);
  329.             }
  330.         }
  331.         return $this;
  332.     }
  333.     public function setManagerUser(?bool $juryUser): self
  334.     {
  335.         $roles $this->getRoles();
  336.         if ($juryUser === true) {
  337.             if (!$this->isJuryUser()) {
  338.                 $roles[] = 'ROLE_MANAGER';
  339.                 $this->setRoles($roles);
  340.             }
  341.         } else {
  342.             if ($this->isJuryUser()) {
  343.                 $roles array_diff($roles, ['ROLE_MANAGER']);
  344.                 $this->setRoles($roles);
  345.             }
  346.         }
  347.         return $this;
  348.     }
  349.     /**
  350.      * @return Collection<int, JuryMember>
  351.      */
  352.     public function getJuryMembers(): Collection
  353.     {
  354.         return $this->juryMembers;
  355.     }
  356.     public function addJuryMember(JuryMember $juryMember): self
  357.     {
  358.         if (!$this->juryMembers->contains($juryMember)) {
  359.             $this->juryMembers[] = $juryMember;
  360.             $juryMember->setUser($this);
  361.         }
  362.         return $this;
  363.     }
  364.     public function removeJuryMember(JuryMember $juryMember): self
  365.     {
  366.         if ($this->juryMembers->removeElement($juryMember)) {
  367.             // set the owning side to null (unless already changed)
  368.             if ($juryMember->getUser() === $this) {
  369.                 $juryMember->setUser(null);
  370.             }
  371.         }
  372.         return $this;
  373.     }
  374.     public function isJuryMember(Contest $contest): bool
  375.     {
  376.         $juryMembers $this->getJuryMembers();
  377.         foreach ($juryMembers as $juryMember) {
  378.             if ($juryMember->getContest() === $contest) {
  379.                 return true;
  380.             }
  381.         }
  382.         return false;
  383.     }
  384.     public function __toString(): string
  385.     {
  386.         return $this->getFirstname() . ' ' $this->getLastname();
  387.     }
  388.     /**
  389.      * @return Collection<int, Rating>
  390.      */
  391.     public function getRatings(): Collection
  392.     {
  393.         return $this->ratings;
  394.     }
  395.     public function addRating(Rating $rating): self
  396.     {
  397.         if (!$this->ratings->contains($rating)) {
  398.             $this->ratings[] = $rating;
  399.             $rating->setUser($this);
  400.         }
  401.         return $this;
  402.     }
  403.     public function removeRating(Rating $rating): self
  404.     {
  405.         if ($this->ratings->removeElement($rating)) {
  406.             // set the owning side to null (unless already changed)
  407.             if ($rating->getUser() === $this) {
  408.                 $rating->setUser(null);
  409.             }
  410.         }
  411.         return $this;
  412.     }
  413.     public function getFunction(): ?string
  414.     {
  415.         return $this->function;
  416.     }
  417.     public function setFunction(?string $function): self
  418.     {
  419.         $this->function $function;
  420.         return $this;
  421.     }
  422.     public function getInstitution(): ?string
  423.     {
  424.         return $this->institution;
  425.     }
  426.     public function setInstitution(?string $institution): self
  427.     {
  428.         $this->institution $institution;
  429.         return $this;
  430.     }
  431.     public function getPhone(): ?string
  432.     {
  433.         return $this->phone;
  434.     }
  435.     public function setPhone(?string $phone): self
  436.     {
  437.         $this->phone $phone;
  438.         return $this;
  439.     }
  440.     public function getMobile(): ?string
  441.     {
  442.         return $this->mobile;
  443.     }
  444.     public function setMobile(?string $mobile): self
  445.     {
  446.         $this->mobile $mobile;
  447.         return $this;
  448.     }
  449.     public function getFullSalutation(): string
  450.     {
  451.         $output $this->getFirstname() . ' ' $this->getLastname();
  452.         if ($this->getTitle()) {
  453.             $output $this->getTitle() . ' ' $output;
  454.         }
  455.         if ($this->getSalutation() == 1) {
  456.             return 'Sehr geehrte Frau ' $output;
  457.         } elseif ($this->getSalutation() == 2) {
  458.             return 'Sehr geehrter Herr ' $output;
  459.         } else {
  460.             return 'Hallo ' $output;
  461.         }
  462.     }
  463.     public function isNotify(): ?bool
  464.     {
  465.         return $this->notify;
  466.     }
  467.     public function setNotify(?bool $notify): self
  468.     {
  469.         $this->notify $notify;
  470.         return $this;
  471.     }
  472. }