2016-05-13 55 views
0

我正在嘗試在我的項目中實施FOSUserBundle。 我剛剛設置了Group功能,創建了一個組併爲其添加了一個用戶。 真正讓我困惑的是用戶不像我期望的那樣繼承組角色。我的期望是,如果用戶有一個組,例如角色ROLE_ADMIN,那麼該用戶也將具有該角色。 因此,像Symfony FOSUserBundle集團目的

if (!$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { 
     throw $this->createAccessDeniedException(); 
    } 

不會拋出異常,但它確實 要我違背什麼文檔這裏

組中的角色說,將要賦予所有用戶屬於它。

所以我的問題是,我該如何正確使用組? 我是否應該至少容納一個組中的所有用戶,並且從不檢查分配給用戶的角色,而是檢查他們的角色?

回答

1
  • 服務security.context與上述更改一起被棄用。推薦 改用: @security.authorization_checker => isGranted() @security.token_storage => getToken() @security.token_storage => setToken()

所以才:

if($this->isGranted('ROLE_ADMIN')) 

Referre到Symfony的組件:

protected function isGranted($attributes, $object = null) 
    { 
     if (!$this->container->has('security.authorization_checker')) { 
      throw new \LogicException('The SecurityBundle is not registered in your application.'); 
     } 

     return $this->container->get('security.authorization_checker')->isGranted($attributes, $object); 
    } 
+0

謝謝!測試時我犯了一個錯誤。我沒有註銷並返回,導致分析器顯示我的用戶沒有分配任何角色。那麼現在它完美的作品 – Kable