Php 在Symfony2中使用来自不同bundle的多个投票者
我最初在我的一个包中创建了一个投票者,并且毫无问题地使用了它。但是,我在另一个包中创建了另一个包,因为它使用不同的类,并且决定是否让用户通过的方式也不同 问题是,尽管我遵循了与第一个完全相同的步骤,但第二个投票人没有被检测到 我做错了什么?是否可以只创建和使用一个投票人 因此,这是第一个正常工作的,我完全按照文档所说的那样做了 将其注册为服务Php 在Symfony2中使用来自不同bundle的多个投票者,php,symfony,symfony-security,Php,Symfony,Symfony Security,我最初在我的一个包中创建了一个投票者,并且毫无问题地使用了它。但是,我在另一个包中创建了另一个包,因为它使用不同的类,并且决定是否让用户通过的方式也不同 问题是,尽管我遵循了与第一个完全相同的步骤,但第二个投票人没有被检测到 我做错了什么?是否可以只创建和使用一个投票人 因此,这是第一个正常工作的,我完全按照文档所说的那样做了 将其注册为服务 services: security.access.support_voter: class: SupportMessageBu
services:
security.access.support_voter:
class: SupportMessageBundle\Security\Voter\SupportVoter
public: false
tags:
- { name: security.voter }
创造它。简要说明它的作用:检查当前用户在mySupportMessageBundle
方面的角色,mySupportMessageBundle是一个用于管理支持票证的捆绑包。我还有一个常量,用于检查票据是否由用户启动
namespace SupportMessageBundle\Security\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
use MedAppBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
class SupportVoter extends AbstractVoter
{
const SUPPORT = 'support';
const SUPERADMIN = 'superadmin';
const MEDIC = 'medic';
const ISMINE = 'ismine';
/* const EDIT = 'edit';*/
protected function getSupportedAttributes()
{
return array(self::SUPPORT, self::SUPERADMIN,self::MEDIC,self::ISMINE/*, self::EDIT*/);
}
protected function getSupportedClasses()
{
return array('MedAppBundle\Entity\User','SupportMessageBundle\Entity\Ticket');
}
protected function isGranted($attribute, $object, $user = null)
{
// make sure there is a user object (i.e. that the user is logged in)
if (!$user instanceof UserInterface) {
return false;
}
// double-check that the User object is the expected entity.
// It always will be, unless there is some misconfiguration of the
// security system.
if (!$user instanceof User) {
throw new \LogicException('The user is somehow not our User class!');
}
switch ($attribute) {
case self::SUPPORT:
// the data object could have for example a method isPrivate()
// which checks the Boolean attribute $private
{
if ($user->hasRole('ROLE_SUPPORT')||$user->hasRole('ROLE_SUPER_ADMIN')) {
return true;
}
}
break;
case self::SUPERADMIN:
// the data object could have for example a method isPrivate()
// which checks the Boolean attribute $private
{
if ($user->hasRole('ROLE_SUPER_ADMIN')) {
return true;
}
}
break;
/*case self::EDIT:
// this assumes that the data object has a getOwner() method
// to get the entity of the user who owns this data object
if ($user->getId() === $post->getOwner()->getId()) {
return true;
}
break;*/
case self::MEDIC:
// the data object could have for example a method isPrivate()
// which checks the Boolean attribute $private
{
if ($user->hasRole('ROLE_MEDIC')) {
return true;
}
}
break;
case self::ISMINE:
// the data object could have for example a method isPrivate()
// which checks the Boolean attribute $private
{
if ($user == $object->getSender()) {
return true;
}
}
break;
}
return false;
}
}
至于第二个,
我也将其注册为服务,在另一个bundle的services.yml
:
services:
security.access.features_voter:
class: MedAppBundle\Security\Voter\FeaturesVoter
public: false
tags:
- { name: security.voter }
这次我没有检查任何东西,我只是返回true
,但是每次调用它都会返回我false
,所以显然有些地方出了问题
namespace MedAppBundle\Security\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
class FeaturesVoter extends AbstractVoter
{
const ISMINE = 'ismine';
protected function getSupportedAttributes()
{
return array(self::ISMINE);
}
protected function getSupportedClasses()
{
return array('MedAppBundle/Entity/Features');
}
public function isGranted($attribute, $object, $user = null)
{
return true;
}
}
我试着这样称呼它:
$this->isGranted('issmine',$id); //returns false
$this->denyAccessUnlessGranted('issmine', $id, 'Unauthorized access!'); //denies access
$id
是一个功能
对象,它来自包含功能
实体的MedAppBundle
中的控制器
然而,第一个投票人工作正常,所以我必须在这个人的声明中做错事,否则不可能有多个
调试:容器不列出它们,甚至不列出正在工作的。这些捆绑包是在内核中加载的,因为我一直在使用它们的控制器和其他东西,只是服务似乎不起作用。所有捆绑包都有$loader->load('services.yml')
位于BundlenameExtension.php文件的DependencyInjection目录中是,您可以有多个投票者。”MedAppBundle:Entity:Features'应为'MedAppBundle\Entity\Features'。将用户类从支持的类方法中取出。可能想深入了解一下发生了什么。我做了这两件事,但仍然不起作用。我找不到一个示例,其中定义了两个投票者,每个人都只定义了一个,我查看了一下抽象类,但不确定为什么不调用我的第二个投票者。请注意,调用投票者在这两个包中都不起作用。你确定服务都被定义了吗?使用应用程序/控制台调试:服务进行验证。我在一份申请中使用了8+个不同的投票者。嗯,似乎他们不在名单上。不过,我没有debug:services。我使用了debug:container,实际上它们不在该列表中。是的,您可以有多个投票者MedAppBundle:Entity:Features'应为'MedAppBundle\Entity\Features'。将用户类从支持的类方法中取出。可能想深入了解一下发生了什么。我做了这两件事,但仍然不起作用。我找不到一个示例,其中定义了两个投票者,每个人都只定义了一个,我查看了一下抽象类,但不确定为什么不调用我的第二个投票者。请注意,调用投票者在这两个包中都不起作用。你确定服务都被定义了吗?使用应用程序/控制台调试:服务进行验证。我在一份申请中使用了8+个不同的投票者。嗯,似乎他们不在名单上。不过,我没有debug:services。我使用了debug:container,实际上它们不在该列表中