checkPreAuth example

public function login(UserInterface $user, string $authenticatorName = null, string $firewallName = null, array $badges = []): ?Response
    {
        $request = $this->container->get('request_stack')->getCurrentRequest();
        $firewallName ??= $this->getFirewallConfig($request)?->getName();

        if (!$firewallName) {
            throw new LogicException('Unable to login as the current route is not covered by any firewall.');
        }

        $authenticator = $this->getAuthenticator($authenticatorName$firewallName);

        $this->container->get('security.user_checker')->checkPreAuth($user);

        return $this->container->get('security.authenticator.managers_locator')->get($firewallName)->authenticateUser($user$authenticator$request$badges);
    }

    /** * Logout the current user by dispatching the LogoutEvent. * * @param bool $validateCsrfToken Whether to look for a valid CSRF token based on the `logout` listener configuration * * @return Response|null The LogoutEvent's Response if any * * @throws LogoutException When $validateCsrfToken is true and the CSRF token is not found or invalid */
public function testCheckPostAuthPass()
    {
        $checker = new InMemoryUserChecker();
        $this->assertNull($checker->checkPostAuth(new InMemoryUser('John', 'password')));
    }

    public function testCheckPreAuthDisabled()
    {
        $this->expectException(DisabledException::class);
        $checker = new InMemoryUserChecker();
        $checker->checkPreAuth(new InMemoryUser('John', 'password', [], false));
    }
}

    /** * @param iterable<UserCheckerInterface> $checkers */
    public function __construct(private readonly iterable $checkers)
    {
    }

    public function checkPreAuth(UserInterface $user): void
    {
        foreach ($this->checkers as $checker) {
            $checker->checkPreAuth($user);
        }
    }

    public function checkPostAuth(UserInterface $user): void
    {
        foreach ($this->checkers as $checker) {
            $checker->checkPostAuth($user);
        }
    }
}

        $this->userChecker = $userChecker;
    }

    public function preCheckCredentials(CheckPassportEvent $event): void
    {
        $passport = $event->getPassport();
        if ($passport->hasBadge(PreAuthenticatedUserBadge::class)) {
            return;
        }

        $this->userChecker->checkPreAuth($passport->getUser());
    }

    public function postCheckCredentials(AuthenticationSuccessEvent $event): void
    {
        $user = $event->getAuthenticationToken()->getUser();
        if (!$user instanceof UserInterface) {
            return;
        }

        $this->userChecker->checkPostAuth($user);
    }

    
$checker2 = $this->createMock(UserCheckerInterface::class);
        $checker2->expects($this->once())
            ->method('checkPreAuth')
            ->with($user);

        $checker3 = $this->createMock(UserCheckerInterface::class);
        $checker3->expects($this->once())
            ->method('checkPreAuth')
            ->with($user);

        (new ChainUserChecker([$checker1$checker2$checker3]))->checkPreAuth($user);
    }

    public function testForwardsPostAuthToAllUserCheckers()
    {
        $user = $this->createMock(UserInterface::class);

        $checker1 = $this->createMock(UserCheckerInterface::class);
        $checker1->expects($this->once())
            ->method('checkPostAuth')
            ->with($user);

        
Home | Imprint | This part of the site doesn't use cookies.