Forms Symfony2-基于角色访问的用户特定表单
我有一个表单,它有一个类别字段下拉列表,其中有一个(OneToMany/manytone)到一个post实体 情况:现在,客户必须在下拉列表中选择类别,他们可能会错误地选择错误的类别,这将转到另一个博客(如果他们选择了错误的类别),他们将无法将其更改回正确的类别 为了缓解这一潜在问题,我想采用以下两种方法之一作为解决方案: 1)根据他们有权访问的类别自动设置类别 2)或限制用户仅选择其有权访问的类别(例如,如果用户具有特定角色,则只能在下拉列表中获取该类别) 用户有一个ROLE_用户限制,允许他们仅CRUD他们有权访问的内容 e、 gForms Symfony2-基于角色访问的用户特定表单,forms,symfony,drop-down-menu,user-roles,Forms,Symfony,Drop Down Menu,User Roles,我有一个表单,它有一个类别字段下拉列表,其中有一个(OneToMany/manytone)到一个post实体 情况:现在,客户必须在下拉列表中选择类别,他们可能会错误地选择错误的类别,这将转到另一个博客(如果他们选择了错误的类别),他们将无法将其更改回正确的类别 为了缓解这一潜在问题,我想采用以下两种方法之一作为解决方案: 1)根据他们有权访问的类别自动设置类别 2)或限制用户仅选择其有权访问的类别(例如,如果用户具有特定角色,则只能在下拉列表中获取该类别) 用户有一个ROLE_用户限制,允许他
- ROLEUSER1只能访问/category1(并且可以在此上使用CRUD)
- ROLEUSER2只能访问/category2(并且可以在此上使用CRUD)
- ROLEUSER3只能访问/category3(并且可以在此上使用CRUD)
public function job1CreateAction(Request $request)
{
$entity = new Post();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('job1_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
private function createCreateForm(Post $entity)
{
$form = $this->createForm(new PostType(), $entity, array(
'action' => $this->generateUrl('job1_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
这个怎么样
从表单生成器中删除“类别”字段,并在控制器操作中手动设置它:
if ($this->get('security.context')->isGranted('ROLEUSER1') {
$entity->setCategory(CATEGORY1);
}
public function job1CreateAction(Request $request)
{
$entity = new Post();
if ($this->get('security.context')->isGranted('ROLEUSER1') {
$category1 = $this->getDoctrine()->getManager()->getRepository('MYBUNDLE:POST')->find(1); // we are getting category object. this is just an example cade, may be this will be different in your case
$entity->setCategory($category1);
}
$form = $this->createCreateForm($entity);
....
}
编辑:
控制器操作:
if ($this->get('security.context')->isGranted('ROLEUSER1') {
$entity->setCategory(CATEGORY1);
}
public function job1CreateAction(Request $request)
{
$entity = new Post();
if ($this->get('security.context')->isGranted('ROLEUSER1') {
$category1 = $this->getDoctrine()->getManager()->getRepository('MYBUNDLE:POST')->find(1); // we are getting category object. this is just an example cade, may be this will be different in your case
$entity->setCategory($category1);
}
$form = $this->createCreateForm($entity);
....
}
及表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('body')
->add('author')
//->add('category')
->add('file', 'file', array(
'label' => 'Image',
'required' => false
))
->add('created');
}
关于这种方法的问题,我使用的是CRUD生成
原则:生成:CRUD
,那么这在控制器中的位置如何,您能举个例子吗?@如果您使用原则生成操作,您应该查看控制器文件夹中的后控制器。必须采取行动。。只需编辑该操作。。对,但在createAction中的何处,更新了我的查询以显示createAction和createForm操作。@我更新了答案,添加了一些示例code@chancefind()方法通过id获取对象,您可以找到一些信息