2014-09-19 45 views
0

我需要在Web門戶上開發一個帶有Silex框架的選民系統(基於Symfony組件)。選民是否存在優先概念?

這些不同的選民會檢查當前用戶是否在良好的國家,如果他subcibes到哪個節目,如果他激活網站上的廣告,...我用他們與Unanime規則。

但我也想使用角色系統,我需要這個角色選民優先於其他角色。

也就是說,如果角色選民棄權,那麼其他選民可以以共識決定作出決定,在任何其他情況下,這是我希望得到的共識。

Symfony是否提供了它的工具?我已經用Affirmative和Unanime決策管理矩陣模擬了這些案例,但是我沒有找到如何讓角色選舉比其他更重要。

回答

1

其實你必須寫自己AccessDecisionManager來執行它:

在我來說,我需要的是RoleHierarchyVoter覆蓋其他票,節選如果棄權。如果棄權我使用一個一致的策略:

class AccessDecisionManager implements AccessDecisionManagerInterface { 

    private $voters; 

    public function __construct(array $voters) { 
     $this->voters = $voters; 
    } 

    public function decide(TokenInterface $token, array $attributes, $object = null) { 
     $deny = 0; 

     foreach ($this->voters as $voter) { 
      $result = $voter->vote($token, $object, $attributes); 
      if ($voter instanceof RoleHierarchyVoter) { 
       if ($result === VoterInterface::ACCESS_GRANTED) 
        return true; 
       elseif ($result === VoterInterface::ACCESS_DENIED) 
        return false; 
       else 
        continue; 
      }else { 
       if ($result === VoterInterface::ACCESS_DENIED) 
        $deny++; 
      } 
     } 

     if ($deny > 0) 
      return false; 
     else 
      return true; 
    } 
} 

要註冊我的自定義的AccessDecisionManager:

$app['security.access_manager'] = $app->share(function (Application $app) { 
    return new AccessDecisionManager($app['security.voters']); 
}); 
1

您可以爲選民設置優先級:

your_voter: 
    class: # ... 
    public: false 
    arguments: 
     # ... 
    tags: 
     - { name: security.voter , priority: 255 } 
+0

你有任何鏈接到本功能集成到文檔? – Fractaliste 2014-09-19 16:57:47

+1

@Fractaliste https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml#L110 – sergekv 2014-09-19 17:05:29