-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Security][SecurityBundle] Add is_granted feature for impersonator on AuthorizationChecker #28794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization\Voter; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager; | ||
|
||
/** | ||
* SwitchRoleVoter votes if we check the impersonator roles. | ||
* | ||
* @author Fabien Papet <fabien.papet@gmail.com> | ||
*/ | ||
class SwitchRoleVoter implements VoterInterface | ||
{ | ||
/** | ||
* @var AccessDecisionManager | ||
*/ | ||
private $accessDecisionManager; | ||
private $prefix; | ||
|
||
public function __construct(AccessDecisionManager $accessDecisionManager, string $prefix = 'IMPERSONATOR_') | ||
{ | ||
$this->accessDecisionManager = $accessDecisionManager; | ||
$this->prefix = $prefix; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function vote(TokenInterface $token, $subject, array $attributes) | ||
{ | ||
$result = VoterInterface::ACCESS_ABSTAIN; | ||
|
||
if (!$token instanceof SwitchUserToken) { | ||
return VoterInterface::ACCESS_DENIED; | ||
} | ||
|
||
foreach ($attributes as $attribute) { | ||
if (!\is_string($attribute) || 0 !== strpos($attribute, $this->prefix)) { | ||
continue; | ||
} | ||
|
||
return $this->accessDecisionManager->decide($token->getOriginalToken(), [substr($attribute, \strlen($this->prefix))], $subject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (VoterInterface::ACCESS_GRANTED === $result = $this->accessDecisionManager->decide($token->getOriginalToken(), [substr($attribute, \strlen($this->prefix))], $subject)) {
return $result;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah no this should just decorate the call forget that comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I think this is more complex, the current logic prevents passing two prefixed attributes, you should extend Voter to simplify the decoration or handle this case too IMHO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without extending, you could go with: $attributes = array_map(function ($attribute) {
return substr($attribute, \strlen($this->prefix));
}, array_filter($attributes, function ($attribute) {
return 0 === strpos($attribute, $this->prefix) }));
}));
if (!$attributes) {
return $result;
}
return $this->accessDecisionManager->decide($token->getOriginalToken(), $attributes, $subject); WDYT? |
||
} | ||
|
||
return $result; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be removed since this is inferred from the controller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from the constructor* right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this auto correction is amazing some time :D