Files
tonehaus/src/Entity/User.php
T

195 lines
4.6 KiB
PHP

<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User models an authenticated account that can create reviews and albums.
*/
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: 'users')]
#[UniqueEntity(fields: ['email'], message: 'This email is already registered.')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[Assert\NotBlank]
#[Assert\Email]
private string $email = '';
/**
* @var list<string>
*/
#[ORM\Column(type: 'json')]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column(type: 'string')]
private string $password = '';
#[ORM\Column(type: 'string', length: 120, nullable: true)]
#[Assert\Length(max: 120)]
private ?string $displayName = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $profileImagePath = null;
#[ORM\Column(type: 'string', length: 50, nullable: true)]
private ?string $oauthProvider = null;
#[ORM\Column(type: 'string', length: 190, nullable: true)]
private ?string $oauthId = null;
/**
* Returns the database identifier.
*/
public function getId(): ?int
{
return $this->id;
}
/**
* Returns the normalized email address.
*/
public function getEmail(): string
{
return $this->email;
}
/**
* Sets and normalizes the email address.
*/
public function setEmail(string $email): void
{
$this->email = strtolower($email);
}
/**
* Symfony security identifier alias for the email.
*/
public function getUserIdentifier(): string
{
return $this->email;
}
/**
* Returns the unique role list plus the implicit ROLE_USER.
*
* @return list<string>
*/
public function getRoles(): array
{
$roles = $this->roles;
if (!in_array('ROLE_USER', $roles, true)) {
$roles[] = 'ROLE_USER';
}
return array_values(array_unique($roles));
}
/**
* Replaces the granted role list.
*
* @param list<string> $roles
*/
public function setRoles(array $roles): void
{
$this->roles = array_values(array_unique($roles));
}
/**
* Adds a single role if not already present.
*/
public function addRole(string $role): void
{
$roles = $this->getRoles();
if (!in_array($role, $roles, true)) {
$roles[] = $role;
}
$this->roles = $roles;
}
/**
* Returns the hashed password string.
*/
public function getPassword(): string
{
return $this->password;
}
/**
* Stores the hashed password string.
*/
public function setPassword(string $hashedPassword): void
{
$this->password = $hashedPassword;
}
/**
* Removes any sensitive transient data (no-op here).
*/
#[\Deprecated(reason: 'No transient credentials stored; method retained for BC.')]
public function eraseCredentials(): void
{
// no-op
}
/**
* Returns the optional display name.
*/
public function getDisplayName(): ?string
{
return $this->displayName;
}
/**
* Sets the optional display name.
*/
public function setDisplayName(?string $displayName): void
{
$this->displayName = $displayName;
}
public function getProfileImagePath(): ?string
{
return $this->profileImagePath;
}
public function setProfileImagePath(?string $profileImagePath): void
{
$this->profileImagePath = $profileImagePath;
}
public function getOauthProvider(): ?string
{
return $this->oauthProvider;
}
public function setOauthProvider(?string $oauthProvider): void
{
$this->oauthProvider = $oauthProvider === null ? null : strtolower(trim($oauthProvider));
}
public function getOauthId(): ?string
{
return $this->oauthId;
}
public function setOauthId(?string $oauthId): void
{
$this->oauthId = $oauthId === null ? null : trim($oauthId);
}
}