Skip to content

Commit a20776f

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: [Security] Add `access_decision()` and `access_decision_for_user()`
2 parents 18f3c78 + a0566b3 commit a20776f

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

security.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,34 @@ the built-in ``is_granted_for_user()`` helper function:
25762576
<a href="...">Delete</a>
25772577
{% endif %}
25782578

2579+
Symfony also provides the ``access_decision()`` and ``access_decision_for_user()``
2580+
Twig functions to check authorization and to retrieve the reasons for denying
2581+
permission in :ref:`your custom security voters <creating-the-custom-voter>`:
2582+
2583+
.. code-block:: html+twig
2584+
2585+
{% set voter_decision = access_decision('post_edit', post) %}
2586+
{% if voter_decision.isGranted() %}
2587+
{# ... #}
2588+
{% else %}
2589+
{# before showing voter messages to end users, make sure it's safe to do so #}
2590+
<p>{{ voter_decision.message }}</p>
2591+
{% endif %}
2592+
2593+
{% set voter_decision = access_decision('post_edit', post, anotherUser) %}
2594+
{% if voter_decision.isGranted() %}
2595+
{# ... #}
2596+
{% else %}
2597+
<p>The {{ anotherUser.name }} user doesn't have sufficient permission:</p>
2598+
{# before showing voter messages to end users, make sure it's safe to do so #}
2599+
<p>{{ voter_decision.message }}</p>
2600+
{% endif %}
2601+
2602+
.. versionadded:: 7.4
2603+
2604+
The ``access_decision()`` and ``access_decision_for_user()`` Twig functions
2605+
were introduced in Symfony 7.4.
2606+
25792607
.. _security-isgrantedforuser:
25802608

25812609
Securing other Services
@@ -2622,6 +2650,37 @@ want to include extra details only for users that have a ``ROLE_SALES_ADMIN`` ro
26222650
is unavailable (e.g., in a CLI context such as a message queue or cron job), you
26232651
can use the ``isGrantedForUser()`` method to explicitly set the target user.
26242652

2653+
You can also use the ``getAccessDecision()`` and ``getAccessDecisionForUser()``
2654+
methods to check authorization and get to retrieve the reasons for denying
2655+
permission in :ref:`your custom security voters <creating-the-custom-voter>`::
2656+
2657+
// src/SalesReport/SalesReportManager.php
2658+
2659+
// ...
2660+
use Symfony\Bundle\SecurityBundle\Security;
2661+
2662+
class SalesReportManager
2663+
{
2664+
public function __construct(
2665+
private Security $security,
2666+
) {
2667+
}
2668+
2669+
public function generateReport(): void
2670+
{
2671+
$voterDecision = $this->security->getAccessDecision('ROLE_SALES_ADMIN');
2672+
if ($voterDecision->isGranted('ROLE_SALES_ADMIN')) {
2673+
// ...
2674+
} else {
2675+
// do something with $voterDecision->getMessage()
2676+
}
2677+
2678+
// ...
2679+
}
2680+
2681+
// ...
2682+
}
2683+
26252684
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
26262685
Symfony will automatically pass the ``security.helper`` to your service
26272686
thanks to autowiring and the ``Security`` type-hint.

security/voters.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ calls out to the "voter" system. Right now, no voters will vote on whether or no
119119
the user can "view" or "edit" a ``Post``. But you can create your *own* voter that
120120
decides this using whatever logic you want.
121121

122+
.. _creating-the-custom-voter:
123+
122124
Creating the custom Voter
123125
-------------------------
124126

0 commit comments

Comments
 (0)