Skip to content
Open
Show file tree
Hide file tree
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
64 changes: 64 additions & 0 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,34 @@
<a href="...">Delete</a>
{% endif %}

Symfony also provides the ``access_decision()`` and ``access_decision_for_user()``
Twig functions to check authorization and to retrieve the reasons for denying
permission in :ref:`your custom security voters <creating-the-custom-voter>`:

.. code-block:: html+twig

{% set voter_decision = access_decision('post_edit', post) %}

Check failure on line 2600 in security.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Twig] Unknown "access_decision" function.
{% if voter_decision.isGranted() %}
{# ... #}
{% else %}
{# before showing voter messages to end users, make sure it's safe to do so #}
<p>{{ voter_decision.message }}</p>
{% endif %}

{% set voter_decision = access_decision('post_edit', post, anotherUser) %}
{% if voter_decision.isGranted() %}
{# ... #}
{% else %}
<p>The {{ anotherUser.name }} user doesn't have sufficient permission:</p>
{# before showing voter messages to end users, make sure it's safe to do so #}
<p>{{ voter_decision.message }}</p>
{% endif %}

.. versionadded:: 7.4

The ``access_decision()`` and ``access_decision_for_user()`` Twig functions
were introduced in Symfony 7.4.

.. _security-isgrantedforuser:

Securing other Services
Expand Down Expand Up @@ -2642,6 +2670,42 @@
The :method:`Symfony\\Bundle\\SecurityBundle\\Security::isGrantedForUser`
method was introduced in Symfony 7.3.

You can also use the ``getAccessDecision()`` and ``getAccessDecisionForUser()``
methods to check authorization and get to retrieve the reasons for denying
permission in :ref:`your custom security voters <creating-the-custom-voter>`::

// src/SalesReport/SalesReportManager.php

// ...
use Symfony\Bundle\SecurityBundle\Security;

class SalesReportManager
{
public function __construct(
private Security $security,
) {
}

public function generateReport(): void
{
$voterDecision = $this->security->getAccessDecision('ROLE_SALES_ADMIN');
if ($voterDecision->isGranted('ROLE_SALES_ADMIN')) {
// ...
} else {
// do something with $voterDecision->getMessage()
}

// ...
}

// ...
}

.. versionadded:: 7.4

The ``getAccessDecision()`` and ``getAccessDecisionForUser()`` methods
were introduced in Symfony 7.4.

If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
Symfony will automatically pass the ``security.helper`` to your service
thanks to autowiring and the ``Security`` type-hint.
Expand Down
2 changes: 2 additions & 0 deletions security/voters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ calls out to the "voter" system. Right now, no voters will vote on whether or no
the user can "view" or "edit" a ``Post``. But you can create your *own* voter that
decides this using whatever logic you want.

.. _creating-the-custom-voter:

Creating the custom Voter
-------------------------

Expand Down
Loading