2013-12-16 76 views
0

我想我已經按照所有步驟創建了一個選舉器,以允許用戶只編輯他們創建的對象。自定義安全選民問題Symfony2

1)應用程序/配置/ services.yml

wars.profesorbundle.security.ownervoter : 
    class: Wars\ProfesorBundle\Security\OwnerVoter 

2)OwnerVoter.php

<?php 

namespace Wars\ProfesorBundle\Security ; 

use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface ; 

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface ; 

class OwnerVoter implements VoterInterface 
{ 
    public function supportsAttribute($attribute) 
    { 
     return 'ROLE_EDITAR_MENSAJE' == $attribute; 
    } 

    public function supportsClass($class) 
    { 
     return true; 
    } 

    public function vote(TokenInterface $token, $object, array $attributes) 
    { 
     $vote = VoterInterface::ACCESS_ABSTAIN; 

     foreach ($attributes as $attribute) { 

      if (false === $this->supportsAttribute($attribute)) { 
       continue; 
      } 

      $user = $token->getUser(); 
      $vote = VoterInterface::ACCESS_DENIED; 

      // Check that the message being edited was published by the same teacher 
      if ($object->getProfesor()->getId() === $user->getId()) { 
       $vote = VoterInterface::ACCESS_GRANTED ; 
      } 
     } 

     return $vote; 
    } 
} 

我不知道哪裏是錯誤的,因爲我總是得到一個否定的例外:

if (false === $this->get('security.context')->isGranted('ROLE_EDITAR_MENSAJE', $panel)) 

回答

1

問題出現在app/config/config.yml中。我忘了導入應用程序/配置/ services.yml:

// config.yml

進口:

- {} services.yml