Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. SensioFrameworkExtraBundle
  5. @Security & @IsGranted

@Security & @IsGranted

Edit this page
It's no longer recommended to use this bundle in current Symfony applications. All the annotations provided by this bundle are now built-in in Symfony as PHP attributes. Check out the full list of Symfony attributes.

Usage

The @Security and @IsGranted annotations restrict access on controllers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

class PostController extends Controller
{
    /**
     * @IsGranted("ROLE_ADMIN")
     *
     * or use @Security for more flexibility:
     *
     * @Security("is_granted('ROLE_ADMIN') and is_granted('ROLE_FRIENDLY_USER')")
     */
    public function index()
    {
        // ...
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

class PostController extends Controller
{
    #[IsGranted('ROLE_ADMIN')]
    /** or use Security attribute */
    #[Security("is_granted('ROLE_ADMIN') and is_granted('ROLE_FRIENDLY_USER')")]
    public function index()
    {
        // ...
    }
}

@IsGranted

The @IsGranted() annotation is the simplest way to restrict access. Use it to restrict by roles, or use custom voters to restrict access based on variables passed to the controller:

1
2
3
4
5
6
7
8
9
/**
 * @Route("/posts/{id}")
 *
 * @IsGranted("ROLE_ADMIN")
 * @IsGranted("POST_SHOW", subject="post")
 */
public function show(Post $post)
{
}
1
2
3
4
5
6
#[Route('/posts/{id}')]
#[IsGranted('ROLE_ADMIN')]
#[IsGranted('POST_SHOW', subject: 'post')]
public function show(Post $post)
{
}

Each IsGranted() must grant access for the user to have access to the controller.

Tip

The @IsGranted("POST_SHOW", subject="post") is an example of using a custom security voter. For more details, see the Security Voters page.

You can also control the message and status code:

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * Will throw a normal AccessDeniedException:
 *
 * @IsGranted("ROLE_ADMIN", message="No access! Get out!")
 *
 * Will throw an HttpException with a 404 status code:
 *
 * @IsGranted("ROLE_ADMIN", statusCode=404, message="Post not found")
 */
public function show(Post $post)
{
}
1
2
3
4
5
6
7
/** Will throw a normal AccessDeniedException */
#[IsGranted('ROLE_ADMIN', message: 'No access! Get out!')]
/** Will throw an HttpException with a 404 status code */
#[IsGranted('ROLE_ADMIN', statusCode: 404, message: 'Post not found')]
public function show(Post $post)
{
}

@Security

The @Security annotation is more flexible than @IsGranted: it allows you to pass an expression that can contain custom logic:

1
2
3
4
5
6
7
/**
 * @Security("is_granted('ROLE_ADMIN') and is_granted('POST_SHOW', post)")
 */
public function show(Post $post)
{
    // ...
}
1
2
3
4
5
#[Security("is_granted('ROLE_ADMIN') and is_granted('POST_SHOW', post)")]
public function show(Post $post)
{
    // ...
}

The expression can use all functions that you can use in the access_control section of the security bundle configuration, with the addition of the is_granted() function.

The expression has access to the following variables:

  • token: The current security token;
  • user: The current user object;
  • request: The request instance;
  • roles: The user roles;
  • and all request attributes.

You can throw an Symfony\Component\HttpKernel\Exception\HttpException exception instead of Symfony\Component\Security\Core\Exception\AccessDeniedException using the statusCode option:

1
2
3
4
5
6
/**
 * @Security("is_granted('POST_SHOW', post)", statusCode=404)
 */
public function show(Post $post)
{
}
1
2
3
4
#[Security("is_granted('POST_SHOW', post)", statusCode: 404)]
public function show(Post $post)
{
}

The message option allows you to customize the exception message:

1
2
3
4
5
6
/**
 * @Security("is_granted('POST_SHOW', post)", statusCode=404, message="Resource not found.")
 */
public function show(Post $post)
{
}
1
2
3
4
#[Security("is_granted('POST_SHOW', post)", statusCode: 404, message: 'Resource not found.')]
public function show(Post $post)
{
}

Tip

You can also add @IsGranted or @Security annotations on a controller class to prevent access to all actions in the class.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Take the exam at home

    Take the exam at home

    Version:

    Table of Contents

    • Usage
    • @IsGranted
    • @Security

    Symfony footer

    Avatar of Andrew Tch, a Symfony contributor

    Thanks Andrew Tch for being a Symfony contributor

    1 commit • 66 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony