Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Entity;
- use ApiPlatform\Core\Annotation\ApiResource;
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\Security\Core\User\UserInterface;
- use Symfony\Component\Validator\Constraints as Assert;
- use Symfony\Component\Serializer\Annotation\Groups;
- /**
- * @ApiResource(
- * attributes={
- * "normalization_context"={"get"}
- * },
- * collectionOperations={
- * "post"={
- * "denormalozation_context"={"groups"={"write"}},
- * "normalization_context"={"groups"={"read"}},
- * "validation_groups"={"write"},
- * "access_control"="is_granted('IS_AUTHENTICATED_FULLY')"
- * },
- * "register"={
- * "method"="POST",
- * "path"="/users/register",
- * "denormalozation_context"={"groups"={"register"}},
- * "normalization_context"={"groups"={"read"}},
- * "validation_groups"={"register"},
- * "swagger_context"={
- * "summary"="Register a user",
- * "description"="For anonymous user to register an account."
- * }
- * }
- * }
- * )
- * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
- */
- class User implements UserInterface
- {
- const ROLE_USER = 'ROLE_USER';
- const ROLE_WEBADMIN = 'ROLE_WEBADMIN';
- const ROLE_SUPERADMIN = 'ROLE_SUPERADMIN';
- const DEFAULT_ROLES = [self::ROLE_USER];
- /**
- * @ORM\Id()
- * @ORM\GeneratedValue()
- * @ORM\Column(type="integer")
- * @Groups({"read"})
- */
- private $id;
- /**
- * @ORM\Column(type="string", length=180, unique=true)
- * @Assert\Unique()
- * @Groups({"read", "write", "register"})
- */
- private $username;
- /**
- * @ORM\Column(type="json")
- * @Groups({"read"})
- */
- private $roles = [];
- /**
- * @var string The hashed password
- * @ORM\Column(type="string")
- * @Assert\NotBlank()
- * @Groups({"write", "register"})
- */
- private $password;
- /**
- * @Groups({"write", "register"})
- * @Assert\Expression(
- * "this.getPassword() === this.getRetypePassword()",
- * message="Passwords does not match",
- * groups={"write", "register"}
- * )
- */
- private $retype_password;
- /**
- * @ORM\Column(type="integer", nullable=true)
- */
- private $password_change_date;
- /**
- * @ORM\Column(type="string", length=255, unique=true)
- * @Assert\NotBlank()
- * @Assert\Unique(groups={"write", "register"})
- * @Groups({"write", "register"})
- */
- private $email;
- /**
- * @ORM\Column(type="string", length=255)
- * @Assert\NotBlank()
- * @Groups({"read", "write", "register"})
- */
- private $name;
- /**
- * @ORM\Column(type="boolean", options={"default": 0} )
- * @Groups({"read"})
- */
- private $is_active;
- /**
- * @ORM\Column(type="string", length=40, nullable=true)
- */
- private $confirmation_token;
- /**
- * User constructor.
- */
- public function __construct()
- {
- $this->setIsActive(false)
- ->setConfirmationToken(NULL)
- ->setRoles(self::DEFAULT_ROLES);
- }
- public function getId(): ?int
- {
- return $this->id;
- }
- /**
- * A visual identifier that represents this user.
- *
- * @see UserInterface
- */
- public function getUsername(): string
- {
- return (string) $this->username;
- }
- public function setUsername(string $username): self
- {
- $this->username = $username;
- return $this;
- }
- /**
- * @see UserInterface
- */
- public function getRoles(): array
- {
- $roles = $this->roles;
- // guarantee every user at least has ROLE_USER
- $roles[] = 'ROLE_USER';
- return array_unique($roles);
- }
- public function setRoles(array $roles): self
- {
- $this->roles = $roles;
- return $this;
- }
- /**
- * @see UserInterface
- */
- public function getPassword(): string
- {
- return (string) $this->password;
- }
- public function setPassword(string $password): self
- {
- $this->password = $password;
- return $this;
- }
- /**
- * @see UserInterface
- */
- public function getSalt()
- {
- // not needed when using the "bcrypt" algorithm in security.yaml
- }
- /**
- * @see UserInterface
- */
- public function eraseCredentials()
- {
- // If you store any temporary, sensitive data on the user, clear it here
- // $this->plainPassword = null;
- }
- public function getEmail(): ?string
- {
- return $this->email;
- }
- public function setEmail(string $email): self
- {
- $this->email = $email;
- return $this;
- }
- public function getName(): ?string
- {
- return $this->name;
- }
- public function setName(string $name): self
- {
- $this->name = $name;
- return $this;
- }
- public function getIsActive(): ?bool
- {
- return $this->is_active;
- }
- public function setIsActive(bool $is_active): self
- {
- $this->is_active = $is_active;
- return $this;
- }
- public function getConfirmationToken(): ?string
- {
- return $this->confirmation_token;
- }
- public function setConfirmationToken(?string $confirmation_token): self
- {
- $this->confirmation_token = $confirmation_token;
- return $this;
- }
- /**
- * @return mixed
- */
- public function getPasswordChangeDate()
- {
- return $this->password_change_date;
- }
- /**
- * @param mixed $password_change_date
- */
- public function setPasswordChangeDate($password_change_date): void
- {
- $this->password_change_date = $password_change_date;
- }
- /**
- * @return string
- */
- public function getRetypePassword(): string
- {
- return $this->retype_password;
- }
- /**
- * @param string $retype_password
- */
- public function setRetypePassword(string $retype_password): void
- {
- $this->retype_password = $retype_password;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment