src/Entity/User.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use Serializable;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. /**
  10.  * App\Entity\User
  11.  *
  12.  * @ORM\Table(name="users")
  13.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  14.  */
  15. class User implements UserInterfaceSerializable
  16. {
  17.     /**
  18.      * @ORM\Column(type="integer")
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue(strategy="AUTO")
  21.      */
  22.     private int $id;
  23.     /**
  24.      * @ORM\Column(type="string", length=25, unique=true)
  25.      */
  26.     private string $username;
  27.     /**
  28.      * @var null|string
  29.      * @Gedmo\Slug(fields={"username"}, updatable=false)
  30.      * @ORM\Column(name="slug", type="string", length=255, unique=true, nullable=true)
  31.      */
  32.     private ?string $slug;
  33.     /**
  34.      * @ORM\Column(type="string", length=32, nullable=true)
  35.      */
  36.     private ?string $salt;
  37.     /**
  38.      * @ORM\Column(type="string", length=128, nullable=true)
  39.      */
  40.     private ?string $password;
  41.     /**
  42.      *
  43.      * @Assert\Regex(
  44.      *      pattern="/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/",
  45.      *      message="Please user at least one upper case letter, one lower case letter, and one number"
  46.      * )
  47.      */
  48.     private $plainPassword;
  49.     /**
  50.      * @ORM\Column(type="string", length=60, unique=true, nullable=true)
  51.      */
  52.     private ?string $email;
  53.     /**
  54.      * @ORM\Column(name="is_active", type="boolean")
  55.      */
  56.     private bool $isActive;
  57.     /**
  58.      * @ORM\Column(name="roles", type="json")
  59.      */
  60.     private ?array $roles = [];
  61.     /**
  62.      * @Gedmo\Timestampable(on="create")
  63.      * @ORM\Column(type="datetime")
  64.      */
  65.     private ?DateTime $created;
  66.     /**
  67.      * @Gedmo\Timestampable(on="update")
  68.      * @ORM\Column(type="datetime")
  69.      */
  70.     private ?DateTime $updated;
  71.     /**
  72.      * @Gedmo\Blameable(on="create")
  73.      * @ORM\ManyToOne(targetEntity="User")
  74.      * @ORM\JoinColumn(name="created_by", referencedColumnName="id", onDelete="set null")
  75.      */
  76.     private ?User $createdBy;
  77.     /**
  78.      * @Gedmo\Blameable(on="update")
  79.      * @ORM\ManyToOne(targetEntity="User")
  80.      * @ORM\JoinColumn(name="updated_by", referencedColumnName="id", onDelete="set null")
  81.      */
  82.     private ?User $updatedBy;
  83.     /**
  84.      * @ORM\OneToOne(targetEntity="App\Entity\People", mappedBy="user")
  85.      **/
  86.     private ?People $person;
  87.     public function __construct()
  88.     {
  89.         $this->isActive true;
  90.         $this->salt md5(uniqid(nulltrue));
  91.         $this->roles = [];
  92.     }
  93.     public function getUsername(): string
  94.     {
  95.         return $this->username;
  96.     }
  97.     public function getSalt(): ?string
  98.     {
  99.         return $this->salt;
  100.     }
  101.     public function getPassword(): ?string
  102.     {
  103.         return $this->password;
  104.     }
  105.     public function eraseCredentials()
  106.     {
  107.         // If you store any temporary, sensitive data on the user, clear it here
  108.          $this->plainPassword null;
  109.     }
  110.     /** @see Serializable::serialize */
  111.     public function serialize(): ?string
  112.     {
  113.         return serialize([
  114.             $this->id,
  115.             $this->username,
  116.             $this->password,
  117.             $this->salt,
  118.             $this->isActive,
  119.         ]);
  120.     }
  121.     /** @see Serializable::unserialize */
  122.     public function unserialize($data)
  123.     {
  124.         [
  125.             $this->id,
  126.             $this->username,
  127.             $this->password,
  128.             $this->salt,
  129.             $this->isActive
  130.             ] = unserialize($data);
  131.     }
  132.     public function getId(): int
  133.     {
  134.         return $this->id;
  135.     }
  136.     public function setUsername(string $username): User
  137.     {
  138.         $this->username $username;
  139.         return $this;
  140.     }
  141.     public function setSalt(?string $salt): User
  142.     {
  143.         $this->salt $salt;
  144.         return $this;
  145.     }
  146.     public function setPassword(?string $password): User
  147.     {
  148.         $this->password $password;
  149.         return $this;
  150.     }
  151.     public function setEmail(?string $email): User
  152.     {
  153.         $this->email $email;
  154.         return $this;
  155.     }
  156.     public function getEmail(): ?string
  157.     {
  158.         return $this->email;
  159.     }
  160.     public function setIsActive(bool $isActive): User
  161.     {
  162.         $this->isActive $isActive;
  163.         return $this;
  164.     }
  165.     public function getIsActive(): bool
  166.     {
  167.         return $this->isActive;
  168.     }
  169.     public function getCreatedBy(): ?User
  170.     {
  171.         return $this->createdBy;
  172.     }
  173.     public function getUpdatedBy(): ?User
  174.     {
  175.         return $this->updatedBy;
  176.     }
  177.     public function setCreatedBy(?User $createdBy null): ?User
  178.     {
  179.         $this->createdBy $createdBy;
  180.         return $this;
  181.     }
  182.     public function setUpdatedBy(?User $updatedBy null): ?User
  183.     {
  184.         $this->updatedBy $updatedBy;
  185.         return $this;
  186.     }
  187.     public function setSlug($slug)
  188.     {
  189.         $this->slug $slug;
  190.     }
  191.     public function getSlug(): ?string
  192.     {
  193.         return $this->slug;
  194.     }
  195.     public function setCreated(?DateTime $created): User
  196.     {
  197.         $this->created $created;
  198.         return $this;
  199.     }
  200.     public function getCreated(): ?DateTime
  201.     {
  202.         return $this->created;
  203.     }
  204.     public function setUpdated(?DateTime $updated): User
  205.     {
  206.         $this->updated $updated;
  207.         return $this;
  208.     }
  209.     public function getUpdated(): ?DateTime
  210.     {
  211.         return $this->updated;
  212.     }
  213.     public function setRoles(?array $roles = []): User
  214.     {
  215.         $this->roles $roles;
  216.         return $this;
  217.     }
  218.     public function getRoles(): array
  219.     {
  220.         $roles $this->roles;
  221.         // guarantee every user at least has ROLE_USER
  222.         $roles[] = 'ROLE_USER';
  223.         $roles[] = 'ROLE_ADMIN';
  224.         $roles[] = 'ROLE_EDITOR';
  225.         $roles[] = 'ROLE_SUPER_ADMIN';
  226. //        $roles[] = 'ROLE_BLOGGER';
  227.         $roles[] = 'ROLE_PUBLISHER';
  228.         return array_unique($roles);
  229.     }
  230.     /**
  231.      * @return mixed
  232.      */
  233.     public function getPlainPassword()
  234.     {
  235.         return $this->plainPassword;
  236.     }
  237.     /**
  238.      * @param $plainPassword
  239.      */
  240.     public function setPlainPassword($plainPassword)
  241.     {
  242.         $this->plainPassword $plainPassword;
  243.     }
  244.     public function setPerson(?People $person null): User
  245.     {
  246.         $this->person $person;
  247.         return $this;
  248.     }
  249.     public function getPerson(): ?People
  250.     {
  251.         return $this->person;
  252.     }
  253.     public function isAccountNonExpired(): bool
  254.     {
  255.         return true;
  256.     }
  257.     public function isAccountNonLocked(): bool
  258.     {
  259.         return true;
  260.     }
  261.     public function isCredentialsNonExpired(): bool
  262.     {
  263.         return true;
  264.     }
  265.     public function isEnabled(): bool
  266.     {
  267.         return $this->isActive;
  268.     }
  269. }