src/Entity/User.php line 368

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. /**
  8.  * @ORM\Table(name="users")
  9.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  10.  */
  11. class User implements UserInterface, \JsonSerializable
  12. {
  13.     const ROLE_ADMIN = "ROLE_ADMIN";
  14.     const ROLE_COMPANY = "ROLE_COMPANY";
  15.     const ROLE_CANDIDATE = "ROLE_CANDIDATE";
  16.     const ROLES = [
  17.         self::ROLE_ADMIN => 'Admin',
  18.         self::ROLE_COMPANY => 'Company',
  19.         self::ROLE_CANDIDATE => 'Candidate'
  20.     ];
  21.     /**
  22.      * @ORM\Id()
  23.      * @ORM\GeneratedValue()
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.      /**
  28.      * One User has One Company.
  29.      * @ORM\OneToOne(targetEntity="Company", mappedBy="user")
  30.      */
  31.     private $company;
  32.     /**
  33.      * @ORM\OneToOne(targetEntity="Candidate", mappedBy="user")
  34.      */
  35.     private $candidate;
  36.     /**
  37.      *
  38.      * @ORM\Column(type="string", length=180)
  39.      * @Assert\Email()
  40.      * @Assert\NotBlank()
  41.      */
  42.     private $email;
  43.     /**
  44.      * @ORM\Column(type="json")
  45.      */
  46.     private $roles = [];
  47.     /**
  48.      * @var string The hashed password
  49.      * @ORM\Column(type="string", nullable=true)
  50.      */
  51.     private $password;
  52.     /**
  53.      * @ORM\Column(type="string", nullable=true)
  54.      */
  55.     private $accessTokenEconomedia;
  56.     /**
  57.      * @ORM\Column(type="string", nullable=true)
  58.      */
  59.     private $refreshTokenEconomedia;
  60.     
  61.      /**
  62.      * @ORM\Column(type="integer", nullable=true)
  63.      */
  64.     private $idEconomedia;
  65.     
  66.      /**
  67.      * @ORM\Column(type="integer", nullable=true)
  68.      */
  69.     private $personaldataAgreements;
  70.      /**
  71.      * @ORM\Column(type="integer", nullable=true)
  72.      */
  73.     private $economediaAgreements;
  74.      /**
  75.      * @ORM\Column(type="integer", nullable=true)
  76.      */
  77.     private $partnersAgreements;
  78.     /**
  79.      * @ORM\Column(type="datetime", nullable=true)
  80.      */
  81.     private $createdAt;
  82.     /**
  83.      * @ORM\Column(type="boolean", options={"default": "0"})
  84.      */
  85.     private $fromNewsletter = FALSE;
  86.     private $newPassword;
  87.     public function getId(): ?int
  88.     {
  89.         return $this->id;
  90.     }
  91.     public function getCompany(): ?Company
  92.     {
  93.         return $this->company;
  94.     }
  95.     public function setCompany(?Company $company): self
  96.     {
  97.         $this->company = $company;
  98.         return $this;
  99.     }
  100.     public function removeCompany(Company $company): self
  101.     {
  102.         if ($this->company == $company) {
  103.             $this->company = null;
  104.         }
  105.         return $this;
  106.     }
  107.     public function getCandidate(): ?Candidate
  108.     {
  109.         return $this->candidate;
  110.     }
  111.     public function setCandidate(?Candidate $candidate): self
  112.     {
  113.         $this->candidate = $candidate;
  114.         return $this;
  115.     }
  116.     public function removeCandidate(Candidate $candidate): self
  117.     {
  118.         if ($this->candidate == $candidate) {
  119.             $this->candidate = null;
  120.         }
  121.         return $this;
  122.     }
  123.     
  124.     public function getEmail(): ?string
  125.     {
  126.         return $this->email;
  127.     }
  128.     public function setEmail(string $email): self
  129.     {
  130.         $this->email = $email;
  131.         return $this;
  132.     }
  133.     
  134.     public function getAccessTokenEconomedia(): ?string
  135.     {
  136.         return $this->accessTokenEconomedia;
  137.     }
  138.     public function setAccessTokenEconomedia(string $accessTokenEconomedia): self
  139.     {
  140.         $this->accessTokenEconomedia = $accessTokenEconomedia;
  141.         return $this;
  142.     }
  143.     
  144.     public function getRefreshTokenEconomedia(): ?string
  145.     {
  146.         return $this->refreshTokenEconomedia;
  147.     }
  148.     public function setRefreshTokenEconomedia(string $refreshTokenEconomedia): self
  149.     {
  150.         $this->refreshTokenEconomedia = $refreshTokenEconomedia;
  151.         return $this;
  152.     }
  153.     
  154.     public function getIdEconomedia(): ?int
  155.     {
  156.         return $this->idEconomedia;
  157.     }
  158.     public function setIdEconomedia(int $idEconomedia): self
  159.     {
  160.         $this->idEconomedia = $idEconomedia;
  161.         return $this;
  162.     }
  163.     
  164.     public function getPersonaldataAgreements(): ?int
  165.     {
  166.         return $this->personaldataAgreements;
  167.     }
  168.     public function setPersonaldataAgreements(int $personaldataAgreements): self
  169.     {
  170.         $this->personaldataAgreements = $personaldataAgreements;
  171.         return $this;
  172.     }
  173.     
  174.     public function getEconomediaAgreements(): ?int
  175.     {
  176.         return $this->economediaAgreements;
  177.     }
  178.     public function setEconomediaAgreements(int $economediaAgreements): self
  179.     {
  180.         $this->economediaAgreements = $economediaAgreements;
  181.         return $this;
  182.     }
  183.     
  184.     public function getPartnersAgreements(): ?int
  185.     {
  186.         return $this->partnersAgreements;
  187.     }
  188.     public function setPartnersAgreements(int $partnersAgreements): self
  189.     {
  190.         $this->partnersAgreements = $partnersAgreements;
  191.         return $this;
  192.     }
  193.     public function getCreatedAt(): ?\DateTime {
  194.         return $this->createdAt;
  195.     }
  196.     public function setCreatedAt(\DateTime $createdAt): self {
  197.         $this->createdAt = $createdAt;
  198.         return $this;
  199.     }
  200.     /**
  201.      * @return bool
  202.      */
  203.     public function getFromNewsletter() {
  204.         return $this->fromNewsletter;
  205.     }
  206.     /**
  207.      * @param bool $fromNewsletter
  208.      *
  209.      * @return self
  210.      */
  211.     public function setFromNewsletter(bool $fromNewsletter): self {
  212.         $this->fromNewsletter = $fromNewsletter;
  213.         return $this;
  214.     }
  215.     /**
  216.      * A visual identifier that represents this user.
  217.      *
  218.      * @see UserInterface
  219.      */
  220.     public function getUsername(): string
  221.     {
  222.         return (string) $this->email;
  223.     }
  224.     /**
  225.      * @see UserInterface
  226.      */
  227.     public function getRoles(): array
  228.     {
  229.         $roles = $this->roles;
  230.         // guarantee every user at least has ROLE_USER - вече не е нужно
  231.         // $roles[] = 'ROLE_USER';
  232.         return array_unique($roles);
  233.     }
  234.     public function addRole($role): self
  235.     {
  236.         if(!in_array($role, $this->getRoles())){
  237.             $this->roles[] = $role;
  238.         }
  239.         return $this;
  240.     }
  241.     public function setRoles(array $roles): self
  242.     {
  243.         $this->roles = $roles;
  244.         return $this;
  245.     }
  246.     public function removeRole($role): self
  247.     {
  248.         if (($key = array_search($role, $this->getRoles())) !== false) {
  249.             array_splice($this->roles, $key, 1);
  250.         }
  251.         return $this;
  252.     }
  253.     /**
  254.      * @see UserInterface
  255.      */
  256.     public function getPassword(): ?string
  257.     {
  258.         return $this->password;
  259.     }
  260.     public function setPassword(?string $password): self
  261.     {
  262.         if (!is_null($password) && $password != '') {
  263.             $this->password = $password;
  264.         }
  265.         return $this;
  266.     }
  267.     public function getNewPassword(): ?string
  268.     {
  269.         return $this->newPassword;
  270.     }
  271.     public function setNewPassword(?string $newPassword): self
  272.     {
  273.         if (!is_null($newPassword) && $newPassword != '') {
  274.             $this->newPassword = $newPassword;
  275.         }
  276.         return $this;
  277.     }
  278.     /**
  279.      * @see UserInterface
  280.      * @return ?string
  281.      */
  282.     public function getSalt()
  283.     {
  284.         // not needed when using the "bcrypt" algorithm in security.yaml
  285.     }
  286.     /**
  287.      * @see UserInterface
  288.      */
  289.     public function eraseCredentials()
  290.     {
  291.         // If you store any temporary, sensitive data on the user, clear it here
  292.         // $this->plainPassword = null;
  293.     }
  294.     public function __toString() {
  295.         return $this->getEmail();
  296.     }
  297.     /**
  298.      * Specify data which should be serialized to JSON
  299.      *
  300.      * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
  301.      * @return mixed data which can be serialized by <b>json_encode</b>,
  302.      * which is a value of any type other than a resource.
  303.      * @since 5.4.0
  304.      */
  305.     public function jsonSerialize() {
  306.         $company = $this->getCompany();
  307.         return [
  308.             'id' => $this->getId(),
  309.             'email' => $this->getEmail(),
  310.             'roles' => $this->getRoles(),
  311.             'company' => $company,
  312.             'subscription' => $company ? $company->getOneActiveSubscription() : null,
  313.             'candidate' => $this->getCandidate(),
  314.             'economediaId' => $this->getIdEconomedia(),
  315.             'createdAt' => $this->getCreatedAt() ? $this->getCreatedAt()->format('Y-m-d H:i:s') : null,
  316.         ];
  317.     }
  318. }