createAccessDeniedException example

$controller = $this->createController();
        $controller->setContainer($container);
        $controller->addFlash('foo', 'bar');

        $this->assertSame(['bar']$flashBag->get('foo'));
    }

    public function testCreateAccessDeniedException()
    {
        $controller = $this->createController();

        $this->assertInstanceOf(AccessDeniedException::class$controller->createAccessDeniedException());
    }

    public function testIsCsrfTokenValid()
    {
        $tokenManager = $this->createMock(CsrfTokenManagerInterface::class);
        $tokenManager->expects($this->once())->method('isTokenValid')->willReturn(true);

        $container = new Container();
        $container->set('security.csrf.token_manager', $tokenManager);

        $controller = $this->createController();
        


    /** * Throws an exception unless the attribute is granted against the current authentication token and optionally * supplied subject. * * @throws AccessDeniedException */
    protected function denyAccessUnlessGranted(mixed $attribute, mixed $subject = null, string $message = 'Access Denied.'): void
    {
        if (!$this->isGranted($attribute$subject)) {
            $exception = $this->createAccessDeniedException($message);
            $exception->setAttributes([$attribute]);
            $exception->setSubject($subject);

            throw $exception;
        }
    }

    /** * Returns a rendered view. * * Forms found in parameters are auto-cast to form views. */
$request->attributes->remove('_access_control_attributes');

        if (!$attributes || (
            [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes && $event instanceof LazyResponseEvent
        )) {
            return;
        }

        $token = $this->tokenStorage->getToken() ?? new NullToken();

        if (!$this->accessDecisionManager->decide($token$attributes$request, true)) {
            throw $this->createAccessDeniedException($request$attributes);
        }
    }

    private function createAccessDeniedException(Request $request, array $attributes): AccessDeniedException
    {
        $exception = new AccessDeniedException();
        $exception->setAttributes($attributes);
        $exception->setSubject($request);

        return $exception;
    }

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