簡體   English   中英

為什么Symfony2 Voter一致的決策策略循環通過傳遞的屬性?

[英]Why is the Symfony2 Voter unanimous decision-strategy looping through the passed attributes?

我實現了一個自定義的Symfony2 Voter,並將一個屬性數組傳遞給denyAccessUnlessGranted的第一個參數,就像在我的控制器中一樣:

$attr = [
    'module' => 'userModule'
    'action' => 'edit'
];
$this->denyAccessUnlessGranted($attr, $this, 'Not authorize to edit user');

如果決策管理者的方法被設置為affirmative話,這可以正常工作。 然而,當我轉向unanimous方法時,由於我的自定義選民的設計方式,所有突然的事情都不起作用。 我瀏覽了Symfony源代碼,發現原因是因為確定unanimous方法的投票結果的方法在調用所有注冊選民之前循環通過屬性(而不是簡單地將它們傳遞給選民) affirmativeconsensus方法)。

下面包括Symfony/Component/Security/Core/Authorization/AccessDecisionManager片段:

private function decideAffirmative(TokenInterface $token, array $attributes, $object = null)
{
    $deny = 0;
    foreach ($this->voters as $voter) {
        $result = $voter->vote($token, $object, $attributes);
        ...
     }
}

private function decideConsensus(TokenInterface $token, array $attributes, $object = null)
{
    foreach ($this->voters as $voter) {
        $result = $voter->vote($token, $object, $attributes);
        ...
    }
}


private function decideUnanimous(TokenInterface $token, array $attributes, $object = null)
{
    $grant = 0;
    // ***** THIS IS THE ISSUE: WHY LOOP THROUGH THE ATTRIBUTES ****
    foreach ($attributes as $attribute) {
        foreach ($this->voters as $voter) {
            $result = $voter->vote($token, $object, array($attribute));
            ...
        }
    }
 }

unanimous決策的是第三個。 循環遍歷屬性的原因是什么? 這意味着我將根據我使用的策略做出重新編碼我的自定義選民,我覺得這很奇怪。

PS:我的自定義選民的實施細節對這個問題並不重要,所以我決定不把它放在這里。

PS#2:這不是我的代碼,這是來自Symfony2框架的代碼( https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Security/Core/Authorization/AccessDecisionManager.php )。 我只是想知道它背后的原因,以便我可以正確使用選民功能。 我猜最好的人回答這個問題的人就是那些熟悉Symfony2源代碼的人。

看看RoleVoter類。

如果授權作為參數傳遞的任何屬性(至少有一個允許投票),則Voter :: Vote()返回VoterInterface :: ACCESS_GRANTED。 如果有許多屬性,有些屬性已經授權,有些則沒有 - 無論如何都要投入VoterInterface :: ACCESS_GRANTED。

但是一致投票需要每個屬性都被授權(沒有拒絕投票); 因此我們需要分別測試每個屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM