src/Entity/User.php line 33
<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use App\Annotations\HideFromView;
use App\Trait\EntityTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\UserRepository;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use ApiPlatform\Action\NotFoundAction;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
/**
* User entity
*/
#[ApiResource(
normalizationContext: ['enable_max_depth'=>true],
)]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User implements UserInterface, PasswordAuthenticatedUserInterface, TwoFactorInterface
{
use EntityTrait;
/**
* ID
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
/**
* @var string $username Max length 180, unique, not nullable
*/
#[ORM\Column(length: 180, unique: true)]
private ?string $username = null;
// TODO: relation with UserRoles, OneToMany
/**
* The list of the user's roles
*/
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[Ignore]
#[ORM\Column]
private ?string $password = null;
/**
* @var string $apiToken Max length 255, unique, not nullable
*/
#[ORM\Column(length: 255, unique: true)]
private ?string $apiToken = null;
/**
* @var string $email Max length 255, not nullable
*/
#[ORM\Column(length: 255)]
private ?string $email = null;
/**
* @var bool $enable Not nullable
*/
#[ORM\Column]
private ?bool $enable = null;
#[MaxDepth(1)]
#[ORM\ManyToMany(targetEntity: UserGroup::class, mappedBy: 'users')]
private Collection $userGroups;
#[ORM\ManyToOne(inversedBy: 'users')]
private ?Channel $channel = null;
#[MaxDepth(1)]
#[ORM\OneToOne(mappedBy: 'user', cascade: ['persist', 'remove'])]
private ?RefillSetting $refillSetting = null;
#[Ignore]
#[HideFromView]
#[MaxDepth(1)]
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Refill::class)]
private Collection $refills;
#[ORM\Column(length: 255)]
private ?string $authCode;
public function __construct()
{
$this->userGroups = new ArrayCollection();
$this->refills = new ArrayCollection();
}
/**
* Getter for the entity's id property
*
* @return int The id property
*/
public function getId(): ?int
{
return $this->id;
}
/**
* Setter for the entity's id property
*
* @param $id The property's new value
* @return self
*/
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
/**
* Getter for the entity's username property
*
* @return string The username property
*/
public function getUsername(): ?string
{
return $this->username;
}
/**
* Setter for the entity's username property
*
* @param $username The property's new value
* @return self
*/
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles; // Roles belonging to the user
foreach ($this->userGroups as $group)
{
foreach ($group->getRoles() as $groupRole)
{
array_push($roles, $groupRole);
}
}
$roles[] = 'ROLE_STDUSER'; // guarantee every user at least has ROLE_USER
return array_unique($roles); // Instead of checking every element with in_array, I just remove duplicates at the end
}
/**
* Setter for the entity's roles property
*
* @param $roles The property's new value
* @return self
*/
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
/**
* Setter for the entity's password property
*
* @param $password The property's new value
* @return self
*/
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* Getter for the entity's apiToken property
*
* @return string The apiToken property
*/
public function getApiToken(): ?string
{
return $this->apiToken;
}
/**
* Setter for the entity's apiToken property
*
* @param $apiToken The property's new value
* @return self
*/
public function setApiToken(?string $apiToken): self
{
$this->apiToken = $apiToken;
return $this;
}
/**
* @return string|null
*/
public function getAuthCode(): ?string
{
return $this->authCode;
}
/**
* @param string|null $authCode
*/
public function setAuthCode(?string $authCode): void
{
$this->authCode = $authCode;
}
/**
* Getter for the entity's email property
*
* @return string The email property
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* Setter for the entity's email property
*
* @param $email The property's new value
* @return self
*/
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* Converts the entity to a string. Used to show a string that represents the entity in form fields
*
* @return string The username
*/
public function __toString()
{
return $this->username;
}
/**
* Getter for the entity's enable property
*
* @return string The enable property
*/
public function isEnable(): ?bool
{
return $this->enable;
}
/**
* Setter for the entity's enable property
*
* @param $enable The property's new value
* @return self
*/
public function setEnable(bool $enable): self
{
$this->enable = $enable;
return $this;
}
/**
* @return Collection<int, UserGroup>
*/
public function getUserGroups(): Collection
{
return $this->userGroups;
}
public function addUserGroup(UserGroup $userGroup): self
{
if (!$this->userGroups->contains($userGroup)) {
$this->userGroups->add($userGroup);
$userGroup->addUser($this);
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): self
{
if ($this->userGroups->removeElement($userGroup)) {
$userGroup->removeUser($this);
}
return $this;
}
/**
* Gets the total amount of tokens belonging to the user, not taking into consideration entity type and action
*/
public function getBalance(): ?int
{
$balance = 0;
foreach($this->refills as $refill)
$balance += ($refill->getQuantity() - $refill->getUsed());
return $balance;
}
public function getChannel(): ?Channel
{
return $this->channel;
}
public function setChannel(?Channel $channel): self
{
$this->channel = $channel;
return $this;
}
public function getRefillSetting(): ?RefillSetting
{
return $this->refillSetting;
}
public function setRefillSetting(?RefillSetting $refillSetting): self
{
// unset the owning side of the relation if necessary
if ($refillSetting === null && $this->refillSetting !== null) {
$this->refillSetting->setUser(null);
}
// set the owning side of the relation if necessary
if ($refillSetting !== null && $refillSetting->getUser() !== $this) {
$refillSetting->setUser($this);
}
$this->refillSetting = $refillSetting;
return $this;
}
/**
* @return Collection<int, Refill>
*/
public function getRefills(): Collection
{
return $this->refills;
}
public function addRefill(Refill $refill): self
{
if (!$this->refills->contains($refill)) {
$this->refills->add($refill);
$refill->setUser($this);
}
return $this;
}
public function removeRefill(Refill $refill): self
{
if ($this->refills->removeElement($refill)) {
// set the owning side to null (unless already changed)
if ($refill->getUser() === $this) {
$refill->setUser(null);
}
}
return $this;
}
public function isEmailAuthEnabled(): bool
{
return true; // This can be a persisted field to switch email code authentication on/off
}
public function getEmailAuthRecipient(): string
{
return $this->email;
}
public function getEmailAuthCode(): ?string
{
/*
if (null === $this->authCode) {
throw new \LogicException('The email authentication code was not set');
}
*/
return $this->authCode;
}
public function setEmailAuthCode(?string $authCode): void
{
$this->authCode = $authCode;
}
}