src/Entity/User.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Utils\Commons;
  4. use App\Utils\Media;
  5. use DateTime;
  6. use DateTimeInterface;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
  15. use Scheb\TwoFactorBundle\Model\BackupCodeInterface;
  16. /**
  17.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  18.  * @UniqueEntity(fields={"username"}, message="Username already exists")
  19.  * @UniqueEntity(fields={"email"}, message="Email already exists")
  20.  */
  21. class User extends Media implements UserInterfaceTwoFactorInterfaceBackupCodeInterfacePasswordAuthenticatedUserInterface
  22. {
  23.     /**
  24.      * @ORM\Id()
  25.      * @ORM\GeneratedValue()
  26.      * @ORM\Column(type="integer")
  27.      */
  28.     private $id;
  29.     /**
  30.      * @ORM\Column(type="string", length=180, unique=true)
  31.      */
  32.     private $username;
  33.     /**
  34.      * @ORM\Column(type="string", length=50, unique=true)
  35.      * @Assert\NotBlank(message="Please provide email")
  36.      * @Assert\Email(
  37.      *     message = "Please enter valid email."
  38.      * )
  39.      */
  40.     private $email;
  41.     /**
  42.      * @ORM\Column(type="string", length=50, nullable=true)
  43.      */
  44.     private $oldEmail null;
  45.     /**
  46.      * @ORM\Column(type="string", length=255, nullable=true)
  47.      */
  48.     private $emailRecoveryCode null;
  49.     /**
  50.      * @ORM\Column(type="datetime", nullable=true)
  51.      */
  52.     private $emailRecoveryCodeExpireDateTime null;
  53.     /**
  54.      * @ORM\Column(type="json")
  55.      */
  56.     private $roles;
  57.     /**
  58.      * @var string The hashed password
  59.      * @ORM\Column(type="string", nullable=true)
  60.      * @Assert\Length(max = 4096)
  61.      */
  62.     private $password;
  63.     /**
  64.      * @ORM\Column(type="string", length=30, nullable=true)
  65.      */
  66.     private $prename;
  67.     /**
  68.      * @ORM\Column(type="string", length=30, nullable=true)
  69.      */
  70.     private $surname;
  71.     /**
  72.      * @ORM\Column(type="string", length=30, nullable=true)
  73.      */
  74.     private $phone;
  75.     /**
  76.      * @ORM\Column(type="text", nullable=true)
  77.      */
  78.     private $info;
  79.     /**
  80.      * @ORM\Column(type="integer", nullable=true)
  81.      */
  82.     private $parent;
  83.     /**
  84.      * @ORM\Column(type="datetime", nullable=true)
  85.      */
  86.     protected $memberSince;
  87.     /**
  88.      * @ORM\Column(type="datetime", nullable=true)
  89.      */
  90.     protected $lastLogin;
  91.     /**
  92.      * @ORM\Column(type="integer", nullable=true)
  93.      */
  94.     protected $status;
  95.     /**
  96.      * @ORM\Column(type="string", nullable=true)
  97.      */
  98.     protected $language;
  99.     /**
  100.      * @ORM\Column(type="integer", nullable=true)
  101.      */
  102.     protected $pageLimit;
  103.     /**
  104.      * @ORM\ManyToMany(targetEntity="App\Entity\UserGroup", inversedBy="users")
  105.      */
  106.     private $userGroups;
  107.     /**
  108.      * @ORM\ManyToMany(targetEntity="App\Entity\BackendGroup", inversedBy="users")
  109.      */
  110.     private $backendGroups;
  111.     /**
  112.      * @ORM\ManyToMany(targetEntity="App\Entity\Team", inversedBy="users")
  113.      */
  114.     private $teams;
  115.     /**
  116.      * @ORM\ManyToOne(targetEntity="App\Entity\Team", inversedBy="customers")
  117.      */
  118.     private $customerTeam;
  119.     /**
  120.      * @ORM\OneToMany(targetEntity="App\Entity\FileGroup", mappedBy="user")
  121.      */
  122.     private $fileGroups;
  123.     /**
  124.      * @ORM\OneToMany(targetEntity="App\Entity\Support", mappedBy="user")
  125.      */
  126.     private $supports;
  127.     /**
  128.      * @ORM\ManyToOne(targetEntity="App\Entity\Workspace", inversedBy="users")
  129.      */
  130.     private $workspace;
  131.     /**
  132.      * @ORM\OneToOne(targetEntity="App\Entity\CustomerDetail", mappedBy="customer", cascade={"all"})
  133.      */
  134.     private $customerDetail;
  135.     /**
  136.      * @ORM\OneToMany(targetEntity="App\Entity\WishListLike", mappedBy="user")
  137.      */
  138.     private $userLikes;
  139.     /**
  140.      * @ORM\OneToMany(targetEntity="App\Entity\UserPopUpMessage", mappedBy="user",cascade={"persist","remove"} )
  141.      */
  142.     private $popUpMessages;
  143.     /**
  144.      * @ORM\Column(type="string", nullable=true)
  145.      */
  146.     private $timezone 'Europe/Berlin';
  147.     /**
  148.      * @ORM\Column(name="googleAuthenticatorSecret", type="string", nullable=true)
  149.      */
  150.     private $googleAuthenticatorSecret;
  151.     /**
  152.      * @ORM\Column(type="json")
  153.      */
  154.     private $backupCodes = [];
  155.     /**
  156.      * @ORM\Column(type="integer", options={"default": 0})
  157.      */
  158.     private $loginCounter 0;
  159.     /**
  160.      * @ORM\Column(type="boolean", options={"default" : false}))
  161.      */
  162.     private $deleted false;
  163.     /**
  164.      * @ORM\OneToOne(targetEntity="App\Entity\ResetPassword", mappedBy="user")
  165.      */
  166.     private $resetPassword;
  167.     /**
  168.      * @ORM\OneToOne(targetEntity="App\Entity\Dashboard", mappedBy="user")
  169.      */
  170.     private $dashboard;
  171.     public function __construct()
  172.     {
  173.         $this->memberSince = new DateTime();
  174.         $this->roles = [Commons::ROLE_USER];
  175.         $this->userGroups = new ArrayCollection();
  176.         $this->fileGroups = new ArrayCollection();
  177.         $this->backendGroups = new ArrayCollection();
  178.         $this->teams = new ArrayCollection();
  179.         $this->userLikes = new ArrayCollection();
  180.         $this->supports = new ArrayCollection();
  181.         $this->popUpMessages = new ArrayCollection();
  182.         $this->status 1;
  183.         $this->backupCodes = [];
  184.     }
  185.     public function __toString(): string
  186.     {
  187.         return $this->getPrename() . ' ' $this->getSurname();
  188.     }
  189.     /**
  190.      * @ORM\PostLoad
  191.      */
  192.     public function init()
  193.     {
  194.         $this->type Media::USER;
  195.     }
  196.     public function getId(): ?int
  197.     {
  198.         return $this->id;
  199.     }
  200.     /**
  201.      * A visual identifier that represents this user.
  202.      *
  203.      * @see UserInterface
  204.      */
  205.     public function getUsername(): string
  206.     {
  207.         return (string)$this->username;
  208.     }
  209.     public function setUsername(string $username): self
  210.     {
  211.         $this->username $username;
  212.         return $this;
  213.     }
  214.     /**
  215.      * @see UserInterface
  216.      */
  217.     public function getRoles(): array
  218.     {
  219.         return $this->roles;
  220.     }
  221.     public function setRoles(array $roles): self
  222.     {
  223.         $this->roles $roles;
  224.         return $this;
  225.     }
  226.     /**
  227.      * @see UserInterface
  228.      */
  229.     public function getPassword(): string
  230.     {
  231.         return (string)$this->password;
  232.     }
  233.     public function setPassword(?string $password): self
  234.     {
  235.         $this->password $password;
  236.         return $this;
  237.     }
  238.     /**
  239.      * @see UserInterface
  240.      */
  241.     public function getSalt()
  242.     {
  243.         // not needed when using the "bcrypt" algorithm in security.yaml
  244.     }
  245.     /**
  246.      * @see UserInterface
  247.      */
  248.     public function eraseCredentials()
  249.     {
  250.         // If you store any temporary, sensitive data on the user, clear it here
  251.         // $this->plainPassword = null;
  252.     }
  253.     public function getPrename(): ?string
  254.     {
  255.         return $this->prename;
  256.     }
  257.     public function setPrename(?string $prename): self
  258.     {
  259.         $this->prename $prename;
  260.         return $this;
  261.     }
  262.     public function getSurname(): ?string
  263.     {
  264.         return $this->surname;
  265.     }
  266.     public function setSurname(?string $surname): self
  267.     {
  268.         $this->surname $surname;
  269.         return $this;
  270.     }
  271.     public function getEmail(): ?string
  272.     {
  273.         return $this->email;
  274.     }
  275.     public function setEmail(string $email): self
  276.     {
  277.         $this->email $email;
  278.         return $this;
  279.     }
  280.     public function getPhone(): ?string
  281.     {
  282.         return $this->phone;
  283.     }
  284.     public function setPhone(?string $phone): self
  285.     {
  286.         $this->phone $phone;
  287.         return $this;
  288.     }
  289.     public function getMemberSince(): ?DateTimeInterface
  290.     {
  291.         return $this->memberSince;
  292.     }
  293.     public function setMemberSince(?DateTimeInterface $memberSince): self
  294.     {
  295.         $this->memberSince $memberSince;
  296.         return $this;
  297.     }
  298.     public function getLastLogin(): ?DateTimeInterface
  299.     {
  300.         return $this->lastLogin;
  301.     }
  302.     public function setLastLogin(?DateTimeInterface $lastLogin): self
  303.     {
  304.         $this->lastLogin $lastLogin;
  305.         return $this;
  306.     }
  307.     public function getInfo(): ?string
  308.     {
  309.         return $this->info;
  310.     }
  311.     public function setInfo(?string $info): self
  312.     {
  313.         $this->info $info;
  314.         return $this;
  315.     }
  316.     public function getParent(): ?string
  317.     {
  318.         return $this->parent;
  319.     }
  320.     public function setParent(?string $parent): self
  321.     {
  322.         $this->parent $parent;
  323.         return $this;
  324.     }
  325.     /**
  326.      * @return Collection|UserGroup[]
  327.      */
  328.     public function getUserGroups(): Collection
  329.     {
  330.         return $this->userGroups;
  331.     }
  332.     public function addUserGroup(UserGroup $userGroup): self
  333.     {
  334.         if (!$this->userGroups->contains($userGroup)) {
  335.             $this->userGroups[] = $userGroup;
  336.         }
  337.         return $this;
  338.     }
  339.     public function removeUserGroup(UserGroup $userGroup): self
  340.     {
  341.         if ($this->userGroups->contains($userGroup)) {
  342.             $this->userGroups->removeElement($userGroup);
  343.         }
  344.         return $this;
  345.     }
  346.     /**
  347.      * @return Collection|FileGroup[]
  348.      */
  349.     public function setFileGroups(): Collection
  350.     {
  351.         return $this->fileGroups;
  352.     }
  353.     public function addFileGroup(FileGroup $fileGroup): self
  354.     {
  355.         if (!$this->fileGroups->contains($fileGroup)) {
  356.             $this->fileGroups[] = $fileGroup;
  357.         }
  358.         return $this;
  359.     }
  360.     public function removeFileGroup(FileGroup $fileGroup): self
  361.     {
  362.         if ($this->fileGroups->contains($fileGroup)) {
  363.             $this->userGroups->removeElement($fileGroup);
  364.         }
  365.         return $this;
  366.     }
  367.     /**
  368.      * @return Collection|FileGroup[]
  369.      */
  370.     public function getFileGroups(): Collection
  371.     {
  372.         return $this->fileGroups;
  373.     }
  374.     public function getWorkspace(): ?Workspace
  375.     {
  376.         return $this->workspace;
  377.     }
  378.     public function setWorkspace(?Workspace $workspace): self
  379.     {
  380.         $this->workspace $workspace;
  381.         return $this;
  382.     }
  383.     /**
  384.      * @return Collection|BackendGroup[]
  385.      */
  386.     public function getBackendGroups(): Collection
  387.     {
  388.         return $this->backendGroups;
  389.     }
  390.     public function addBackendGroup(BackendGroup $backendGroup): self
  391.     {
  392.         if (!$this->backendGroups->contains($backendGroup)) {
  393.             $this->backendGroups[] = $backendGroup;
  394.         }
  395.         return $this;
  396.     }
  397.     public function removeBackendGroup(BackendGroup $backendGroup): self
  398.     {
  399.         if ($this->backendGroups->contains($backendGroup)) {
  400.             $this->backendGroups->removeElement($backendGroup);
  401.         }
  402.         return $this;
  403.     }
  404.     public function getStatus(): ?bool
  405.     {
  406.         return $this->status;
  407.     }
  408.     public function setStatus(?bool $status): self
  409.     {
  410.         $this->status $status;
  411.         return $this;
  412.     }
  413.     /**
  414.      * @return Collection|Team[]
  415.      */
  416.     public function getTeams(): Collection
  417.     {
  418.         return $this->teams;
  419.     }
  420.     public function addTeam(Team $team): self
  421.     {
  422.         if (!$this->teams->contains($team)) {
  423.             $this->teams[] = $team;
  424.         }
  425.         return $this;
  426.     }
  427.     public function removeTeam(Team $team): self
  428.     {
  429.         if ($this->teams->contains($team)) {
  430.             $this->teams->removeElement($team);
  431.         }
  432.         return $this;
  433.     }
  434.     public function getCustomerTeam(): ?Team
  435.     {
  436.         return $this->customerTeam;
  437.     }
  438.     public function setCustomerTeam(?Team $customerTeam): self
  439.     {
  440.         $this->customerTeam $customerTeam;
  441.         return $this;
  442.     }
  443.     public function getCustomerDetail(): ?CustomerDetail
  444.     {
  445.         return $this->customerDetail;
  446.     }
  447.     public function setCustomerDetail(?CustomerDetail $customerDetail): self
  448.     {
  449.         $this->customerDetail $customerDetail;
  450.         // set (or unset) the owning side of the relation if necessary
  451.         $newCustomer null === $customerDetail null $this;
  452.         if ($customerDetail->getCustomer() !== $newCustomer) {
  453.             $customerDetail->setCustomer($newCustomer);
  454.         }
  455.         return $this;
  456.     }
  457.     public function getLanguage(): ?string
  458.     {
  459.         return $this->language;
  460.     }
  461.     public function setLanguage(?string $language): self
  462.     {
  463.         $this->language $language;
  464.         return $this;
  465.     }
  466.     public function getPageLimit(): ?int
  467.     {
  468.         return $this->pageLimit;
  469.     }
  470.     public function setPageLimit(?int $pageLimit): self
  471.     {
  472.         $this->pageLimit $pageLimit;
  473.         return $this;
  474.     }
  475.     /**
  476.      * @param WishListLike $wishListLike
  477.      *
  478.      * @return $this
  479.      */
  480.     public function addUserLike(WishListLike $wishListLike): self
  481.     {
  482.         if (!$this->userLikes->contains($wishListLike)) {
  483.             $this->userLikes[] = $wishListLike;
  484.         }
  485.         return $this;
  486.     }
  487.     /**
  488.      * @param WishListLike $wishListLike
  489.      *
  490.      * @return $this
  491.      */
  492.     public function removeUserLike(WishListLike $wishListLike): self
  493.     {
  494.         if ($this->userLikes->contains($wishListLike)) {
  495.             $this->userLikes->removeElement($wishListLike);
  496.         }
  497.         return $this;
  498.     }
  499.     /**
  500.      * @return Collection|WishListLike[]
  501.      */
  502.     public function getUserLikes(): Collection
  503.     {
  504.         return $this->userLikes;
  505.     }
  506.     /**
  507.      * @return Collection|Support[]
  508.      */
  509.     public function getSupports(): Collection
  510.     {
  511.         return $this->supports;
  512.     }
  513.     /**
  514.      * @param Support $support
  515.      *
  516.      * @return $this
  517.      */
  518.     public function addSupport(Support $support): self
  519.     {
  520.         if (!$this->supports->contains($support)) {
  521.             $this->supports->add($support);
  522.             $support->setUser($this);
  523.         }
  524.         return $this;
  525.     }
  526.     /**
  527.      * @param Support $support
  528.      *
  529.      * @return $this
  530.      */
  531.     public function removeSupport(Support $support): self
  532.     {
  533.         if ($this->supports->contains($support)) {
  534.             $this->supports->removeElement($support);
  535.             if ($support->getWorkspace() === $this) {
  536.                 $support->setUser(null);
  537.             }
  538.         }
  539.         return $this;
  540.     }
  541.     /**
  542.      * @return string|null
  543.      */
  544.     public function getTimezone(): ?string
  545.     {
  546.         return $this->timezone;
  547.     }
  548.     /**
  549.      * @param mixed $timezone
  550.      */
  551.     public function setTimezone($timezone): void
  552.     {
  553.         $this->timezone $timezone;
  554.     }
  555.     /**
  556.      * @return mixed
  557.      */
  558.     public function getPopUpMessages()
  559.     {
  560.         return $this->popUpMessages;
  561.     }
  562.     public function isGoogleAuthenticatorEnabled(): bool
  563.     {
  564.         return $this->googleAuthenticatorSecret true false;
  565.     }
  566.     public function getGoogleAuthenticatorUsername(): string
  567.     {
  568.         return $this->username;
  569.     }
  570.     public function getGoogleAuthenticatorSecret(): ?string
  571.     {
  572.         return $this->googleAuthenticatorSecret;
  573.     }
  574.     public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
  575.     {
  576.         $this->googleAuthenticatorSecret $googleAuthenticatorSecret;
  577.     }
  578.     /**
  579.      * Check if it is a valid backup code.
  580.      *
  581.      * @param string $code
  582.      *
  583.      * @return bool
  584.      */
  585.     public function isBackupCode(string $code): bool
  586.     {
  587.         return in_array($code$this->getBackupCodes());
  588.     }
  589.     /**
  590.      * Invalidate a backup code
  591.      *
  592.      * @param string $code
  593.      */
  594.     public function invalidateBackupCode(string $code): void
  595.     {
  596.         $key array_search($code$this->getBackupCodes());
  597.         if ($key !== false) {
  598.             unset($this->backupCodes[$key]);
  599.             $codes $this->backupCodes;
  600.             $this->backupCodes array_values($codes);
  601.         }
  602.     }
  603.     /**
  604.      * Add a backup code
  605.      *
  606.      * @param string $code
  607.      */
  608.     public function addBackupCode(string $code): void
  609.     {
  610.         $key array_search($code$this->getBackupCodes());
  611.         if ($key === false) {
  612.             $codes $this->getBackupCodes();
  613.             $codes[] = $code;
  614.             $this->backupCodes array_values($codes);
  615.         }
  616.     }
  617.     /**
  618.      * @return array
  619.      */
  620.     public function getBackupCodes(): array
  621.     {
  622.         if ($this->backupCodes === null) {
  623.             $this->backupCodes = [];
  624.         }
  625.         return $this->backupCodes;
  626.     }
  627.     public function resetBackupCodes()
  628.     {
  629.         $this->backupCodes = [];
  630.     }
  631.     /**
  632.      * @param int $length
  633.      */
  634.     public function generateRandomNumber($length 32)
  635.     {
  636.         $random "";
  637.         srand((double)microtime() * 1000000);
  638.         $data "123456123456789071234567890890";
  639.         // $data .= "aBCdefghijklmn123opq45rs67tuv89wxyz"; // if you need alphabatic also
  640.         for ($i 0$i $length$i++) {
  641.             $random .= substr($data, (rand() % (strlen($data))), 1);
  642.         }
  643.         $this->addBackupCode($random);
  644.     }
  645.     /**
  646.      * @param int $length
  647.      * @return string
  648.      */
  649.     public function generateRandomPassword($length 32): string
  650.     {
  651.         $random "";
  652.         srand((double)microtime() * 1000000);
  653.         $data "123456123456789071234567890890";
  654.         $data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";
  655.         for ($i 0$i $length$i++) {
  656.             $random .= substr($data, (rand() % (strlen($data))), 1);
  657.         }
  658.         return $random;
  659.     }
  660.     /**
  661.      * @return int
  662.      */
  663.     public function getLoginCounter(): int
  664.     {
  665.         return $this->loginCounter;
  666.     }
  667.     /**
  668.      * @return int
  669.      */
  670.     public function incrementLoginCounter(): int
  671.     {
  672.         $this->loginCounter++;
  673.         return $this->loginCounter;
  674.     }
  675.     /**
  676.      * @param int $loginCounter
  677.      */
  678.     public function setLoginCounter(int $loginCounter): void
  679.     {
  680.         $this->loginCounter $loginCounter;
  681.     }
  682.     /**
  683.      * @return bool
  684.      */
  685.     public function isDeleted(): bool
  686.     {
  687.         return $this->deleted;
  688.     }
  689.     /**
  690.      * @param bool $deleted
  691.      */
  692.     public function setDeleted(bool $deleted): void
  693.     {
  694.         $this->deleted $deleted;
  695.     }
  696.     /**
  697.      * @return null|string
  698.      */
  699.     public function getOldEmail(): ?string
  700.     {
  701.         return $this->oldEmail;
  702.     }
  703.     /**
  704.      * @param string|null $oldEmail
  705.      */
  706.     public function setOldEmail(?string $oldEmail): void
  707.     {
  708.         $this->oldEmail $oldEmail;
  709.     }
  710.     /**
  711.      * @return string|null
  712.      */
  713.     public function getEmailRecoveryCode(): ?string
  714.     {
  715.         return $this->emailRecoveryCode;
  716.     }
  717.     /**
  718.      * @param string| null $emailRecoveryCode
  719.      */
  720.     public function setEmailRecoveryCode(?string $emailRecoveryCode): void
  721.     {
  722.         $this->emailRecoveryCode $emailRecoveryCode;
  723.     }
  724.     /**
  725.      * @return DateTimeInterface|null
  726.      */
  727.     public function getEmailRecoveryCodeExpireDateTime(): ?DateTimeInterface
  728.     {
  729.         return $this->emailRecoveryCodeExpireDateTime;
  730.     }
  731.     /**
  732.      * @param DateTimeInterface| null $emailRecoveryCodeExpireDateTime
  733.      */
  734.     public function setEmailRecoveryCodeExpireDateTime(?DateTimeInterface $emailRecoveryCodeExpireDateTime): void
  735.     {
  736.         $this->emailRecoveryCodeExpireDateTime $emailRecoveryCodeExpireDateTime;
  737.     }
  738.     /**
  739.      * @return  null|ResetPassword
  740.      */
  741.     public function getResetPassword(): ?ResetPassword
  742.     {
  743.         return $this->resetPassword;
  744.     }
  745.     /**
  746.      * @param null|ResetPassword $resetPassword
  747.      */
  748.     public function setResetPassword(?ResetPassword $resetPassword): void
  749.     {
  750.         $this->resetPassword $resetPassword;
  751.     }
  752.     /**
  753.      * @return mixed
  754.      */
  755.     public function getDashboard()
  756.     {
  757.         return $this->dashboard;
  758.     }
  759.     /**
  760.      * @param mixed $dashboard
  761.      */
  762.     public function setDashboard($dashboard): void
  763.     {
  764.         $this->dashboard $dashboard;
  765.     }
  766. }