User Entity Repository

User Entity

Your application certainly has a user repository. Good news: there is no need to modify it!
The User Entity Repository can be completely decoupled from the user entity used by your application.
This repository should be declared as a Symfony service and shall implement Webauthn\Bundle\Repository\PublicKeyCredentialUserEntityRepository.
Hereafter an example where the application User Repository is injected. This repository uses Doctrine and provides findOneBy* methods.
src/Repository/PublicKeyCredentialUserEntityRepository.php
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Entity\User;
8
use Symfony\Component\Security\Core\User\UserInterface;
9
use Symfony\Component\Uid\Ulid;
10
use Webauthn\Bundle\Repository\PublicKeyCredentialUserEntityRepository as PublicKeyCredentialUserEntityRepositoryInterface;
11
use Webauthn\PublicKeyCredentialUserEntity;
12
13
final class PublicKeyCredentialUserEntityRepository implements PublicKeyCredentialUserEntityRepositoryInterface
14
{
15
/**
16
* The UserRepository $userRepository is the repository
17
* that already exists in the application
18
*/
19
public function __construct(private UserRepository $userRepository)
20
{
21
}
22
23
/**
24
* This method creates the next Webauthn User Entity ID
25
* In this example, we use Ulid
26
*/
27
public function generateNextUserEntityId(): string {
28
return Ulid::generate();
29
}
30
31
/**
32
* This method saves the user or does nothing if the user already exists.
33
* It may throw an exception. Just adapt it on your needs
34
*/
35
public function saveUserEntity(PublicKeyCredentialUserEntity $userEntity): void
36
{
37
/** @var User|null $user */
38
$user = $this->userRepository->findOneBy([
39
'id' => $userEntity->getId(),
40
]);
41
if (!$user instanceof UserInterface) {
42
return;
43
}
44
$user = new User(
45
$userEntity->getId(),
46
$userEntity->getUserIdentifier(),
47
$userEntity->getDisplayName() // Or any other similar method your entity may have
48
);
49
$this->userRepository->save($user); // Custom method to be added in your repository
50
}
51
52
public function findOneByUsername(string $username): ?PublicKeyCredentialUserEntity
53
{
54
/** @var User|null $user */
55
$user = $this->userRepository->findOneBy([
56
'username' => $username,
57
]);
58
59
return $this->getUserEntity($user);
60
}
61
62
public function findOneByUserHandle(string $userHandle): ?PublicKeyCredentialUserEntity
63
{
64
/** @var User|null $user */
65
$user = $this->userRepository->findOneBy([
66
'id' => $userHandle,
67
]);
68
69
return $this->getUserEntity($user);
70
}
71
72
/**
73
* Converts a Symfony User (if any) into a Webauthn User Entity
74
*/
75
private function getUserEntity(null|User $user): ?PublicKeyCredentialUserEntity
76
{
77
if ($user === null) {
78
return null;
79
}
80
81
return new PublicKeyCredentialUserEntity(
82
$user->getUsername(),
83
$user->getUserIdentifier(),
84
$user->getDisplayName(),
85
null
86
);
87
}
88
}
89
Copied!
Export as PDF
Copy link
Edit on GitHub