checkPostAuth example

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

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

        (new ChainUserChecker([$checker1$checker2$checker3]))->checkPostAuth($user);
    }
}
$this->userChecker->checkPreAuth($passport->getUser());
    }

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

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

    public static function getSubscribedEvents(): array
    {
        return [
            CheckPassportEvent::class => ['preCheckCredentials', 256],
            AuthenticationSuccessEvent::class => ['postCheckCredentials', 256],
        ];
    }
}
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\InMemoryUserChecker;
use Symfony\Component\Security\Core\User\UserInterface;

class InMemoryUserCheckerTest extends TestCase
{
    public function testCheckPostAuthNotAdvancedUserInterface()
    {
        $checker = new InMemoryUserChecker();

        $this->assertNull($checker->checkPostAuth($this->createMock(UserInterface::class)));
    }

    public function testCheckPostAuthPass()
    {
        $checker = new InMemoryUserChecker();
        $this->assertNull($checker->checkPostAuth(new InMemoryUser('John', 'password')));
    }

    public function testCheckPreAuthDisabled()
    {
        $this->expectException(DisabledException::class);
        
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);
        }
    }
}


        if (false === $this->accessDecisionManager->decide($token[$this->role]$user)) {
            $exception = new AccessDeniedException();
            $exception->setAttributes($this->role);

            throw $exception;
        }

        $this->logger?->info('Attempting to switch to user.', ['username' => $username]);

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

        $roles = $user->getRoles();
        $roles[] = 'ROLE_PREVIOUS_ADMIN';
        $originatedFromUri = str_replace('/&', '/?', preg_replace('#[&?]'.$this->usernameParameter.'=[^&]*#', '', $request->getRequestUri()));
        $token = new SwitchUserToken($user$this->firewallName, $roles$token$originatedFromUri);

        if (null !== $this->dispatcher) {
            $switchEvent = new SwitchUserEvent($request$token->getUser()$token);
            $this->dispatcher->dispatch($switchEvent, SecurityEvents::SWITCH_USER);
            // use the token from the event in case any listeners have replaced it.             $token = $switchEvent->getToken();
        }
Home | Imprint | This part of the site doesn't use cookies.