src/Entity/User.php line 30

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license Silk Software House Sp. Z O. O.
  4.  */
  5. declare(strict_types=1);
  6. namespace App\Entity;
  7. use App\Entity\Traits\IdTrait;
  8. use App\Entity\Traits\TimePreUpdateTrait;
  9. use App\Entity\Traits\TimeTrait;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use DateTime;
  14. use Exception;
  15. use App\Validator\Constraints as AppAssert;
  16. /**
  17.  * Class User.
  18.  *
  19.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  20.  * @ORM\Table(name="users")
  21.  * @ORM\HasLifecycleCallbacks()
  22.  *
  23.  * @UniqueEntity(fields={"email"}, repositoryMethod="findCaseInsensitiveByEmail")
  24.  * @UniqueEntity(fields={"username"}, repositoryMethod="findCaseInsensitiveByUsername")
  25.  */
  26. class User implements UserInterface
  27. {
  28.     use IdTrait;
  29.     use TimeTrait;
  30.     use TimePreUpdateTrait;
  31.     /**
  32.      * Username.
  33.      *
  34.      * @var string
  35.      *
  36.      * @ORM\Column(name="username", type="text", unique=true)
  37.      */
  38.     private string $username '';
  39.     /**
  40.      * Email.
  41.      *
  42.      * @ORM\Column(name="email", type="string", length=180, unique=true)
  43.      */
  44.     private string $email '';
  45.     /**
  46.      * Roles.
  47.      *
  48.      * @ORM\Column(name="roles", type="json")
  49.      */
  50.     private array $roles = [];
  51.     /**
  52.      * Password.
  53.      *
  54.      * @var string The hashed password
  55.      *
  56.      * @ORM\Column(name="password", type="string")
  57.      */
  58.     private string $password '';
  59.     /**
  60.      * Customer.
  61.      *
  62.      * @var Customer|null
  63.      *
  64.      * @AppAssert\ValidCustomerForUser()
  65.      *
  66.      * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="users", cascade={"persist"})
  67.      * @ORM\JoinColumn(name="customer_id", nullable=true)
  68.      */
  69.     private ?Customer $customer null;
  70.     /**
  71.      * Hash used to change password.
  72.      *
  73.      * @var string|null
  74.      *
  75.      * @ORM\Column(name="hash", type="text", nullable=true, options={
  76.      *     "comment": "Hash used to generate password resetting url."
  77.      * })
  78.      */
  79.     private ?string $hash null;
  80.     /**
  81.      * Time to which hash will be active.
  82.      *
  83.      * @var DateTime|null
  84.      *
  85.      * @ORM\Column(name="hash_validity_ends_at", type="datetime", nullable=true, options={
  86.      *     "comment": "Hash validity ends at.",
  87.      * })
  88.      */
  89.     private ?DateTime $hashValidityEndsAt null;
  90.     /**
  91.      * Time when this user was activated.
  92.      *
  93.      * @var DateTime|null
  94.      *
  95.      * @ORM\Column(name="activated_at", type="datetime", nullable=true, options={
  96.      *     "comment": "Activated at.",
  97.      * })
  98.      */
  99.     private ?DateTime $activatedAt null;
  100.     /**
  101.      * Time when the user was blocked.
  102.      *
  103.      * @var DateTime|null
  104.      *
  105.      * @ORM\Column(name="blocked_at", type="datetime", nullable=true, options={
  106.      *     "comment": "Blocked at.",
  107.      * })
  108.      */
  109.     private ?DateTime $blockedAt null;
  110.     /**
  111.      * User setting.
  112.      *
  113.      * @var UserSetting|null
  114.      *
  115.      * @ORM\OneToOne(targetEntity="App\Entity\UserSetting", mappedBy="user")
  116.      */
  117.     private ?UserSetting $setting null;
  118.     /**
  119.      * Phone.
  120.      *
  121.      * @var string|null
  122.      *
  123.      * @ORM\Column(name="phone", type="text", nullable=true, options={
  124.      *     "comment": "Phone."
  125.      * })
  126.      */
  127.     private ?string $phone null;
  128.     /**
  129.      * Surname.
  130.      *
  131.      * @var string|null
  132.      *
  133.      * @ORM\Column(name="surname", type="text", nullable=true, options={
  134.      *     "comment": "Surname."
  135.      * })
  136.      */
  137.     private ?string $surname null;
  138.     /**
  139.      * First name.
  140.      *
  141.      * @var string|null
  142.      *
  143.      * @ORM\Column(name="first_name", type="text", nullable=true, options={
  144.      *     "comment": "First name."
  145.      * })
  146.      */
  147.     private ?string $firstName null;
  148.     /**
  149.      * Get email.
  150.      *
  151.      * @return string|null
  152.      */
  153.     public function getEmail(): ?string
  154.     {
  155.         return $this->email;
  156.     }
  157.     /**
  158.      * Set email.
  159.      *
  160.      * @param string $email
  161.      *
  162.      * @return User
  163.      */
  164.     public function setEmail(string $email): self
  165.     {
  166.         $this->email $email;
  167.         return $this;
  168.     }
  169.     /**
  170.      * A visual identifier that represents this user.
  171.      *
  172.      * @return string
  173.      *
  174.      * @see UserInterface
  175.      */
  176.     public function getUsername(): string
  177.     {
  178.         return $this->username;
  179.     }
  180.     /**
  181.      * Get roles.
  182.      *
  183.      * @return array
  184.      *
  185.      * @see UserInterface
  186.      */
  187.     public function getRoles(): array
  188.     {
  189.         $roles $this->roles;
  190.         // guarantee every user at least has ROLE_USER
  191.         $roles[] = 'ROLE_USER';
  192.         return array_unique($roles);
  193.     }
  194.     /**
  195.      * Set roles.
  196.      *
  197.      * @param array $roles
  198.      *
  199.      * @return User
  200.      */
  201.     public function setRoles(array $roles): self
  202.     {
  203.         $this->roles $roles;
  204.         return $this;
  205.     }
  206.     /**
  207.      * Get roles extended.
  208.      *
  209.      * @return string
  210.      */
  211.     public function getRoleSingle(): string
  212.     {
  213.         if ($this->hasRole('ROLE_ADMIN')) {
  214.             return 'ROLE_ADMIN';
  215.         }
  216.         if ($this->hasRole('ROLE_CUSTOMER')) {
  217.             return 'ROLE_CUSTOMER';
  218.         }
  219.         return 'ROLE_USER';
  220.     }
  221.     /**
  222.      * Set role single.
  223.      *
  224.      * @param string $role
  225.      *
  226.      * @return User
  227.      */
  228.     public function setRoleSingle(string $role): self
  229.     {
  230.         return $this->setRoles([$role]);
  231.     }
  232.     /**
  233.      * Has role.
  234.      *
  235.      * @param string $role
  236.      *
  237.      * @return bool
  238.      */
  239.     public function hasRole(string $role): bool
  240.     {
  241.         return in_array($role$this->getRoles());
  242.     }
  243.     /**
  244.      * Get password.
  245.      *
  246.      * @see UserInterface
  247.      *
  248.      * @return string
  249.      */
  250.     public function getPassword(): string
  251.     {
  252.         return $this->password;
  253.     }
  254.     /**
  255.      * Set password.
  256.      *
  257.      * @param string $password
  258.      *
  259.      * @return User
  260.      */
  261.     public function setPassword(string $password): self
  262.     {
  263.         $this->password $password;
  264.         return $this;
  265.     }
  266.     /**
  267.      * @see UserInterface
  268.      */
  269.     public function eraseCredentials()
  270.     {
  271.         // If you store any temporary, sensitive data on the user, clear it here
  272.         // $this->plainPassword = null;
  273.     }
  274.     /**
  275.      * Get customer.
  276.      *
  277.      * @return Customer|null
  278.      */
  279.     public function getCustomer(): ?Customer
  280.     {
  281.         return $this->customer;
  282.     }
  283.     /**
  284.      * Set customer.
  285.      *
  286.      * @param Customer|null $customer
  287.      *
  288.      * @return User
  289.      */
  290.     public function setCustomer(?Customer $customer): self
  291.     {
  292.         $this->customer $customer;
  293.         return $this;
  294.     }
  295.     /**
  296.      * @inheritDoc
  297.      */
  298.     public function getSalt()
  299.     {
  300.         // TODO: Implement getSalt() method.
  301.     }
  302.     /**
  303.      * Get hash.
  304.      *
  305.      * @return string|null
  306.      */
  307.     public function getHash(): ?string
  308.     {
  309.         return $this->hash;
  310.     }
  311.     /**
  312.      * Set hash.
  313.      *
  314.      * @param string|null $hash
  315.      *
  316.      * @return User
  317.      */
  318.     public function setHash(?string $hash): self
  319.     {
  320.         $this->hash $hash;
  321.         return $this;
  322.     }
  323.     /**
  324.      * Get hash validity ends at.
  325.      *
  326.      * @return DateTime|null
  327.      */
  328.     public function getHashValidityEndsAt(): ?DateTime
  329.     {
  330.         return $this->hashValidityEndsAt;
  331.     }
  332.     /**
  333.      * Set hash validity ends at.
  334.      *
  335.      * @param DateTime|null $hashValidityEndsAt
  336.      *
  337.      * @return User
  338.      */
  339.     public function setHashValidityEndsAt(?DateTime $hashValidityEndsAt): self
  340.     {
  341.         $this->hashValidityEndsAt $hashValidityEndsAt;
  342.         return $this;
  343.     }
  344.     /**
  345.      * Start the password resetting process.
  346.      *
  347.      * @return User
  348.      */
  349.     public function startPasswordResettingProcess(): self
  350.     {
  351.         $this->hash md5(uniqid((string)mt_rand(), true));
  352.         try {
  353.             $this->hashValidityEndsAt = new DateTime('+30min');
  354.         } catch (Exception $e) {
  355.         }
  356.         return $this;
  357.     }
  358.     /**
  359.      * Can password resetting be triggered?
  360.      *
  361.      * @return bool
  362.      */
  363.     public function canPasswordResettingBeTriggered(): bool
  364.     {
  365.         try {
  366.             return !$this->hashValidityEndsAt || $this->hashValidityEndsAt < new DateTime();
  367.         } catch (Exception $e) {
  368.         }
  369.         return false;
  370.     }
  371.     /**
  372.      * Is hash correct?
  373.      *
  374.      * @param string|null $hash
  375.      *
  376.      * @return bool
  377.      */
  378.     public function getIsHashCorrect(?string $hash): bool
  379.     {
  380.         try {
  381.             return $hash && $hash ===  $this->hash && $this->hashValidityEndsAt >= new DateTime();
  382.         } catch (Exception $e) {
  383.         }
  384.         return false;
  385.     }
  386.     /**
  387.      * Get activated at.
  388.      *
  389.      * @return DateTime|null
  390.      */
  391.     public function getActivatedAt(): ?DateTime
  392.     {
  393.         return $this->activatedAt;
  394.     }
  395.     /**
  396.      * Set activated at.
  397.      *
  398.      * @param DateTime|null $activatedAt
  399.      *
  400.      * @return User
  401.      */
  402.     public function setActivatedAt(?DateTime $activatedAt): self
  403.     {
  404.         $this->activatedAt $activatedAt;
  405.         return $this;
  406.     }
  407.     /**
  408.      * Get blocked at.
  409.      *
  410.      * @return DateTime|null
  411.      */
  412.     public function getBlockedAt(): ?DateTime
  413.     {
  414.         return $this->blockedAt;
  415.     }
  416.     /**
  417.      * Set blocked at.
  418.      *
  419.      * @param DateTime|null $blockedAt
  420.      *
  421.      * @return User
  422.      */
  423.     public function setBlockedAt(?DateTime $blockedAt): self
  424.     {
  425.         $this->blockedAt $blockedAt;
  426.         return $this;
  427.     }
  428.     /**
  429.      * Set username.
  430.      *
  431.      * @param string $username
  432.      *
  433.      * @return User
  434.      */
  435.     public function setUsername(string $username): self
  436.     {
  437.         $this->username $username;
  438.         return $this;
  439.     }
  440.     /**
  441.      * Get active customer.
  442.      *
  443.      * @return Customer|null
  444.      */
  445.     public function getActiveCustomer(): ?Customer
  446.     {
  447.         return $this->customer && (!$this->customer->getActiveTo() || $this->customer->getActiveTo() > new DateTime())
  448.             ? $this->customer null;
  449.     }
  450.     /**
  451.      * To string.
  452.      *
  453.      * @return string
  454.      */
  455.     public function __toString()
  456.     {
  457.         return $this->username ?? '';
  458.     }
  459.     /**
  460.      * Belongs to.
  461.      *
  462.      * @param User $user
  463.      *
  464.      * @return bool
  465.      */
  466.     public function belongsTo(self $user): bool
  467.     {
  468.         if ($user->hasRole('ROLE_ADMIN')) {
  469.             return true;
  470.         }
  471.         if (!$user->hasRole('ROLE_CUSTOMER') || !$this->getActiveCustomer() || !$user->getActiveCustomer()) {
  472.             return false;
  473.         }
  474.         return $user->getActiveCustomer()->getId() === $this->getActiveCustomer()->getId();
  475.     }
  476.     /**
  477.      * Get user setting.
  478.      *
  479.      * @return UserSetting|null
  480.      */
  481.     public function getSetting(): ?UserSetting
  482.     {
  483.         return $this->setting;
  484.     }
  485.     /**
  486.      * @return int[]
  487.      */
  488.     public function getUsedDevicesIds(): array
  489.     {
  490.         return $this->setting $this->setting->getUsedDevices()->map(function (Device $device) {
  491.             return $device->getId();
  492.         })->toArray() : [];
  493.     }
  494.     /**
  495.      * Set user setting.
  496.      *
  497.      * @param UserSetting|null $setting
  498.      *
  499.      * @return User
  500.      */
  501.     public function setSetting(?UserSetting $setting): self
  502.     {
  503.         $this->setting $setting;
  504.         return $this;
  505.     }
  506.     /**
  507.      * Get phone.
  508.      *
  509.      * @return string|null
  510.      */
  511.     public function getPhone(): ?string
  512.     {
  513.         return $this->phone;
  514.     }
  515.     /**
  516.      * Set phone.
  517.      *
  518.      * @param string|null $phone
  519.      *
  520.      * @return User
  521.      */
  522.     public function setPhone(?string $phone): self
  523.     {
  524.         $this->phone $phone;
  525.         return $this;
  526.     }
  527.     /**
  528.      * Get surname.
  529.      *
  530.      * @return string|null
  531.      */
  532.     public function getSurname(): ?string
  533.     {
  534.         return $this->surname;
  535.     }
  536.     /**
  537.      * Set surname.
  538.      *
  539.      * @param string|null $surname
  540.      *
  541.      * @return User
  542.      */
  543.     public function setSurname(?string $surname): self
  544.     {
  545.         $this->surname $surname;
  546.         return $this;
  547.     }
  548.     /**
  549.      * Get first name.
  550.      *
  551.      * @return string|null
  552.      */
  553.     public function getFirstName(): ?string
  554.     {
  555.         return $this->firstName;
  556.     }
  557.     /**
  558.      * Set the first name.
  559.      *
  560.      * @param string|null $firstName
  561.      *
  562.      * @return User
  563.      */
  564.     public function setFirstName(?string $firstName): self
  565.     {
  566.         $this->firstName $firstName;
  567.         return $this;
  568.     }
  569.     /**
  570.      * Get a standard recipe.
  571.      *
  572.      * @return Recipe|null
  573.      */
  574.     public function getStandardRecipe(): ?Recipe
  575.     {
  576.         return $this->getSetting() ? $this->getSetting()->getStandardRecipe() : null;
  577.     }
  578.     /**
  579.      * Sanitize
  580.      *
  581.      * @return void
  582.      */
  583.     public function normalize(): void
  584.     {
  585.         if ($this->username) {
  586.             $this->username strtolower($this->username);
  587.         }
  588.         if ($this->email) {
  589.             $this->email strtolower($this->email);
  590.         }
  591.     }
  592. }