src/Entity/Account.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\ORM\Mapping\JoinColumn;
  5. use Doctrine\ORM\Mapping\ManyToOne;
  6. use Doctrine\ORM\Mapping\OneToOne;
  7. use Frivol\Common\Dict\AccountStatus;
  8. use JMS\Serializer\Annotation\Exclude;
  9. use JMS\Serializer\Annotation\Groups;
  10. /**
  11.  * @ORM\Table("Account")
  12.  *
  13.  * @ORM\Entity(repositoryClass="App\Repository\AccountRepository")
  14.  *
  15.  * @ORM\HasLifecycleCallbacks()
  16.  */
  17. class Account
  18. {
  19.     public const CONFIRMATION_TOKEN_SALT '-thizzzIZZZmyyyyyZzzzzecret!';
  20.     /**
  21.      * @Groups({"commonids", "conversationdetail", "conversationmessageindex", "spamindex", "messageindex", "grouplist", "trafficindex", "adminindex", "amateurupgradeindex", "waitrating", "operatorindex", "contentdetail", "mediaindex"})
  22.      *
  23.      * @ORM\Id()
  24.      *
  25.      * @ORM\GeneratedValue()
  26.      *
  27.      * @ORM\Column(type="integer")
  28.      */
  29.     private $id;
  30.     /**
  31.      * @Exclude()
  32.      *
  33.      * @ORM\Column(type="string", length=64, nullable=true)
  34.      */
  35.     private $password;
  36.     /**
  37.      * @Exclude()
  38.      *
  39.      * @ORM\Column(type="string", length=32, nullable=true)
  40.      */
  41.     private $salt;
  42.     /**
  43.      * @Groups({"accountindex", "adminindex", "webmasterindex", "amateurupgradeindex"})
  44.      *
  45.      * @ORM\Column(type="string", length=255, unique=true)
  46.      */
  47.     private $email;
  48.     /**
  49.      * @Groups({"accountindex"})
  50.      *
  51.      * @ORM\Column(type="string", length=6, nullable=true)
  52.      */
  53.     private $email_token;
  54.     /**
  55.      * @Groups({"accountstatus", "webmasterindex", "amateurindex", "admindetails", "mediaindex"})
  56.      *
  57.      * @ORM\Column(type="smallint", length=6)
  58.      */
  59.     private $status AccountStatus::NEW;
  60.     /**
  61.      * @Groups({"accountindex", "adminindex"})
  62.      *
  63.      * @ORM\Column(type="string", length=2)
  64.      */
  65.     private $locale 'de';
  66.     /**
  67.      * @Groups({"adminindex", "amateurupgradeindex", "logindetails"})
  68.      *
  69.      * @OneToOne(targetEntity="Member", inversedBy="account", cascade={"persist"})
  70.      *
  71.      * @JoinColumn(name="member_id", referencedColumnName="id", onDelete="SET NULL")
  72.      */
  73.     protected $member;
  74.     /**
  75.      * @Groups({"adminindex"})
  76.      *
  77.      * @OneToOne(targetEntity="Webmaster", inversedBy="account", cascade={"persist"})
  78.      *
  79.      * @JoinColumn(name="webmaster_id", referencedColumnName="id", onDelete="SET NULL")
  80.      */
  81.     protected $webmaster;
  82.     /**
  83.      * @Groups({"accountindex"})
  84.      *
  85.      * @ManyToOne(targetEntity="Webmaster", cascade={"persist"})
  86.      *
  87.      * @JoinColumn(name="advertised_by", referencedColumnName="id", onDelete="SET NULL")
  88.      */
  89.     protected $advertised_by;
  90.     /**
  91.      * @Groups({"webmasterstats", "accountindex"})
  92.      *
  93.      * @ORM\Column(type="integer", nullable=true)
  94.      */
  95.     private $advertised_campaign_id;
  96.     /**
  97.      * @Groups({"accountindex", "adminindex"})
  98.      *
  99.      * @ORM\Column(type="datetime")
  100.      */
  101.     private $created_at;
  102.     /**
  103.      * @ORM\Column(type="datetime")
  104.      */
  105.     private $updated_at;
  106.     /**
  107.      * @Exclude()
  108.      *
  109.      * @Groups({"accountindex"})
  110.      *
  111.      * @ManyToOne(targetEntity="Member", cascade={"persist"})
  112.      *
  113.      * @JoinColumn(name="advertised_by_member", referencedColumnName="id", onDelete="SET NULL")
  114.      */
  115.     private $advertised_by_member;
  116.     /**
  117.      * @Exclude()
  118.      *
  119.      * @Groups({"accountindex"})
  120.      *
  121.      * @ManyToOne(targetEntity="Content", cascade={"persist"})
  122.      *
  123.      * @JoinColumn(name="advertised_by_content", referencedColumnName="id", onDelete="SET NULL")
  124.      */
  125.     private $advertised_by_content;
  126.     /**
  127.      *
  128.      * @Groups({"accountindex"})
  129.      *
  130.      * @ManyToOne(targetEntity="MailProvider", cascade={"persist"})
  131.      *
  132.      * @JoinColumn(name="mailProvider", referencedColumnName="id", onDelete="SET NULL", nullable=true)
  133.      */
  134.     private $mailProvider;
  135.     /**
  136.      * @Groups({"webmasterstats"})
  137.      *
  138.      * @ORM\Column(type="string", length=255, nullable=true)
  139.      */
  140.     private $advertised_by_tracking_id;
  141.     /**
  142.      * @ORM\OneToOne(targetEntity="App\Entity\Admin", mappedBy="account", cascade={"persist", "remove"})
  143.      */
  144.     private $admin;
  145.     /**
  146.      * @Exclude()
  147.      *
  148.      * @ORM\Column(type="json", nullable=true)
  149.      */
  150.     private ?array $meta null;
  151.     /**
  152.      * @ORM\PrePersist()
  153.      */
  154.     public function prePersist()
  155.     {
  156.         if (!$this->getCreatedAt()) {
  157.             $this->setCreatedAt(new \DateTime());
  158.         }
  159.         if (!$this->getUpdatedAt()) {
  160.             $this->setUpdatedAt(new \DateTime());
  161.         }
  162.     }
  163.     public function generateEmailToken(): self
  164.     {
  165.         $this->setEmailToken(random_int(10009999));
  166.         return $this;
  167.     }
  168.     public function getId()
  169.     {
  170.         return $this->id;
  171.     }
  172.     public function getPassword(): ?string
  173.     {
  174.         return $this->password;
  175.     }
  176.     public function setPassword(?string $password): self
  177.     {
  178.         $this->password $password;
  179.         return $this;
  180.     }
  181.     public function getSalt(): ?string
  182.     {
  183.         return $this->salt;
  184.     }
  185.     public function setSalt(?string $salt): self
  186.     {
  187.         $this->salt $salt;
  188.         return $this;
  189.     }
  190.     public function getEmail(): ?string
  191.     {
  192.         return $this->email;
  193.     }
  194.     public function setEmail(string $email): self
  195.     {
  196.         $this->email trim(strtolower($email));
  197.         return $this;
  198.     }
  199.     public function getEmailToken(): ?string
  200.     {
  201.         return $this->email_token;
  202.     }
  203.     public function setEmailToken(?string $email_token): self
  204.     {
  205.         $this->email_token $email_token;
  206.         return $this;
  207.     }
  208.     public function removeEmailToken(): self
  209.     {
  210.         $this->email_token null;
  211.         return $this;
  212.     }
  213.     public function getStatus(): ?int
  214.     {
  215.         return $this->status;
  216.     }
  217.     public function setStatus(int $status): self
  218.     {
  219.         $this->status $status;
  220.         return $this;
  221.     }
  222.     public function getLocale(): ?string
  223.     {
  224.         return $this->locale;
  225.     }
  226.     public function setLocale(string $locale): self
  227.     {
  228.         $this->locale $locale;
  229.         return $this;
  230.     }
  231.     public function getAdvertisedCampaignId(): ?int
  232.     {
  233.         return $this->advertised_campaign_id;
  234.     }
  235.     public function setAdvertisedCampaignId(?int $advertised_campaign_id): self
  236.     {
  237.         $this->advertised_campaign_id $advertised_campaign_id;
  238.         return $this;
  239.     }
  240.     public function getCreatedAt(): ?\DateTimeInterface
  241.     {
  242.         return $this->created_at;
  243.     }
  244.     public function setCreatedAt(\DateTimeInterface $created_at): self
  245.     {
  246.         $this->created_at $created_at;
  247.         return $this;
  248.     }
  249.     public function getUpdatedAt(): ?\DateTimeInterface
  250.     {
  251.         return $this->updated_at;
  252.     }
  253.     public function setUpdatedAt(\DateTimeInterface $updated_at): self
  254.     {
  255.         $this->updated_at $updated_at;
  256.         return $this;
  257.     }
  258.     public function getAdvertisedByMember(): ?Member
  259.     {
  260.         return $this->advertised_by_member;
  261.     }
  262.     public function setAdvertisedByMember(?Member $member): self
  263.     {
  264.         $this->advertised_by_member $member;
  265.         return $this;
  266.     }
  267.     public function getAdvertisedByContent(): ?Content
  268.     {
  269.         return $this->advertised_by_content;
  270.     }
  271.     public function setAdvertisedByContent(?Content $advertised_by_content): self
  272.     {
  273.         $this->advertised_by_content $advertised_by_content;
  274.         return $this;
  275.     }
  276.     public function getAdvertisedByTrackingId(): ?int
  277.     {
  278.         return $this->advertised_by_tracking_id;
  279.     }
  280.     public function setAdvertisedByTrackingId(?string $advertised_by_tracking_id): self
  281.     {
  282.         $this->advertised_by_tracking_id $advertised_by_tracking_id;
  283.         return $this;
  284.     }
  285.     public function setAdvertisedBy(?Webmaster $advertised_by): self
  286.     {
  287.         $this->advertised_by $advertised_by;
  288.         return $this;
  289.     }
  290.     public function getAdvertisedBy(): ?Webmaster
  291.     {
  292.         return $this->advertised_by;
  293.     }
  294.     public function hasWebmaster(): bool
  295.     {
  296.         return null !== $this->getWebmaster();
  297.     }
  298.     public function setWebmaster(?Webmaster $webmaster): self
  299.     {
  300.         $this->webmaster $webmaster;
  301.         return $this;
  302.     }
  303.     public function getWebmaster(): ?Webmaster
  304.     {
  305.         return $this->webmaster;
  306.     }
  307.     public function hasMember(): bool
  308.     {
  309.         return null != $this->getMember();
  310.     }
  311.     public function getMember(): ?Member
  312.     {
  313.         return $this->member;
  314.     }
  315.     public function setMember(?Member $member): self
  316.     {
  317.         $this->member $member;
  318.         return $this;
  319.     }
  320.     public function isAdmin(): bool
  321.     {
  322.         return $this->admin instanceof Admin;
  323.     }
  324.     public function getAdmin(): ?Admin
  325.     {
  326.         return $this->admin;
  327.     }
  328.     public function setAdmin(Admin $admin): self
  329.     {
  330.         $this->admin $admin;
  331.         // set the owning side of the relation if necessary
  332.         if ($this !== $admin->getAccount()) {
  333.             $admin->setAccount($this);
  334.         }
  335.         return $this;
  336.     }
  337.     public function isActive(): bool
  338.     {
  339.         return AccountStatus::ACTIVE === $this->status;
  340.     }
  341.     public function isNew(): bool
  342.     {
  343.         return AccountStatus::NEW === $this->status;
  344.     }
  345.     public function isBlocked(): bool
  346.     {
  347.         return AccountStatus::BLOCKED === $this->status;
  348.     }
  349.     public function isDeleted(): bool
  350.     {
  351.         return AccountStatus::DELETED === $this->status;
  352.     }
  353.     public function isConfirmed(): bool
  354.     {
  355.         return $this->member instanceof Member && $this->member->getIsActive();
  356.     }
  357.     public function setNextPassword(string $newHashstring $newSalt): self
  358.     {
  359.         $this->meta['nextPasswordHash'] = $newHash;
  360.         $this->meta['nextPasswordSalt'] = $newSalt;
  361.         $this->generateEmailToken();
  362.         return $this;
  363.     }
  364.     public function applyNextPassword(): bool
  365.     {
  366.         if (!$this->hasNextPasswordHash() || !$this->hasNextPasswordSalt()) {
  367.             return false;
  368.         }
  369.         $this->password $this->getNextPasswordHash();
  370.         $this->salt $this->getNextPasswordSalt();
  371.         $this->removeNextPassword();
  372.         $this->removeEmailToken();
  373.         return true;
  374.     }
  375.     public function getConfirmationToken(): ?string
  376.     {
  377.         if (empty($this->email_token)) {
  378.             return null;
  379.         }
  380.         return sha1($this->email.$this->email_token.self::CONFIRMATION_TOKEN_SALT);
  381.     }
  382.     private function hasNextPasswordHash(): bool
  383.     {
  384.         return isset($this->meta['nextPasswordHash']) && !empty($this->meta['nextPasswordHash']);
  385.     }
  386.     private function hasNextPasswordSalt(): bool
  387.     {
  388.         return isset($this->meta['nextPasswordSalt']) && !empty($this->meta['nextPasswordSalt']);
  389.     }
  390.     private function getNextPasswordHash(): ?string
  391.     {
  392.         return $this->meta['nextPasswordHash'] ?? null;
  393.     }
  394.     private function getNextPasswordSalt(): ?string
  395.     {
  396.         return $this->meta['nextPasswordSalt'] ?? null;
  397.     }
  398.     private function removeNextPassword(): self
  399.     {
  400.         unset($this->meta['nextPasswordHash'], $this->meta['nextPasswordSalt']);
  401.         return $this;
  402.     }
  403.     public function setNextEmail(string $nextEmail): self
  404.     {
  405.         $this->meta['nextEmail'] = $nextEmail;
  406.         $this->generateEmailToken();
  407.         return $this;
  408.     }
  409.     public function getNextEmail(): ?string
  410.     {
  411.         return $this->meta['nextEmail'] ?? null;
  412.     }
  413.     private function hasNextEmail(): bool
  414.     {
  415.         return isset($this->meta['nextEmail']) && !empty($this->meta['nextEmail']);
  416.     }
  417.     private function removeNextEmail(): self
  418.     {
  419.         unset($this->meta['nextEmail']);
  420.         $this->removeEmailToken();
  421.         return $this;
  422.     }
  423.     public function applyNextEmail(): bool
  424.     {
  425.         if (!$this->hasNextEmail()) {
  426.             return false;
  427.         }
  428.         $this->email $this->getNextEmail();
  429.         $this->removeNextEmail();
  430.         return true;
  431.     }
  432.     public function activate(): self
  433.     {
  434.         $this->setStatus(AccountStatus::ACTIVE);
  435.         $this->removeEmailToken();
  436.         return $this;
  437.     }
  438.     /**
  439.      * @return mixed
  440.      */
  441.     public function getMailProvider(): ?MailProvider {
  442.         return $this->mailProvider;
  443.     }
  444.     
  445.     /**
  446.      * @param mixed $mailProvider 
  447.      * @return self
  448.      */
  449.     public function setMailProvider(?MailProvider $mailProvider): self {
  450.         $this->mailProvider $mailProvider;
  451.         return $this;
  452.     }
  453. }