Skip to content

Instantly share code, notes, and snippets.

@rakodev rakodev/User.php
Created Sep 16, 2019

Embed
What would you like to do?
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use Carbon\Carbon;
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 as Assert;
/**
* @ApiResource(
* shortName="user",
* accessControl="is_granted('ROLE_USER')",
* accessControlMessage="You have to be logged in to update your info",
* denormalizationContext={"groups"={"user:write", "user:update"}},
* itemOperations={
* "get",
* "put"={
* "denormalization_context"={"groups"={"user:update"}},
* "access_control" = "is_granted('ROLE_USER') and object == user",
* "access_control_message" = "You have to be logged in to update your info"
* },
* "delete"={
* "denormalization_context"={"groups"={"user:update"}},
* "access_control" = "is_granted('ROLE_ADMIN')"
* }
* },
* collectionOperations={
* "get",
* "post" = {
* "access_control" = "is_granted('IS_AUTHENTICATED_ANONYMOUSLY')",
* "validation_groups"={"Default", "Post"}
* }
* }
* )
* @ApiFilter(PropertyFilter::class)
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"email"}, message="This email is already registered")
* @UniqueEntity(fields={"username"}, message="This username is already registered")
* @ORM\EntityListeners({"App\Listener\UserPostPersistListener"})
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"user:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Groups({"user:read", "user:write", "user:update"})
* @Assert\NotBlank(message="Please provide an email")
* @Assert\Email(message="Please provide an email")
*/
private $email;
/**
* @ORM\Column(type="string", length=25, unique=true)
* @Groups({"user:read", "user:write", "user:update", "event:read", "event:item:get", "event:update"})
* @Assert\NotBlank(message="Please provide a username")
*/
private $username;
/**
* @ORM\Column(type="json")
* @Groups({"admin:write"})
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @Groups({"user:write", "user:update"})
* @Assert\NotBlank(message="Please provide a password", groups={"Post"})
* @SerializedName("password")
*/
private $plainPassword;
/**
* @ORM\Column(type="boolean")
* @Groups({"user:read", "user:update"})
*/
private $isActive;
/**
* @ORM\Column(type="datetime")
* @Groups({"user:read"})
*/
private $createdAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Event", mappedBy="owner", cascade={"persist"})
* @Groups({"user:read", "user:write"})
* @Assert\Valid()
*/
private $events;
/**
* @ORM\OneToMany(targetEntity="App\Entity\EventParticipant", mappedBy="participant", fetch="LAZY")
*/
private $eventParticipants;
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserMedia", mappedBy="user")
* @Groups({"user:read"})
*/
private $userMedia;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Groups({"admin:read", "owner:read", "user:write"})
*/
private $phoneNumber;
public function __construct()
{
$this->events = new ArrayCollection();
$this->eventParticipants = new ArrayCollection();
$this->userMedia = new ArrayCollection();
}
/**
* @ORM\PrePersist()
* @throws \Exception
*/
public function prePersist()
{
$this->createdAt = new \DateTime();
$this->isActive = true;
}
/**
* How long ago this was created
* @Groups({"user:read"})
* @return string
*/
public function getCreatedAtAgo(): string
{
return Carbon::instance($this->getCreatedAt())->diffForHumans();
}
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 (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 getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection|Event[]
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event): self
{
if (!$this->events->contains($event)) {
$this->events[] = $event;
$event->setOwner($this);
}
return $this;
}
public function removeEvent(Event $event): self
{
if ($this->events->contains($event)) {
$this->events->removeElement($event);
// set the owning side to null (unless already changed)
if ($event->getOwner() === $this) {
$event->setOwner(null);
}
}
return $this;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @return Collection|EventParticipant[]
*/
public function getEventParticipants(): Collection
{
return $this->eventParticipants;
}
public function addEventParticipant(EventParticipant $eventParticipant): self
{
if (!$this->eventParticipants->contains($eventParticipant)) {
$this->eventParticipants[] = $eventParticipant;
$eventParticipant->setParticipant($this);
}
return $this;
}
public function removeEventParticipant(EventParticipant $eventParticipant): self
{
if ($this->eventParticipants->contains($eventParticipant)) {
$this->eventParticipants->removeElement($eventParticipant);
// set the owning side to null (unless already changed)
if ($eventParticipant->getParticipant() === $this) {
$eventParticipant->setParticipant(null);
}
}
return $this;
}
/**
* @return Collection|UserMedia[]
*/
public function getUserMedia(): Collection
{
return $this->userMedia;
}
public function addUserMedium(UserMedia $userMedium): self
{
if (!$this->userMedia->contains($userMedium)) {
$this->userMedia[] = $userMedium;
$userMedium->setUser($this);
}
return $this;
}
public function removeUserMedium(UserMedia $userMedium): self
{
if ($this->userMedia->contains($userMedium)) {
$this->userMedia->removeElement($userMedium);
// set the owning side to null (unless already changed)
if ($userMedium->getUser() === $this) {
$userMedium->setUser(null);
}
}
return $this;
}
public function getPlainPassword(): ?string
{
return $this->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
You can’t perform that action at this time.