-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
see: #20
- Loading branch information
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use App\Repository\UserRepository; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Bridge\Doctrine\Types\UuidType; | ||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
#[ORM\Entity(repositoryClass: UserRepository::class)] | ||
#[ORM\Table(name: 'user')] | ||
#[ORM\HasLifecycleCallbacks] | ||
class User implements UserInterface, PasswordAuthenticatedUserInterface | ||
{ | ||
use ArchivableEntity; | ||
use SluggableTrait; | ||
use CreatableUpdateableTrait; | ||
|
||
#[ORM\Id] | ||
#[ORM\Column(type: UuidType::NAME, unique: true)] | ||
#[ORM\GeneratedValue(strategy: 'CUSTOM')] | ||
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')] | ||
private ?Uuid $id = null; | ||
|
||
#[ORM\Column(length: 180, unique: true)] | ||
private ?string $email = null; | ||
|
||
#[ORM\Column(type: 'json')] | ||
private array $roles = []; | ||
|
||
#[ORM\Column] | ||
private ?string $password = null; | ||
|
||
final public function getId(): ?Uuid | ||
{ | ||
return $this->id; | ||
} | ||
|
||
final public function getEmail(): ?string | ||
{ | ||
return $this->email; | ||
} | ||
|
||
final public function setEmail(string $email): self | ||
{ | ||
$this->email = $email; | ||
|
||
return $this; | ||
} | ||
|
||
/** @see UserInterface */ | ||
final public function getUserIdentifier(): string | ||
{ | ||
return $this->email; | ||
} | ||
|
||
/** @see UserInterface */ | ||
final public function getRoles(): array | ||
{ | ||
$roles = $this->roles; | ||
$roles[] = 'ROLE_USER'; | ||
|
||
return array_unique($roles); | ||
} | ||
|
||
final public function setRoles(array $roles): self | ||
{ | ||
$this->roles = $roles; | ||
|
||
return $this; | ||
} | ||
|
||
/** @see PasswordAuthenticatedUserInterface */ | ||
final public function getPassword(): string | ||
{ | ||
return $this->password; | ||
} | ||
|
||
final public function setPassword(#[\SensitiveParameter] string $password): self | ||
{ | ||
$this->password = $password; | ||
|
||
return $this; | ||
} | ||
|
||
/** @see UserInterface */ | ||
final public function eraseCredentials(): void | ||
{ | ||
// If you store any temporary, sensitive data on the user, clear it here | ||
// $this->plainPassword = null; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace App\Repository; | ||
|
||
use App\Entity\User; | ||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | ||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; | ||
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface; | ||
|
||
/** | ||
* @extends ServiceEntityRepository<User> | ||
* | ||
* @implements PasswordUpgraderInterface<User> | ||
* | ||
* @method User|null find($id, $lockMode = null, $lockVersion = null) | ||
* @method User|null findOneBy(array $criteria, array $orderBy = null) | ||
* @method User[] findAll() | ||
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | ||
*/ | ||
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface | ||
{ | ||
public function __construct(ManagerRegistry $registry) | ||
{ | ||
parent::__construct($registry, User::class); | ||
} | ||
|
||
/** | ||
* Used to upgrade (rehash) the user's password automatically over time. | ||
*/ | ||
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void | ||
{ | ||
if (!$user instanceof User) { | ||
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class)); | ||
} | ||
|
||
$user->setPassword($newHashedPassword); | ||
$this->getEntityManager()->persist($user); | ||
$this->getEntityManager()->flush(); | ||
} | ||
|
||
// /** | ||
// * @return User[] Returns an array of User objects | ||
// */ | ||
// public function findByExampleField($value): array | ||
// { | ||
// return $this->createQueryBuilder('u') | ||
// ->andWhere('u.exampleField = :val') | ||
// ->setParameter('val', $value) | ||
// ->orderBy('u.id', 'ASC') | ||
// ->setMaxResults(10) | ||
// ->getQuery() | ||
// ->getResult() | ||
// ; | ||
// } | ||
|
||
// public function findOneBySomeField($value): ?User | ||
// { | ||
// return $this->createQueryBuilder('u') | ||
// ->andWhere('u.exampleField = :val') | ||
// ->setParameter('val', $value) | ||
// ->getQuery() | ||
// ->getOneOrNullResult() | ||
// ; | ||
// } | ||
} |