Symfony 检查是否授予了活动用户以外的其他用户

Symfony 检查是否授予了活动用户以外的其他用户,symfony,authorization,Symfony,Authorization,我们有一些前端逻辑,使用投票者检查用户是否可以显示文档。同样的逻辑应该应用于后端,不同之处在于,在那里,一个管理用户是令牌存储中的活动用户 应该使用相同的投票者,但与另一个用户对象(文档的所有者)一起使用。找不到有关临时更改投票者的已验证用户的任何文档 尽管如此,我无法想象覆盖令牌将是一条可行之路。那么,我们是否可以使用Symfony服务对我们自己的用户进行投票?令牌存储中当前尚未激活的一个?尝试创建另一个authorizationChecker服务 <service id="app.to

我们有一些前端逻辑,使用投票者检查用户是否可以显示文档。同样的逻辑应该应用于后端,不同之处在于,在那里,一个管理用户是令牌存储中的活动用户

应该使用相同的投票者,但与另一个用户对象(文档的所有者)一起使用。找不到有关临时更改投票者的已验证用户的任何文档


尽管如此,我无法想象覆盖令牌将是一条可行之路。那么,我们是否可以使用Symfony服务对我们自己的用户进行投票?令牌存储中当前尚未激活的一个?

尝试创建另一个authorizationChecker服务

<service id="app.token_storage" class="Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage" />

<service id="app.authorization_checker" class="Symfony\Component\Security\Core\Authorization\AuthorizationChecker">
      <argument type="service" id="app.token_storage"/>
      <argument type="service" id="security.authentication.manager"/>
      <argument type="service" id="security.access.decision_manager"/>
      <argument>false</argument>
</service>

尝试创建另一个authorizationChecker服务

<service id="app.token_storage" class="Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage" />

<service id="app.authorization_checker" class="Symfony\Component\Security\Core\Authorization\AuthorizationChecker">
      <argument type="service" id="app.token_storage"/>
      <argument type="service" id="security.authentication.manager"/>
      <argument type="service" id="security.access.decision_manager"/>
      <argument>false</argument>
</service>
对于RoleVoter Decedents,您可以在检查权限后交换存储的令牌并将其还原回来:

$tokenStorage = $this->get('security.token_storage');
$authorizationChecker = $this->get('security.authorization_checker');

// gets the user owner of the document
$owner = $document->getOwner();

// create new token for owner user/roles
$ownerToken = new AnonymousToken(null, $owner, $owner->getRoles());

// save current token to restore later
$prevToken = $tokenStorage->getToken();

// change the current token
$tokenStorage->setToken($ownerToken);

// checking now the owner roles
if ($authorizationChecker->isGranted('show', $document)) {
    // ...
}

// restore the previous token
$tokenStorage->setToken($prevToken);
这些更改仅影响此时的访问决策。为了可重用性,您可以创建一个新的AuthorizationChecker/服务,将用户实例用作IsGrated方法的第三个参数,如下所示:

$user = $document->getOwner();

if ($this->get('my_authorization_checker')->isGranted('show', $document, $user)) {
    // ...
}
此方法将首先检查是否提供了用户,如果提供了,则更改令牌,否则不执行任何操作并检查已登录的用户。

对于RoleVoter Decents,您可以在检查权限后交换存储的令牌并将其还原回来:

$tokenStorage = $this->get('security.token_storage');
$authorizationChecker = $this->get('security.authorization_checker');

// gets the user owner of the document
$owner = $document->getOwner();

// create new token for owner user/roles
$ownerToken = new AnonymousToken(null, $owner, $owner->getRoles());

// save current token to restore later
$prevToken = $tokenStorage->getToken();

// change the current token
$tokenStorage->setToken($ownerToken);

// checking now the owner roles
if ($authorizationChecker->isGranted('show', $document)) {
    // ...
}

// restore the previous token
$tokenStorage->setToken($prevToken);
这些更改仅影响此时的访问决策。为了可重用性,您可以创建一个新的AuthorizationChecker/服务,将用户实例用作IsGrated方法的第三个参数,如下所示:

$user = $document->getOwner();

if ($this->get('my_authorization_checker')->isGranted('show', $document, $user)) {
    // ...
}

此方法将首先检查是否提供了用户,如果提供了,则更改令牌,否则不执行任何操作并检查已登录的用户。

请显示投票者源代码?请显示投票者源代码?这也将覆盖请求范围内的令牌,因此,您提到的代码片段也可以直接应用。因此,我认为没有其他方法可以让您不必自己还原活动令牌?它不会覆盖请求的令牌。通过定义app.token_存储,您正在创建另一个分数,无论您将在其中放入什么,standart security.token_存储都将保留来自请求的令牌。您将有两个令牌存储和两个授权检查器,每个都在检查自己的存储。这也将覆盖请求范围内的令牌,因此您提到的代码片段也可以直接应用。因此,我认为没有其他方法可以让您不必自己还原活动令牌?它不会覆盖请求的令牌。通过定义app.token_存储,您正在创建另一个分数,无论您将在其中放入什么,standart security.token_存储都将保留来自请求的令牌。您将有两个token_存储和两个authorization_Checker,每个都在检查自己的存储。我觉得奇怪的是,不直接支持通过传递UserInterface对象来检查授权。我觉得奇怪的是,不直接支持通过传递UserInterface对象来检查授权。反对的理由是什么?