[英]Custom security voter issue 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))
問題出在app / config / config.yml中。 我忘了導入app / config / services.yml:
// config.yml
進口:
-{services.yml}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.