<?phpdeclare(strict_types=1);namespaceApp\Repository;useApp\Entity\User;useSymfony\Component\Security\Core\User\UserInterface;useSymfony\Component\Uid\Ulid;use Webauthn\Bundle\Repository\PublicKeyCredentialUserEntityRepository as PublicKeyCredentialUserEntityRepositoryInterface;
useWebauthn\PublicKeyCredentialUserEntity;finalclassPublicKeyCredentialUserEntityRepositoryimplementsPublicKeyCredentialUserEntityRepositoryInterface{/** * The UserRepository $userRepository is the repository * that already exists in the application */publicfunction__construct(privateUserRepository $userRepository) { }/** * This method creates the next Webauthn User Entity ID * In this example, we use Ulid */publicfunctiongenerateNextUserEntityId():string {returnUlid::generate(); }/** * This method saves the user or does nothing if the user already exists. * It may throw an exception. Just adapt it on your needs */publicfunctionsaveUserEntity(PublicKeyCredentialUserEntity $userEntity):void {/** @varUser|null $user */ $user =$this->userRepository->findOneBy(['id'=> $userEntity->getId(), ]);if ($user instanceofUserInterface) {return; } $user =newUser( $userEntity->getId(), $userEntity->getUserIdentifier(), $userEntity->getDisplayName()// Or any other similar method your entity may have );$this->userRepository->save($user); // Custom method to be added in your repository }publicfunctionfindOneByUsername(string $username):?PublicKeyCredentialUserEntity {/** @varUser|null $user */ $user =$this->userRepository->findOneBy(['username'=> $username, ]);return$this->getUserEntity($user); }publicfunctionfindOneByUserHandle(string $userHandle):?PublicKeyCredentialUserEntity {/** @varUser|null $user */ $user =$this->userRepository->findOneBy(['id'=> $userHandle, ]);return$this->getUserEntity($user); }/** * Converts a Symfony User (if any) into a Webauthn User Entity */privatefunctiongetUserEntity(null|User $user):?PublicKeyCredentialUserEntity {if ($user ===null) {returnnull; }returnnewPublicKeyCredentialUserEntity( $user->getUsername(), $user->getUserIdentifier(), $user->getDisplayName(),null ); }}