Symfony4 - 如何使用选民过滤实体类型字段



我需要过滤带有选民的实体类型字段中显示的选项。

我有一个用户实体,它与CustomerGroup,CustomerEntity和CustomerSite有一些关系。 例如,我有一个选民,客户组。我可以根据列表视图中当前用户的角色筛选结果。我使用"array_filter"函数使其工作。用户对象示例:

$users = array_filter($users, function (User $user) {
return $this->isGranted('view', $user);
});

我用谷歌搜索了很多页面,但没有任何成功!我尝试在客户组存储库中创建一个自定义函数,并从 FormType 中的query_builder选项调用它:它会抛出错误。见下文: 客户组存储库.php :

{
$customerGroups = $this->createQueryBuilder('cg')
->orderBy('cg.name', 'ASC')
->getQuery()->getArrayResult();
$customerGroups = array_filter($customerGroups, function (CustomerGroup $group) {
return $this->security->isGranted('view', $group);
});
return $customerGroups;
}

以及带有query_builder选项的构建表单函数:

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email')
->add('firstName')
->add('lastName')
->add('isActive')
->add('CustomerGroup', EntityType::class, [
'class' => CustomerGroup::class,
'label' => 'name',
'query_builder' => function(CustomerGroupRepository $er) {
return $er->findAllGranted();
},
'is_granted_disabled' => $options['is_granted_disabled'],
'is_granted_attribute' => 'ROLE_ADMIN',
'is_granted_subject_path' => 'parent.data',
'choice_label' => 'name',
'multiple' => false,
'expanded' => false
]);
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetDataEntity'));
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmitEntity'));
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetDataSite'));
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmitSite'));

我得到的错误:

Argument 1 passed to AppRepositoryCustomerGroupRepository::AppRepository{closure}() must be an instance of AppEntityCustomerGroup, array given

群选民.php :


namespace AppSecurityVoter;
use AppEntityCustomerGroup;
use AppEntityUser;
use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
use SymfonyComponentSecurityCoreAuthorizationVoterVoter;
use SymfonyComponentSecurityCoreSecurity;
use SymfonyComponentSecurityCoreUserUserInterface;
class GroupVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject)
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['view', 'edit'])
&& $subject instanceof CustomerGroup;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case 'edit':
// logic to determine if the user can EDIT
// return true or false
break;
case 'view':
return $this->canView($subject, $user);
break;
}
return false;
}
/**
* @param CustomerGroup $object
* @param User $loggedUser
* @return bool
*/
public function canView(CustomerGroup $object, User $loggedUser)
{
if($this->security->isGranted('ROLE_ADMIN'))
return true;
elseif($this->security->isGranted('ROLE_GROUP_MANAGER'))
{
if($object === $loggedUser->getCustomerGroup())
return true;
}
elseif($this->security->isGranted('ROLE_ENTITY_MANAGER'))
{
if($object === $loggedUser->getCustomerEntity()->getCustomerGroup())
return true;
}
elseif($this->security->isGranted('ROLE_TECHNICIAN'))
{
$customerSites = $loggedUser->getCustomerSites();
foreach ($customerSites as $site)
{
static $retour = false;
if($site->getCustomerEntity()->getCustomerGroup() === $object)
$retour = true;
}
return $retour;
}

return false;
}
}

用户表单类型.php :


namespace AppForm;
use AppEntityCustomerEntity;
use AppEntityCustomerGroup;
use AppEntityCustomerSite;
use AppEntityUser;
use AppRepositoryCustomerGroupRepository;
use DoctrineORMEntityManagerInterface;
use DoctrineORMEntityRepository;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentFormFormEvent;
use SymfonyComponentFormFormEvents;
use SymfonyComponentFormFormInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;

class UserFormType extends AbstractType
{
private $tokenStorage;
private $em;
public function __construct(TokenStorageInterface $tokenStorage, EntityManagerInterface $em)
{
$this->tokenStorage = $tokenStorage;
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email')
->add('firstName')
->add('lastName')
->add('isActive')
->add('CustomerGroup', EntityType::class, [
'class' => CustomerGroup::class,
'label' => 'name',
'is_granted_disabled' => $options['is_granted_disabled'],
'is_granted_attribute' => 'ROLE_ADMIN',
'is_granted_subject_path' => 'parent.data',
'choice_label' => 'name',
'multiple' => false,
'expanded' => false
]);
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetDataEntity'));
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmitEntity'));
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetDataSite'));
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmitSite'));
}
...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
'is_granted_attribute' => null,
'is_granted_subject_path' => null,
'is_granted_hide' => false,
'is_granted_disabled' => false
]);
}
}

表单中"客户组"字段中可用的选项不会被Symfony过滤。 我可以看到所有客户组,即使我的角色不允许它们。

编辑:也许我应该使用"choises"属性。我需要访问客户组存储库才能做到这一点!

感谢您的帮助!

最好

朱利安

感谢您的提示!

最后,我设法通过在存储库中创建自定义"FindByGranted"函数来使其工作。 佛的例子:

public function findAllGranted()
{
$customerGroups = $this->createQueryBuilder('cg')
->orderBy('cg.name', 'ASC')
->getQuery()->execute();
$customerGroups = array_filter($customerGroups, function (CustomerGroup $group) {
return $this->security->isGranted('view', $group);
});
return $customerGroups;
}

然后,我从 formType 调用此函数。

谢谢大家。

问候

最新更新