1
測試我新的自定義選民與戰略「一致」作爲symfony的食譜Symfony2的選民 - 我怎麼能讀哪一個拒絕訪問
建議儘管我的選民返回授予結果被拒絕:
我的選民
class OrderCardViewVoter implements VoterInterface {
private $container;
private $supportedRoles;
public function __construct($container) {
$this->container = $container;
$this->supportedRoles = array('VIEW');
}
public function supportsAttribute($attribute) {
return in_array($attribute, $this->supportedRoles);
//return $attribute === 'VIEW';
}
public function supportsClass($class) {
return true;
}
/**
* Checks whether or not the current user can edit a comment.
*
* Users with the role ROLE_COMMENT_MODERATOR may always edit.
* A comment's author can only edit within 5 minutes of it being posted.
*
* {@inheritdoc}
*/
public function vote(TokenInterface $token, $object, array $attributes)
{
$result = VoterInterface::ACCESS_ABSTAIN;
if (!$object instanceof OrderCard) {
return $result;
}
foreach ($attributes as $attribute) {
if (!$this->supportsAttribute($attribute)) {
continue;
}
$result = VoterInterface::ACCESS_DENIED;
if ($object->getEmployee()->getUser() === $token->getUser()
|| in_array('ROLE_SUPER_ADMIN', $token->getRoles())) {
return VoterInterface::ACCESS_GRANTED;
}
}
return $result;
}
我控制器
public function printAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CuculoERPBundle:OrderCard')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find OrderCard entity.');
}
$securityContext = $this->get('security.context');
if (false === $securityContext->isGranted('VIEW', $entity)) {
throw new AccessDeniedException();
}
// ...
選民返回v如果我從security.yml文件中刪除策略,則爲測試對象添加結果。