permanaj

User entity

Sep 13th, 2019
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.29 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Entity;
  4.  
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10.  
  11. /**
  12.  * @ApiResource(
  13.  *     attributes={
  14.  *          "normalization_context"={"get"}
  15.  *     },
  16.  *     collectionOperations={
  17.  *          "post"={
  18.  *              "denormalozation_context"={"groups"={"write"}},
  19.  *              "normalization_context"={"groups"={"read"}},
  20.  *              "validation_groups"={"write"},
  21.  *              "access_control"="is_granted('IS_AUTHENTICATED_FULLY')"
  22.  *          },
  23.  *          "register"={
  24.  *              "method"="POST",
  25.  *              "path"="/users/register",
  26.  *              "denormalozation_context"={"groups"={"register"}},
  27.  *              "normalization_context"={"groups"={"read"}},
  28.  *              "validation_groups"={"register"},
  29.  *              "swagger_context"={
  30.  *                  "summary"="Register a user",
  31.  *                  "description"="For anonymous user to register an account."
  32.  *              }
  33.  *          }
  34.  *     }
  35.  * )
  36.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  37.  */
  38. class User implements UserInterface
  39. {
  40.     const ROLE_USER = 'ROLE_USER';
  41.     const ROLE_WEBADMIN = 'ROLE_WEBADMIN';
  42.     const ROLE_SUPERADMIN = 'ROLE_SUPERADMIN';
  43.  
  44.     const DEFAULT_ROLES = [self::ROLE_USER];
  45.  
  46.     /**
  47.      * @ORM\Id()
  48.      * @ORM\GeneratedValue()
  49.      * @ORM\Column(type="integer")
  50.      * @Groups({"read"})
  51.      */
  52.     private $id;
  53.  
  54.     /**
  55.      * @ORM\Column(type="string", length=180, unique=true)
  56.      * @Assert\Unique()
  57.      * @Groups({"read", "write", "register"})
  58.      */
  59.     private $username;
  60.  
  61.     /**
  62.      * @ORM\Column(type="json")
  63.      * @Groups({"read"})
  64.      */
  65.     private $roles = [];
  66.  
  67.     /**
  68.      * @var string The hashed password
  69.      * @ORM\Column(type="string")
  70.      * @Assert\NotBlank()
  71.      * @Groups({"write", "register"})
  72.      */
  73.     private $password;
  74.  
  75.     /**
  76.      * @Groups({"write", "register"})
  77.      * @Assert\Expression(
  78.      *     "this.getPassword() === this.getRetypePassword()",
  79.      *     message="Passwords does not match",
  80.      *     groups={"write", "register"}
  81.      * )
  82.      */
  83.     private $retype_password;
  84.  
  85.     /**
  86.      * @ORM\Column(type="integer", nullable=true)
  87.      */
  88.     private $password_change_date;
  89.  
  90.     /**
  91.      * @ORM\Column(type="string", length=255, unique=true)
  92.      * @Assert\NotBlank()
  93.      * @Assert\Unique(groups={"write", "register"})
  94.      * @Groups({"write", "register"})
  95.      */
  96.     private $email;
  97.  
  98.     /**
  99.      * @ORM\Column(type="string", length=255)
  100.      * @Assert\NotBlank()
  101.      * @Groups({"read", "write", "register"})
  102.      */
  103.     private $name;
  104.  
  105.     /**
  106.      * @ORM\Column(type="boolean", options={"default": 0} )
  107.      * @Groups({"read"})
  108.      */
  109.     private $is_active;
  110.  
  111.     /**
  112.      * @ORM\Column(type="string", length=40, nullable=true)
  113.      */
  114.     private $confirmation_token;
  115.  
  116.     /**
  117.      * User constructor.
  118.      */
  119.     public function __construct()
  120.     {
  121.         $this->setIsActive(false)
  122.             ->setConfirmationToken(NULL)
  123.             ->setRoles(self::DEFAULT_ROLES);
  124.     }
  125.  
  126.     public function getId(): ?int
  127.     {
  128.         return $this->id;
  129.     }
  130.  
  131.     /**
  132.      * A visual identifier that represents this user.
  133.      *
  134.      * @see UserInterface
  135.      */
  136.     public function getUsername(): string
  137.     {
  138.         return (string) $this->username;
  139.     }
  140.  
  141.     public function setUsername(string $username): self
  142.     {
  143.         $this->username = $username;
  144.  
  145.         return $this;
  146.     }
  147.  
  148.     /**
  149.      * @see UserInterface
  150.      */
  151.     public function getRoles(): array
  152.     {
  153.         $roles = $this->roles;
  154.         // guarantee every user at least has ROLE_USER
  155.         $roles[] = 'ROLE_USER';
  156.  
  157.         return array_unique($roles);
  158.     }
  159.  
  160.     public function setRoles(array $roles): self
  161.     {
  162.         $this->roles = $roles;
  163.  
  164.         return $this;
  165.     }
  166.  
  167.     /**
  168.      * @see UserInterface
  169.      */
  170.     public function getPassword(): string
  171.     {
  172.         return (string) $this->password;
  173.     }
  174.  
  175.     public function setPassword(string $password): self
  176.     {
  177.         $this->password = $password;
  178.  
  179.         return $this;
  180.     }
  181.  
  182.     /**
  183.      * @see UserInterface
  184.      */
  185.     public function getSalt()
  186.     {
  187.         // not needed when using the "bcrypt" algorithm in security.yaml
  188.     }
  189.  
  190.     /**
  191.      * @see UserInterface
  192.      */
  193.     public function eraseCredentials()
  194.     {
  195.         // If you store any temporary, sensitive data on the user, clear it here
  196.         // $this->plainPassword = null;
  197.     }
  198.  
  199.     public function getEmail(): ?string
  200.     {
  201.         return $this->email;
  202.     }
  203.  
  204.     public function setEmail(string $email): self
  205.     {
  206.         $this->email = $email;
  207.  
  208.         return $this;
  209.     }
  210.  
  211.     public function getName(): ?string
  212.     {
  213.         return $this->name;
  214.     }
  215.  
  216.     public function setName(string $name): self
  217.     {
  218.         $this->name = $name;
  219.  
  220.         return $this;
  221.     }
  222.  
  223.     public function getIsActive(): ?bool
  224.     {
  225.         return $this->is_active;
  226.     }
  227.  
  228.     public function setIsActive(bool $is_active): self
  229.     {
  230.         $this->is_active = $is_active;
  231.  
  232.         return $this;
  233.     }
  234.  
  235.     public function getConfirmationToken(): ?string
  236.     {
  237.         return $this->confirmation_token;
  238.     }
  239.  
  240.     public function setConfirmationToken(?string $confirmation_token): self
  241.     {
  242.         $this->confirmation_token = $confirmation_token;
  243.  
  244.         return $this;
  245.     }
  246.  
  247.     /**
  248.      * @return mixed
  249.      */
  250.     public function getPasswordChangeDate()
  251.     {
  252.         return $this->password_change_date;
  253.     }
  254.  
  255.     /**
  256.      * @param mixed $password_change_date
  257.      */
  258.     public function setPasswordChangeDate($password_change_date): void
  259.     {
  260.         $this->password_change_date = $password_change_date;
  261.     }
  262.  
  263.     /**
  264.      * @return string
  265.      */
  266.     public function getRetypePassword(): string
  267.     {
  268.         return $this->retype_password;
  269.     }
  270.  
  271.     /**
  272.      * @param string $retype_password
  273.      */
  274.     public function setRetypePassword(string $retype_password): void
  275.     {
  276.         $this->retype_password = $retype_password;
  277.     }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment