/User.php Secret
Created
May 28, 2021 13:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Entity; | |
use ApiPlatform\Core\Annotation\ApiFilter; | |
use ApiPlatform\Core\Annotation\ApiResource; | |
use ApiPlatform\Core\Serializer\Filter\PropertyFilter; | |
use App\Repository\UserRepository; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\Common\Collections\Collection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\Serializer\Annotation\Groups; | |
use Symfony\Component\Serializer\Annotation\SerializedName; | |
use Symfony\Component\Validator\Constraints\Email; | |
use Symfony\Component\Validator\Constraints\NotBlank; | |
use Symfony\Component\Validator\Constraints\Valid; | |
/** | |
* @ORM\Entity(repositoryClass=UserRepository::class) | |
*/ | |
#[ | |
ApiResource( | |
collectionOperations: [ | |
'get', | |
'post' => [ | |
'security' => "is_granted('is_not_authenticated')", | |
'validation_groups' => ['Default', 'create'] | |
], | |
], | |
itemOperations: [ | |
'get', | |
'put' => [ | |
'security' => "is_granted('ROLE_USER') and object == user" | |
], | |
'delete' => [ | |
'security' => "is_granted('ROLE_ADMIN')" | |
] | |
], | |
denormalizationContext: ['groups' => ['user:write']], | |
normalizationContext: ['groups' => ['user:read']], | |
security: "is_granted('ROLE_USER')" | |
), | |
UniqueEntity(fields: ['username']), | |
UniqueEntity(fields: ['email']), | |
ApiFilter(PropertyFilter::class) | |
] | |
class User implements UserInterface | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\GeneratedValue | |
* @ORM\Column(type="integer") | |
*/ | |
private $id; | |
/** | |
* @ORM\Column(type="string", length=180, unique=true) | |
*/ | |
#[ | |
Groups(['user:read', 'user:write']), | |
NotBlank(), | |
Email() | |
] | |
private $email; | |
/** | |
* @ORM\Column(type="json") | |
*/ | |
#[Groups(['admin:write'])] | |
private $roles = []; | |
#[ | |
Groups(['user:write']), | |
SerializedName('password'), | |
NotBlank( | |
groups: ['create'] | |
) | |
] | |
private $plainPassword; | |
/** | |
* @var string The hashed password | |
* @ORM\Column(type="string") | |
*/ | |
private $password; | |
/** | |
* @ORM\Column(type="string", length=255, unique=true) | |
*/ | |
#[ | |
Groups(['user:read', 'user:write', 'cheese:item:get', 'cheese:write']), | |
NotBlank() | |
] | |
private $username; | |
/** | |
* @ORM\OneToMany(targetEntity=CheeseListing::class, mappedBy="owner", cascade="persist", orphanRemoval=true) | |
*/ | |
#[ | |
Groups(['user:read', 'user:write']), | |
Valid() | |
] | |
private $cheeseListings; | |
/** | |
* @ORM\Column(type="string", length=50, nullable=true) | |
*/ | |
#[ | |
Groups(['admin:read', 'owner:read', 'user:write']), | |
] | |
private $phoneNumber; | |
public function __construct() | |
{ | |
$this->cheeseListings = new ArrayCollection(); | |
} | |
public function getId(): ?int | |
{ | |
return $this->id; | |
} | |
public function getEmail(): ?string | |
{ | |
return $this->email; | |
} | |
public function setEmail(string $email): self | |
{ | |
$this->email = $email; | |
return $this; | |
} | |
/** | |
* A visual identifier that represents this user. | |
* | |
* @see UserInterface | |
*/ | |
public function getUsername(): string | |
{ | |
return (string) $this->username; | |
} | |
/** | |
* @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 $this->password; | |
} | |
public function setPassword(string $password): self | |
{ | |
$this->password = $password; | |
return $this; | |
} | |
/** | |
* Returning a salt is only needed, if you are not using a modern | |
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml. | |
* | |
* @see UserInterface | |
*/ | |
public function getSalt(): ?string | |
{ | |
return null; | |
} | |
/** | |
* @see UserInterface | |
*/ | |
public function eraseCredentials() | |
{ | |
// If you store any temporary, sensitive data on the user, clear it here | |
$this->plainPassword = null; | |
} | |
public function setUsername(string $username): self | |
{ | |
$this->username = $username; | |
return $this; | |
} | |
/** | |
* @return Collection|CheeseListing[] | |
*/ | |
public function getCheeseListings(): Collection | |
{ | |
return $this->cheeseListings; | |
} | |
public function addCheeseListing(CheeseListing $cheeseListing): self | |
{ | |
if (!$this->cheeseListings->contains($cheeseListing)) { | |
$this->cheeseListings[] = $cheeseListing; | |
$cheeseListing->setOwner($this); | |
} | |
return $this; | |
} | |
public function removeCheeseListing(CheeseListing $cheeseListing): self | |
{ | |
if ($this->cheeseListings->removeElement($cheeseListing)) { | |
// set the owning side to null (unless already changed) | |
if ($cheeseListing->getOwner() === $this) { | |
$cheeseListing->setOwner(null); | |
} | |
} | |
return $this; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getPlainPassword(): ?string | |
{ | |
return $this->plainPassword; | |
} | |
/** | |
* @param mixed $plainPassword | |
*/ | |
public function setPlainPassword(string $plainPassword): self | |
{ | |
$this->plainPassword = $plainPassword; | |
return $this; | |
} | |
public function getPhoneNumber(): ?string | |
{ | |
return $this->phoneNumber; | |
} | |
public function setPhoneNumber(?string $phoneNumber): self | |
{ | |
$this->phoneNumber = $phoneNumber; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment