denyAccessUnlessGranted example

$this->expectException(AccessDeniedException::class);

        $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
        $authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false);

        $container = new Container();
        $container->set('security.authorization_checker', $authorizationChecker);

        $controller = $this->createController();
        $controller->setContainer($container);

        $controller->denyAccessUnlessGranted('foo');
    }

    /** * @dataProvider provideDenyAccessUnlessGrantedSetsAttributesAsArray */
    public function testdenyAccessUnlessGrantedSetsAttributesAsArray($attribute$exceptionAttributes)
    {
        $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
        $authorizationChecker->method('isGranted')->willReturn(false);

        $container = new Container();
        


namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class ProfileController extends AbstractController
{
    public function __invoke()
    {
        $this->denyAccessUnlessGranted('ROLE_USER');

        return $this->json(['email' => $this->getUser()->getUserIdentifier()]);
    }
}
Home | Imprint | This part of the site doesn't use cookies.