2016-05-18 10 views
0

私はsymfony 2.8プロジェクトに参加しています。別の開発者は、ユーザーが特定のタスクに対して権限を持っているかどうかを確認する投票者を書いています。私はすでに問題なくコントローラでこの機能を使用しますが、今、私は動的メニューを取得するためにサービスを書いていると私は方法isGrantedにアクセスする方法がわからない:サービス内の投票者メソッドにアクセスするにはどうすればいいですか

Error: Attempted to call an undefined method named "isGranted" of class.

namespace NameSpaceQuestion\Question\Service; 

use Doctrine\ORM\EntityManager; 

class MenuBuilder 
{ 

    private $areasTools; 
    public $menuItems = array(); 
    private $em; 

    public function __construct(EntityManager $em) 
    { 
     $this->em = $em; 
    } 

    public function createMenuCourse($course,$mode,$user) 
    { 
     $repository = $this->em->getRepository('eBundle:AreasTools'); 
     $areas = $repository->findAll(); 
     //Retrieve every area from the db 
     $itemsMenu = array(); 

     foreach ($areas as $area) { 

      //If the user has permissions the area is included in the menu and proceed to check the tools that belong to the current area 
      if($this->isGranted($mode,$area,$user)){ 
       $itemsMenu[$area->getName()] = array(); 
       $toolsPerCourse = $this->em->getRepository('eBundle:CourseTool')->findByAreaToolAndCourse($area, $course); 
       foreach ($toolsPerCourse as $toolCourse) { 
        //If the user has permissions the tool is included in the menu under the respective Area 
        if(($this->isGranted($mode,$toolCourse,$user))){ 
         array_push($itemsMenu[$area->getName()], $toolCourse->getName()); 
        } 
       } 

      } 
     } 

     return $itemsMenu; 
    } 
} 

答えて

1

あなたはよMenuBuilderクラスのAuthorizationCheckerを取得するには、Dependency Injectionを使用する必要があります。あなたはここでそれについて読むことができます。http://symfony.com/doc/current/cookbook/security/securing_services.html

すでにEntityManagerを注入しているように見えるので、ちょうどあなたのコードにAuthorizationCheckerを追加:理解する

use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; 

class MenuBuilder 
{ 

    protected $authorizationChecker; 

    public function __construct(EntityManager $em, AuthorizationCheckerInterface $authorizationChecker) 
    { 
     $this->authorizationChecker = $authorizationChecker; 
     $this->em = $em; 
    } 

    function createMenuCourse() 
    { 
     if ($this->authorizationChecker->isGranted('EDIT',$user)) { 
       //build menu 
     } 
    } 
} 
0

最初のものはsymfonyのベースコントローラが持っているということですisGrantedのようないくつかのヘルパー関数が実装されています。しかし、もちろん、コントローラの中にいなければ、それらにアクセスすることはできません。一方、基本クラスを見て必要な機能をコピーすることは有益です。

class MenuBuilder 
{ 
    private $em; 
    private $authorizationChecker; 

    public function __construct(
     EntityManager $em, 
     $authorizationChecker // security.authorization_checker 
    ) { 
     $this->em = $em; 
     $this->authorizationChecker = $authorizationChecker; 
    }  
    protected function isGranted($attributes, $object = null) 
    { 
     return $this->authorizationChecker->isGranted($attributes, $object); 
    } 

ダーンそれ:

isGranted機能を使用すると、のようなものが得られ、あなたのメニュービルダーに注入する必要があります。承認チェッカーサービスに依存しています。ステファンは1分で私を倒した。より速く入力することを学ぶ必要があります。

関連する問題