src/Entity/User.php line 18

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. /**
  11.  * @ORM\Entity(repositoryClass=UserRepository::class)
  12.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  13.  * @ORM\HasLifecycleCallbacks()
  14.  */
  15. class User implements UserInterfacePasswordAuthenticatedUserInterface
  16. {
  17.     use Timestamps;
  18.     public const USER_TYPE_ADMIN "admin";
  19.     public const USER_TYPE_PUBLICATION "publication";
  20.     public const USER_TYPE_INDIVIDUAL "individual";
  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 $email;
  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="boolean")
  42.      */
  43.     private $isVerified false;
  44.     /**
  45.      * @ORM\Column(type="string", length=255)
  46.      */
  47.     private $name;
  48.     /**
  49.      * @ORM\Column(type="boolean")
  50.      */
  51.     private $isActive false;
  52.     /**
  53.      * @ORM\ManyToOne(targetEntity=Publication::class, inversedBy="users")
  54.      */
  55.     private $publication;
  56.     /**
  57.      * @ORM\Column(type="string", length=20)
  58.      */
  59.     private $type;
  60.     /**
  61.      * @ORM\Column(type="string", length=255, nullable=true)
  62.      */
  63.     private $country;
  64.     /**
  65.      * @ORM\OneToMany(targetEntity=Files::class, mappedBy="userid")
  66.      */
  67.     private $files;
  68.     public function __construct()
  69.     {
  70.         $this->files = new ArrayCollection();
  71.     }
  72.     public function getId(): ?int
  73.     {
  74.         return $this->id;
  75.     }
  76.     public function getEmail(): ?string
  77.     {
  78.         return $this->email;
  79.     }
  80.     public function setEmail(string $email): self
  81.     {
  82.         $this->email $email;
  83.         return $this;
  84.     }
  85.     /**
  86.      * A visual identifier that represents this user.
  87.      *
  88.      * @see UserInterface
  89.      */
  90.     public function getUserIdentifier(): string
  91.     {
  92.         return (string) $this->email;
  93.     }
  94.     /**
  95.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  96.      */
  97.     public function getUsername(): string
  98.     {
  99.         return (string) $this->email;
  100.     }
  101.     /**
  102.      * @see UserInterface
  103.      */
  104.     public function getRoles(): array
  105.     {
  106.         $roles $this->roles;
  107.         // guarantee every user at least has ROLE_USER
  108.         $roles[] = 'ROLE_USER';
  109.         return array_unique($roles);
  110.     }
  111.     public function setRoles(array $roles): self
  112.     {
  113.         $this->roles $roles;
  114.         return $this;
  115.     }
  116.     /**
  117.      * @see PasswordAuthenticatedUserInterface
  118.      */
  119.     public function getPassword(): string
  120.     {
  121.         return $this->password;
  122.     }
  123.     public function setPassword(string $password): self
  124.     {
  125.         $this->password $password;
  126.         return $this;
  127.     }
  128.     /**
  129.      * Returning a salt is only needed, if you are not using a modern
  130.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  131.      *
  132.      * @see UserInterface
  133.      */
  134.     public function getSalt(): ?string
  135.     {
  136.         return null;
  137.     }
  138.     /**
  139.      * @see UserInterface
  140.      */
  141.     public function eraseCredentials()
  142.     {
  143.         // If you store any temporary, sensitive data on the user, clear it here
  144.         // $this->plainPassword = null;
  145.     }
  146.     public function isVerified(): bool
  147.     {
  148.         return $this->isVerified;
  149.     }
  150.     public function setIsVerified(bool $isVerified): self
  151.     {
  152.         $this->isVerified $isVerified;
  153.         return $this;
  154.     }
  155.     public function getName(): ?string
  156.     {
  157.         return $this->name;
  158.     }
  159.     public function setName(string $name): self
  160.     {
  161.         $this->name $name;
  162.         return $this;
  163.     }
  164.     public function getPrettyRole()
  165.     {
  166.         return  $this->type;
  167.     }
  168.     public function getAvatar()
  169.     {
  170.         $keys array_merge(explode(" "$this->getName()), [" "," "]);
  171.         
  172.         $keys array_filter($keys);
  173.         $keys array_merge($keys, [""""]);
  174.         
  175.         $avatar "";
  176.         for ($i=0$i 2$i++) {
  177.             $avatar .= $keys[$i][0];
  178.         }
  179.         return trim($avatar);
  180.     }
  181.     public function isActive(): ?bool
  182.     {
  183.         return $this->isActive;
  184.     }
  185.     public function getActive(): ?bool
  186.     {
  187.         return $this->isActive;
  188.     }
  189.     public function setIsActive(bool $isActive): self
  190.     {
  191.         $this->isActive $isActive;
  192.         return $this;
  193.     }
  194.     public function getPublication(): ?Publication
  195.     {
  196.         return $this->publication;
  197.     }
  198.     public function setPublication(?Publication $publication): self
  199.     {
  200.         $this->publication $publication;
  201.         return $this;
  202.     }
  203.     public function getType(): ?string
  204.     {
  205.         return $this->type;
  206.     }
  207.     public function setType(string $type): self
  208.     {
  209.         $this->type $type;
  210.         $roles = ['ROLE_USER'];
  211.         switch ($type) {
  212.             case self::USER_TYPE_ADMIN:
  213.                 $roles[] = "ROLE_ADMIN";
  214.                 $this->setRoles($roles);
  215.                 break;
  216.             case self::USER_TYPE_INDIVIDUAL:
  217.                 $roles[] = "ROLE_INDIVIDUAL";
  218.                 $this->setRoles($roles);
  219.                 break;
  220.             case self::USER_TYPE_PUBLICATION:
  221.                 $roles[] = "ROLE_PUBLICATION";
  222.                 $this->setRoles($roles);
  223.                 break;
  224.         }
  225.         return $this;
  226.     }
  227.     public function getCountry(): ?string
  228.     {
  229.         return $this->country;
  230.     }
  231.     public function setCountry(?string $country): self
  232.     {
  233.         $this->country $country;
  234.         return $this;
  235.     }
  236.     public static function getTypeChoices()
  237.     {
  238.         return [
  239.             self::USER_TYPE_ADMIN => self::USER_TYPE_ADMIN,
  240.             self::USER_TYPE_INDIVIDUAL => self::USER_TYPE_INDIVIDUAL,
  241.             self::USER_TYPE_PUBLICATION => self::USER_TYPE_PUBLICATION,
  242.         ];
  243.     }
  244.     public function getReportTypes()
  245.     {
  246.         $types = ["individual" => "Individual"];
  247.         if ($this->type ==self::USER_TYPE_ADMIN) {
  248.             $types['anonymous'] = "Anonymous";
  249.         }
  250.         if ($this->type == self::USER_TYPE_ADMIN || $this->type == self::USER_TYPE_PUBLICATION) {
  251.             $types['publications'] = "Publications";
  252.         }
  253.         $types['aggregated'] = "Aggregated";
  254.         return $types;
  255.     }
  256.     /**
  257.      * @return Collection<int, Files>
  258.      */
  259.     public function getFiles(): Collection
  260.     {
  261.         return $this->files;
  262.     }
  263.     public function addFile(Files $file): self
  264.     {
  265.         if (!$this->files->contains($file)) {
  266.             $this->files[] = $file;
  267.             $file->setUserid($this);
  268.         }
  269.         return $this;
  270.     }
  271.     public function removeFile(Files $file): self
  272.     {
  273.         if ($this->files->removeElement($file)) {
  274.             // set the owning side to null (unless already changed)
  275.             if ($file->getUserid() === $this) {
  276.                 $file->setUserid(null);
  277.             }
  278.         }
  279.         return $this;
  280.     }
  281. }