src/Entity/BackUser.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Utils\Cloudinary\Media;
  4. use App\Entity\Utils\HistoryInterface;
  5. use App\Entity\Utils\Traits\TimestampTrait;
  6. use App\Repository\BackUserRepository;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Stringable;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * Class BackUser.
  15.  */
  16. #[ORM\Table(name'back_user')]
  17. #[ORM\Entity(repositoryClassBackUserRepository::class)]
  18. class BackUser implements UserInterfaceHistoryInterfacePasswordAuthenticatedUserInterfaceStringable
  19. {
  20.     use TimestampTrait;
  21.     #[ORM\Id]
  22.     #[ORM\GeneratedValue]
  23.     #[ORM\Column(typeTypes::INTEGER)]
  24.     private ?int $id null;
  25.     #[ORM\Column(typeTypes::STRING)]
  26.     #[Assert\NotBlank(message'form.error.required'groups: ['edit''create'])]
  27.     #[Assert\Length(min2max255minMessage'form.error.short'maxMessage'form.error.long'groups: ['edit''create'])]
  28.     private string $fullName;
  29.     #[ORM\Column(typeTypes::STRINGuniquetrue)]
  30.     #[Assert\NotBlank(message'form.error.required'groups: ['create'])]
  31.     #[Assert\Length(min2max255minMessage'form.error.short'maxMessage'form.error.long'groups: ['create'])]
  32.     #[Assert\Regex(pattern'/^[a-z0-9](-?[a-z0-9]+)*$/i'message'form.error.username', match: truegroups: ['create'])]
  33.     private string $username;
  34.     #[Assert\Valid]
  35.     #[ORM\OneToOne(targetEntityMedia::class, cascade: ['all'])]
  36.     #[ORM\JoinColumn(name'image_id'onDelete'cascade')]
  37.     protected ?Media $image null;
  38.     #[ORM\Column(typeTypes::STRINGuniquetrue)]
  39.     #[Assert\NotBlank(message'form.error.required'groups: ['edit''create'])]
  40.     #[Assert\Length(min2max255minMessage'form.error.short'maxMessage'form.error.long'groups: ['edit''create'])]
  41.     #[Assert\Email(message'The email {{ value }} is not a valid email.'groups: ['edit''create'])]
  42.     protected string $email;
  43.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  44.     protected ?string $preferredLocale null;
  45.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  46.     protected ?string $preferredLanguage null;
  47.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  48.     protected ?string $timezone null;
  49.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  50.     protected ?string $theme null;
  51.     #[ORM\Column(typeTypes::STRING)]
  52.     protected string $password;
  53.     /**
  54.      * @var ?string
  55.      */
  56.     #[Assert\NotBlank(message'form.error.required'groups: ['create'])]
  57.     #[Assert\Regex(pattern'/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d\/@$!%*#?&]{8,4096}$/'message'form.error.password', match: truegroups: ['create'])]
  58.     protected ?string $plainPassword null;
  59.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  60.     protected ?string $resetPasswordHash null;
  61.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  62.     protected ?string $unlockingHash null;
  63.     #[ORM\Column(typeTypes::JSON)]
  64.     protected array $roles = [];
  65.     #[ORM\Column(typeTypes::JSON)]
  66.     protected array $notifications = [];
  67.     /**
  68.      * Auto Save contents.
  69.      */
  70.     #[ORM\Column(typeTypes::BOOLEAN)]
  71.     protected bool $autoSave true;
  72.     /**
  73.      * Enabled.
  74.      */
  75.     #[ORM\Column(typeTypes::BOOLEAN)]
  76.     protected bool $enabled true;
  77.     /**
  78.      * Excerpt.
  79.      */
  80.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  81.     #[Assert\Length(min1max5000minMessage'form.error.short'maxMessage'form.error.long')]
  82.     protected ?string $excerpt null;
  83.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  84.     protected ?\DateTime $lastConnection null;
  85.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  86.     protected ?string $lastIp null;
  87.     public function __toString(): string
  88.     {
  89.         return $this->fullName;
  90.     }
  91.     public function getId(): ?int
  92.     {
  93.         return $this->id;
  94.     }
  95.     public function setFullName(string $fullName): void
  96.     {
  97.         $this->fullName $fullName;
  98.     }
  99.     public function getFullName(): ?string
  100.     {
  101.         return $this->fullName;
  102.     }
  103.     public function getUsername(): ?string
  104.     {
  105.         return $this->username;
  106.     }
  107.     public function setUsername(string $username): void
  108.     {
  109.         $this->username $username;
  110.     }
  111.     public function getEmail(): ?string
  112.     {
  113.         return $this->email;
  114.     }
  115.     public function setEmail(string $email): void
  116.     {
  117.         $this->email $email;
  118.     }
  119.     public function getPreferredLocale(): ?string
  120.     {
  121.         return $this->preferredLocale;
  122.     }
  123.     public function setPreferredLocale(?string $preferredLocale null): void
  124.     {
  125.         $this->preferredLocale $preferredLocale;
  126.     }
  127.     public function getPreferredLanguage(): ?string
  128.     {
  129.         return $this->preferredLanguage;
  130.     }
  131.     public function setPreferredLanguage(?string $preferredLanguage null): void
  132.     {
  133.         $this->preferredLanguage $preferredLanguage;
  134.     }
  135.     public function getTimezone(): ?string
  136.     {
  137.         return $this->timezone;
  138.     }
  139.     public function setTimezone(string $timezone): void
  140.     {
  141.         $this->timezone $timezone;
  142.     }
  143.     public function getTheme(): ?string
  144.     {
  145.         return $this->theme;
  146.     }
  147.     public function setTheme(string $theme): void
  148.     {
  149.         $this->theme $theme;
  150.     }
  151.     public function getPassword(): ?string
  152.     {
  153.         return $this->password;
  154.     }
  155.     public function setPassword(string $password): void
  156.     {
  157.         $this->plainPassword null;
  158.         $this->password      $password;
  159.     }
  160.     public function getPlainPassword(): ?string
  161.     {
  162.         return $this->plainPassword;
  163.     }
  164.     public function setPlainPassword(?string $plainPassword null): void
  165.     {
  166.         $this->plainPassword $plainPassword;
  167.     }
  168.     public function getResetPasswordHash(): ?string
  169.     {
  170.         return $this->resetPasswordHash;
  171.     }
  172.     public function setResetPasswordHash(string $resetPasswordHash null): void
  173.     {
  174.         $this->resetPasswordHash $resetPasswordHash;
  175.     }
  176.     public function generateResetPasswordHash(): void
  177.     {
  178.         try {
  179.             $hash md5($this->getId().'_'.date_timestamp_get(new \DateTime()).'_'.uniqid());
  180.             $this->setResetPasswordHash($hash);
  181.         } catch (\Exception) {
  182.         }
  183.     }
  184.     public function getUnlockingHash(): ?string
  185.     {
  186.         return $this->unlockingHash;
  187.     }
  188.     public function setUnlockingHash(string $unlockingHash null): void
  189.     {
  190.         $this->unlockingHash $unlockingHash;
  191.     }
  192.     public function generateUnlockingHash(): void
  193.     {
  194.         try {
  195.             $hash md5($this->getId().'_'.date_timestamp_get(new \DateTime()).'_'.uniqid());
  196.             $this->setUnlockingHash($hash);
  197.         } catch (\Exception) {
  198.         }
  199.     }
  200.     public static function getRolesString(): string
  201.     {
  202.         return 'ROLE_SUPER_ADMIN,ROLE_ADMIN,ROLE_REDACTOR,ROLE_READER';
  203.     }
  204.     public function getRoles(): array
  205.     {
  206.         $roles $this->roles;
  207.         if (empty($roles)) {
  208.             $roles[] = 'ROLE_USER';
  209.         }
  210.         return array_unique($roles);
  211.     }
  212.     public function setRoles(array $roles): void
  213.     {
  214.         $this->roles $roles;
  215.     }
  216.     public function getSalt(): ?string
  217.     {
  218.         return null;
  219.     }
  220.     public function eraseCredentials(): void
  221.     {
  222.     }
  223.     public function getInitials(): ?string
  224.     {
  225.         $initials = [];
  226.         $nameParts explode(' '$this->fullName);
  227.         foreach ($nameParts as $part) {
  228.             $initials[] = substr($part01);
  229.         }
  230.         return implode(''$initials);
  231.     }
  232.     public function getImage(): ?Media
  233.     {
  234.         return $this->image;
  235.     }
  236.     public function setImage(?Media $image): void
  237.     {
  238.         $this->image $image;
  239.     }
  240.     public function getNotifications(): ?array
  241.     {
  242.         return $this->notifications;
  243.     }
  244.     public function setNotifications(array $notifications): void
  245.     {
  246.         $this->notifications $notifications;
  247.     }
  248.     public function emptyNotifications(): void
  249.     {
  250.         $this->notifications = [];
  251.     }
  252.     public function getAutoSave(): bool
  253.     {
  254.         return $this->autoSave;
  255.     }
  256.     public function setAutoSave(bool $autoSave): void
  257.     {
  258.         $this->autoSave $autoSave;
  259.     }
  260.     public function switchAutoSave(): void
  261.     {
  262.         $this->autoSave = !$this->autoSave;
  263.     }
  264.     public function getEnabled(): bool
  265.     {
  266.         return $this->enabled;
  267.     }
  268.     public function setEnabled(bool $enabled): void
  269.     {
  270.         $this->enabled $enabled;
  271.     }
  272.     public function getExcerpt(): ?string
  273.     {
  274.         return $this->excerpt;
  275.     }
  276.     public function setExcerpt(?string $excerpt): void
  277.     {
  278.         $this->excerpt $excerpt;
  279.     }
  280.     public function getLastConnection(): ?\DateTime
  281.     {
  282.         return $this->lastConnection;
  283.     }
  284.     public function setLastConnection(?\DateTime $lastConnection): void
  285.     {
  286.         $this->lastConnection $lastConnection;
  287.     }
  288.     public function getLastIp(): ?string
  289.     {
  290.         return $this->lastIp;
  291.     }
  292.     public function setLastIp(?string $lastIp): void
  293.     {
  294.         $this->lastIp $lastIp;
  295.     }
  296.     /*public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array
  297.     {
  298.         $image = null;
  299.         if ($this->image && !empty($this->image->getSingleMedia()) && $this->image->getSingleMedia()->getPublicId() != null) {
  300.             $image = $this->image->getSingleMedia()->getPublicId();
  301.         }
  302.         return [
  303.             'name'     => $this->fullName,
  304.             'username' => $this->username,
  305.             'email'    => $this->email,
  306.             'roles'    => $this->roles,
  307.             'lastIp'   => $this->lastIp,
  308.             'image'    => $image,
  309.             'url'      => '/back/profile/' . $this->id,
  310.         ];
  311.     }*/
  312.     public function getEntityType(): ?string
  313.     {
  314.         return 'back_user';
  315.     }
  316.     public function getUserIdentifier(): string
  317.     {
  318.         return $this->username;
  319.     }
  320.     // PHP 8.1 and higher
  321.     public function __serialize(): array
  322.     {
  323.         return [
  324.             'id'       => $this->id,
  325.             'username' => $this->username,
  326.             'password' => $this->password,
  327.         ];
  328.     }
  329.     // PHP 8.1 and higher
  330.     public function __unserialize(array $data): void
  331.     {
  332.         $this->id       $data['id'];
  333.         $this->username $data['username'];
  334.         $this->password $data['password'];
  335.     }
  336.     /*// PHP 8.0 and lower and will be remove in PHP 9
  337.     public function serialize(): string
  338.     {
  339.         return serialize([$this->id, $this->username, $this->password]);
  340.     }
  341.     // PHP 8.0 and lower and will be remove in PHP 9
  342.     public function unserialize($data): void
  343.     {
  344.         [$this->id, $this->username, $this->password] = unserialize($data, ['allowed_classes' => false]);
  345.     }*/
  346. }