src/Entity/User.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\Validator\Mapping\ClassMetadata;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. /**
  11.  * @ORM\Entity(repositoryClass=UserRepository::class)
  12.  */
  13. class User implements UserInterface
  14. {
  15.     const ROLE_USER = 'ROLE_USER';
  16.     const ROLE_CREATOR = 'ROLE_CREATOR';
  17.     const ROLE_EDITOR = 'ROLE_EDITOR';
  18.     const ROLE_SETTINGS = 'ROLE_SETTINGS';
  19.     const ROLE_DELETE = 'ROLE_DELETE';
  20.     const ROLE_PRINT = 'ROLE_PRINT';
  21.     const ROLE_ADMIN = 'ROLE_ADMIN';
  22.     /**
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue
  25.      * @ORM\Column(type="integer")
  26.      */
  27.     private $id;
  28.     /**
  29.      * @ORM\Column(type="string", length=180, unique=true)
  30.      */
  31.     private $username;
  32.     /**
  33.      * @ORM\Column(type="string", length=250)
  34.      */
  35.     private $displayName;
  36.     /**
  37.      * @ORM\Column(type="string", length=250)
  38.      */
  39.     private $email;
  40.     /**
  41.      * @ORM\Column(type="json")
  42.      */
  43.     private $roles = [];
  44.     /**
  45.      * @var string The hashed password
  46.      * @ORM\Column(type="string")
  47.      */
  48.     private $password;
  49.     /**
  50.      * @var \DateTime
  51.      *
  52.      * @ORM\Column(name="modified", type="datetime", nullable=true, options={"comment"="Módosítva"})
  53.      */
  54.     private $modified;
  55.     private $passwordRetryA;
  56.     private $passwordRetryB;
  57.     public static $rolesList = [
  58.         'Megtekintés' => 'ROLE_USER',
  59.         'Létrehozás' => 'ROLE_CREATOR',
  60.         'Szerkesztés' => 'ROLE_EDITOR',
  61.         'Törlés' => 'ROLE_DELETE',
  62.         'Beállítás' => 'ROLE_SETTINGS',
  63.         'Nyomtatás' => 'ROLE_PRINT',
  64.         'Adminisztrátor' => 'ROLE_ADMIN',
  65.         'Group Adminisztrátor' => 'ROLE_GROUP_ADMIN',
  66.     ];
  67.   
  68.     /**
  69.      * @ORM\ManyToMany(targetEntity=Group::class, mappedBy="member")
  70.      */
  71.     private $groups;
  72.     /**
  73.      * @ORM\OneToMany(targetEntity=Group::class, mappedBy="owner")
  74.      */
  75.     private $ownedGroups;
  76.     /**
  77.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="users")
  78.      */
  79.     private $parent;
  80.     /**
  81.      * @ORM\OneToMany(targetEntity=User::class, mappedBy="parent")
  82.      */
  83.     private $users;
  84.     public function __construct()
  85.     {
  86.         $this->groups = new ArrayCollection();
  87.         $this->ownedGroups = new ArrayCollection();
  88.         $this->users = new ArrayCollection();
  89.     }
  90.     public function getRoleName(string $role){
  91.       foreach (User::$rolesList as $key => $value) {
  92.         if($value == $role){
  93.             return $key;
  94.         }
  95.       }
  96.     }
  97.     public static function loadValidatorMetadata(ClassMetadata $metadata)
  98.     {
  99.         $metadata->addGetterConstraint('checkPassword', new Assert\IsTrue([
  100.             'message' => 'Jelszavak nem egyeznek meg',
  101.         ]));
  102.     }
  103.     public function getCheckPassword()
  104.     {
  105.         return $this->passwordRetryA == $this->passwordRetryB;
  106.     }
  107.     public function getId(): ?int
  108.     {
  109.         return $this->id;
  110.     }
  111.     /**
  112.      * A visual identifier that represents this user.
  113.      *
  114.      * @see UserInterface
  115.      */
  116.     public function getUserIdentifier(): string
  117.     {
  118.         return (string) $this->email;
  119.     }
  120.     /**
  121.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  122.      */
  123.     public function getUsername(): string
  124.     {
  125.         return (string) $this->username;
  126.     }
  127.     public function setUsername(string $username): self
  128.     {
  129.         $this->username = $username;
  130.         return $this;
  131.     }
  132.     /**
  133.      * A visual identifier that represents this user.
  134.      *
  135.      * @see UserInterface
  136.      */
  137.     public function getDisplayName(): string
  138.     {
  139.         return (string) $this->displayName;
  140.     }
  141.     public function setDisplayName(string $displayName): self
  142.     {
  143.         $this->displayName = $displayName;
  144.         return $this;
  145.     }
  146.     /**
  147.      * A visual identifier that represents this user.
  148.      *
  149.      * @see UserInterface
  150.      */
  151.     public function getEmail(): string
  152.     {
  153.         return (string) $this->email;
  154.     }
  155.     public function setEmail(string $email): self
  156.     {
  157.         $this->email = $email;
  158.         return $this;
  159.     }
  160.     /**
  161.      * @see UserInterface
  162.      */
  163.     public function getRoles(): array
  164.     {
  165.         $roles = $this->roles;
  166.         // guarantee every user at least has ROLE_USER
  167.         //$roles[] = 'ROLE_USER';
  168.         return array_unique($roles);
  169.     }
  170.     public function setRoles(array $roles): self
  171.     {
  172.         $this->roles = $roles;
  173.         return $this;
  174.     }
  175.     public function hasRoles()
  176.     {
  177.         foreach (func_get_args() as $k => $role) {
  178.             if (in_array($role, $this->roles)) {
  179.                 return true;
  180.             }
  181.         }
  182.         return false;
  183.     }
  184.     
  185.     public function setRole(string $role): self
  186.     {
  187.         $this->roles[] = $role;
  188.         return $this;
  189.     }
  190.     /**
  191.      * @see UserInterface
  192.      */
  193.     public function getPassword(): string
  194.     {
  195.         return (string) $this->password;
  196.     }
  197.     public function setPassword(string $password): self
  198.     {
  199.         $this->password = $password;
  200.         return $this;
  201.     }
  202.     public function getPasswordRetryA(): string
  203.     {
  204.         return (string) $this->passwordRetryA;
  205.     }
  206.     public function setPasswordRetryA(?string $password): self
  207.     {
  208.         $this->passwordRetryA = $password;
  209.         return $this;
  210.     }
  211.     public function getPasswordRetryB(): string
  212.     {
  213.         return (string) $this->passwordRetryB;
  214.     }
  215.     public function setPasswordRetryB(?string $password): self
  216.     {
  217.         $this->passwordRetryB = $password;
  218.         return $this;
  219.     }
  220.     /**
  221.      * @see UserInterface
  222.      */
  223.     public function getSalt()
  224.     {
  225.         // not needed when using the "bcrypt" algorithm in security.yaml
  226.     }
  227.     /**
  228.      * @see UserInterface
  229.      */
  230.     public function eraseCredentials()
  231.     {
  232.         // If you store any temporary, sensitive data on the user, clear it here
  233.         // $this->plainPassword = null;
  234.     }
  235.     public function getModified(): ?\DateTimeInterface
  236.     {
  237.         return $this->modified;
  238.     }
  239.     
  240.     /**
  241.      * @return Array<int>
  242.      */
  243.     public function getGroupsIds(): Array
  244.     {
  245.         foreach ($this->getOwnedGroups() as $key => $value) {
  246.             $this->groups->add($value);
  247.         }
  248.         $groups = [];
  249.         foreach ($this->groups as $key => $value) {
  250.             $groups[] = $value->getId();
  251.         }
  252.         return $groups;
  253.     }
  254.     /**
  255.      * @return Collection<int, Group>
  256.      */
  257.     public function getGroups(): Collection
  258.     {
  259.         foreach ($this->getOwnedGroups() as $key => $value) {
  260.             $this->groups->add($value);
  261.         }
  262.         return $this->groups;
  263.     }
  264.     public function addGroup(Group $group): self
  265.     {
  266.         if (!$this->groups->contains($group)) {
  267.             $this->groups[] = $group;
  268.             $group->addMember($this);
  269.         }
  270.         return $this;
  271.     }
  272.     public function removeGroup(Group $group): self
  273.     {
  274.         if ($this->groups->removeElement($group)) {
  275.             $group->removeMember($this);
  276.         }
  277.         return $this;
  278.     }
  279.     public function __toString()
  280.     {
  281.         return $this->displayName;
  282.     }
  283.     /**
  284.      * @return Collection<int, Group>
  285.      */
  286.     public function getOwnedGroups(): Collection
  287.     {
  288.         return $this->ownedGroups;
  289.     }
  290.     public function addOwnedGroup(Group $ownedGroup): self
  291.     {
  292.         if (!$this->ownedGroups->contains($ownedGroup)) {
  293.             $this->ownedGroups[] = $ownedGroup;
  294.             $ownedGroup->setOwner($this);
  295.         }
  296.         return $this;
  297.     }
  298.     public function removeOwnedGroup(Group $ownedGroup): self
  299.     {
  300.         if ($this->ownedGroups->removeElement($ownedGroup)) {
  301.             // set the owning side to null (unless already changed)
  302.             if ($ownedGroup->getOwner() === $this) {
  303.                 $ownedGroup->setOwner(null);
  304.             }
  305.         }
  306.         return $this;
  307.     }
  308.     public function getParent(): ?self
  309.     {
  310.         return $this->parent;
  311.     }
  312.     public function setParent(?self $parent): self
  313.     {
  314.         $this->parent = $parent;
  315.         return $this;
  316.     }
  317.     /**
  318.      * @return Collection<int, self>
  319.      */
  320.     public function getUsers(): Collection
  321.     {
  322.         return $this->users;
  323.     }
  324.     public function addUser(self $user): self
  325.     {
  326.         if (!$this->users->contains($user)) {
  327.             $this->users[] = $user;
  328.             $user->setParent($this);
  329.         }
  330.         return $this;
  331.     }
  332.     public function removeUser(self $user): self
  333.     {
  334.         if ($this->users->removeElement($user)) {
  335.             // set the owning side to null (unless already changed)
  336.             if ($user->getParent() === $this) {
  337.                 $user->setParent(null);
  338.             }
  339.         }
  340.         return $this;
  341.     }
  342. }