Skip to content

[Security] Add PHP attributes to a PHP annotations example #16308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2199,30 +2199,55 @@ will happen:
Thanks to the SensioFrameworkExtraBundle, you can also secure your controller
using annotations:

.. code-block:: diff
.. configuration-block::

// src/Controller/AdminController.php
// ...
.. code-block:: php-annotations

+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
// src/Controller/AdminController.php
// ...

+ /**
+ * Require ROLE_ADMIN for *every* controller method in this class.
+ *
+ * @IsGranted("ROLE_ADMIN")
+ */
class AdminController extends AbstractController
{
+ /**
+ * Require ROLE_ADMIN for only this controller method.
+ *
+ * @IsGranted("ROLE_ADMIN")
+ */
public function adminDashboard(): Response
{
// ...
}
}
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

/**
* Require ROLE_ADMIN for all the actions of this controller
*
* @IsGranted("ROLE_ADMIN")
*/
class AdminController extends AbstractController
{
/**
* Require ROLE_SUPER_ADMIN only for this action
*
* @IsGranted("ROLE_SUPER_ADMIN")
*/
public function adminDashboard(): Response
{
// ...
}
}

.. code-block:: php-attributes

// src/Controller/AdminController.php
// ...

use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

/**
* Require ROLE_ADMIN for all the actions of this controller
*/
#[IsGranted('ROLE_ADMIN')]
class AdminController extends AbstractController
{
/**
* Require ROLE_SUPER_ADMIN only for this action
*/
#[IsGranted('ROLE_SUPER_ADMIN')]
public function adminDashboard(): Response
{
// ...
}
}

For more information, see the `FrameworkExtraBundle documentation`_.

Expand Down