From 2371698388062be0ff7b7f4a4a9a13c87b961efa Mon Sep 17 00:00:00 2001 From: n3wborn Date: Wed, 1 Nov 2023 09:07:04 +0100 Subject: [PATCH] feat(entity): add User see: #20 --- src/Entity/User.php | 94 +++++++++++++++++++++++++++++++ src/Repository/UserRepository.php | 67 ++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 src/Entity/User.php create mode 100644 src/Repository/UserRepository.php diff --git a/src/Entity/User.php b/src/Entity/User.php new file mode 100644 index 0000000..f05ac36 --- /dev/null +++ b/src/Entity/User.php @@ -0,0 +1,94 @@ +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; + } +} diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php new file mode 100644 index 0000000..c788f46 --- /dev/null +++ b/src/Repository/UserRepository.php @@ -0,0 +1,67 @@ + + * + * @implements PasswordUpgraderInterface + * + * @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() +// ; +// } +}