Skip to content

[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

Closed
wants to merge 1 commit into from
Closed
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
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
Copy link
Contributor

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the constructor* right ?

Copy link
Contributor

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

*/
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);
Copy link
Contributor

@HeahDude HeahDude Mar 11, 2019

Choose a reason for hiding this comment

The 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;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no this should just decorate the call forget that comment.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}