Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fc0030a

Browse filesBrowse files
committed
use access decision manager to control which token to vote on
1 parent 1b363c4 commit fc0030a
Copy full SHA for fc0030a

File tree

2 files changed

+14
-14
lines changed
Filter options

2 files changed

+14
-14
lines changed

‎security/impersonating_user.rst

Copy file name to clipboardExpand all lines: security/impersonating_user.rst
+6-6Lines changed: 6 additions & 6 deletions
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

Copy file name to clipboardExpand all lines: security/voters.rst
+8-8Lines changed: 8 additions & 8 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.