Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e8d2b6

Browse files
committed
minor #20388 [Security] use access decision manager to control which token to vote on (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Security] use access decision manager to control which token to vote on Following symfony/symfony#58754: calling. `Security::isGranted()` inside a voter has the drawback that we do not know if the checks performed here act on the same token that we have in our voter as the token inside the token storage might have change or may change in between. Commits ------- fc0030a use access decision manager to control which token to vote on
2 parents 1b363c4 + fc0030a commit 0e8d2b6

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

security/impersonating_user.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,17 @@ logic you want::
309309
namespace App\Security\Voter;
310310

311311
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
312+
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
312313
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
313-
use Symfony\Component\Security\Core\Security;
314314
use Symfony\Component\Security\Core\User\UserInterface;
315315

316316
class SwitchToCustomerVoter extends Voter
317317
{
318-
private $security;
318+
private $accessDecisionManager;
319319

320-
public function __construct(Security $security)
320+
public function __construct(AccessDecisionManager $accessDecisionManager)
321321
{
322-
$this->security = $security;
322+
$this->accessDecisionManager = $accessDecisionManager;
323323
}
324324

325325
protected function supports($attribute, $subject): bool
@@ -337,12 +337,12 @@ logic you want::
337337
}
338338

339339
// you can still check for ROLE_ALLOWED_TO_SWITCH
340-
if ($this->security->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
340+
if ($this->accessDecisionManager->isGranted($token, ['ROLE_ALLOWED_TO_SWITCH'])) {
341341
return true;
342342
}
343343

344344
// check for any roles you want
345-
if ($this->security->isGranted('ROLE_TECH_SUPPORT')) {
345+
if ($this->accessDecisionManager->isGranted($token, ['ROLE_TECH_SUPPORT'])) {
346346
return true;
347347
}
348348

security/voters.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -222,33 +222,33 @@ Checking for Roles inside a Voter
222222
---------------------------------
223223

224224
What if you want to call ``isGranted()`` from *inside* your voter - e.g. you want
225-
to see if the current user has ``ROLE_SUPER_ADMIN``. That's possible by injecting
226-
the :class:`Symfony\\Component\\Security\\Core\\Security`
227-
into your voter. You can use this to, for example, *always* allow access to a user
225+
to see if the current user has ``ROLE_SUPER_ADMIN``. That's possible by using an
226+
:class:`access decision manager <Symfony\\Component\\Security\\Core\\Authorization\\AccessDecisionManagerInterface>`
227+
inside your voter. You can use this to, for example, *always* allow access to a user
228228
with ``ROLE_SUPER_ADMIN``::
229229

230230
// src/Security/PostVoter.php
231231

232232
// ...
233-
use Symfony\Component\Security\Core\Security;
233+
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
234234

235235
class PostVoter extends Voter
236236
{
237237
// ...
238238

239-
private $security;
239+
private $accessDecisionManager;
240240

241-
public function __construct(Security $security)
241+
public function __construct(AccessDecisionManagerInterface $accessDecisionManager)
242242
{
243-
$this->security = $security;
243+
$this->accessDecisionManager = $accessDecisionManager;
244244
}
245245

246246
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
247247
{
248248
// ...
249249

250250
// ROLE_SUPER_ADMIN can do anything! The power!
251-
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
251+
if ($this->accessDecisionManager->isGranted($token, ['ROLE_SUPER_ADMIN'])) {
252252
return true;
253253
}
254254

0 commit comments

Comments
 (0)