feat: make login page identify users by email or handle
This commit is contained in:
@@ -14,16 +14,21 @@ security:
|
||||
entity:
|
||||
class: App\Entity\User\User
|
||||
property: handle
|
||||
login_user_provider:
|
||||
id: App\Security\LoginUserProvider
|
||||
firewalls:
|
||||
dev:
|
||||
pattern: ^/(_(profiler|wdt)|css|images|js)/
|
||||
security: false
|
||||
api:
|
||||
provider: app_user_provider
|
||||
pattern: ^/v4
|
||||
security: true
|
||||
stateless: true
|
||||
# https://github.com/thephpleague/oauth2-server-bundle/blob/master/docs/basic-setup.md#configuring-the-security-layer
|
||||
oauth2: true
|
||||
main:
|
||||
provider: login_user_provider
|
||||
form_login:
|
||||
login_path: app_login
|
||||
check_path: app_login
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Security;
|
||||
|
||||
use App\Entity\User\User;
|
||||
use App\Repository\User\UserRepository;
|
||||
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
|
||||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
|
||||
class LoginUserProvider implements UserProviderInterface, PasswordUpgraderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private UserRepository $userRepository,
|
||||
#[Autowire(service: 'security.user.provider.concrete.app_user_provider')]
|
||||
private EntityUserProvider $entityUserProvider,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Symfony calls this method if you use features like switch_user
|
||||
* or remember_me. If you're not using these features, you do not
|
||||
* need to implement this method.
|
||||
*
|
||||
* @throws UserNotFoundException if the user is not found
|
||||
*/
|
||||
public function loadUserByIdentifier(string $identifier): UserInterface
|
||||
{
|
||||
$user = $this->userRepository->findOneByIdentifier($identifier);
|
||||
|
||||
if (!$user) {
|
||||
throw new UserNotFoundException(\sprintf('User "%s" not found.', $identifier));
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function refreshUser(UserInterface $user): UserInterface
|
||||
{
|
||||
return $this->entityUserProvider->refreshUser($user);
|
||||
}
|
||||
|
||||
public function supportsClass(string $class): bool
|
||||
{
|
||||
return $class === User::class || is_subclass_of($class, User::class);
|
||||
}
|
||||
|
||||
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
|
||||
{
|
||||
$this->entityUserProvider->upgradePassword($user, $newHashedPassword);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user