Skip to content
Merged
Changes from 3 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
9 changes: 7 additions & 2 deletions book/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1072,19 +1072,24 @@ fine-grained enough in certain cases. When necessary, you can easily force
authorization from inside a controller::

// ...
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

public function helloAction($name)
{
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException();
throw $this->createAccessDeniedException('Unable to access this page!');
}

// ...
}

.. _book-security-securing-controller-annotations:

.. versionadded:: 2.5
The ``createAccessDeniedException`` method was introduced in Symfony 2.5.

The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createAccessDeniedException()` method creates a special :class:`Symfony\\Component\\Security\\Core\Exception\\AccessDeniedException`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a versionadded block above this:

.. versionadded:: 2.5
    The ``createAccessDeniedException`` method was introduced in Symfony 2.5.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, missed that. Updated.

object, which ultimately triggers a 403 HTTP response inside Symfony.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And can you please wrap lines after the first word that crosses the 72nd character?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


Thanks to the SensioFrameworkExtraBundle, you can also secure your controller using annotations::

// ...
Expand Down