从管理面板编辑用户角色(Symfony2 和 FOSUserBundle)
Edit user role from an admin panel (Symfony2 and FOSUserBundle)
我已经使用自制的 LDAP 控制器完全设置了 FOSUserBundle,它允许用户使用他们已经存在的 Active Directory 登录名登录。
我想要的是允许管理员从后端编辑任何用户角色。知道我该怎么做吗?
(我不想使用 SonataBundle)。
我已经制作了 FormType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$permissions = array(
'ROLE_USER' => 'Aucun droit',
'ROLE_CONSULT' => 'Consultation',
'ROLE_SUPER_ADMIN' => 'Administrateur'
);
$builder
->add(
'id',
'entity',
array(
'class' => 'MainBundle:Users',
'property' => 'displayName',
'label' => 'Choisir l\'utilisateur',
'attr' => array(
'class' => "select2"
)
)
)
->add(
'role',
'choice',
array(
'label' => 'Rôle à attribuer',
'choices' => $permissions,
'attr' => array(
'class' => "select2"
)
)
)
->add(
'save',
'submit',
array(
'label' => 'Sauvegarder',
'attr' => array(
'class' => 'btn'
)
)
);
}
还有控制器,我真的不确定,因为我不知道什么时候 select 指定的用户可以编辑 :
public function editUserAction(Request $request)
{
$request = $this->container->get('request');
$editUser = $this->getDoctrine()->getRepository('MainBundle:Users')->findAll();
$formEditUser = $this->createForm(new ChangeUserRoleType(), $editUser);
$formEditUser->handleRequest($request);
if ($formEditUser->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($editUser);
$em->flush();
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
好的,我解决了。
窗体的 BuildForm :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$permissions = array(
'ROLE_USER' => 'First role',
'ROLE_CONSULT' => 'Second role',
'ROLE_SUPER_ADMIN' => 'Third role'
);
$builder
->add(
'id',
'entity',
array(
'class' => 'MainBundle:Users',
'property' => 'displayName',
'label' => 'Choose the user',
)
)
->add(
'role',
'choice',
array(
'label' => 'Choose the role',
'choices' => $permissions,
)
)
->add(
'save',
'submit'
);
}
控制器上的功能:
public function editRoleAction(Request $request)
{
$request = $this->container->get('request');
$formEditUser = $this->createForm(new ChangeUserRoleType());
$formEditUser->handleRequest($request);
if ($formEditUser->isValid()) {
// Getting the variable of the form
$selectedUser = $request->request->get('value');
// Getting the user infos
$editUser = $this->getDoctrine()->getRepository('MainBundle:Users')->find($selectedUser);
// Using the UserManager (from the FOSUserBundle)
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->findUserByUsername($editUser->getUsername());
// Changing the role of the user
$user->setRoles(array($selectedUser['role']));
// Updating the user
$userManager->updateUser($user);
}
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
我相信您可以根据需要调整它以添加更多角色,例如在选择字段中将选项 "multiple" 设置为 "true"。
我希望我能帮助遇到同样问题的任何人!
我已经使用自制的 LDAP 控制器完全设置了 FOSUserBundle,它允许用户使用他们已经存在的 Active Directory 登录名登录。
我想要的是允许管理员从后端编辑任何用户角色。知道我该怎么做吗? (我不想使用 SonataBundle)。
我已经制作了 FormType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$permissions = array(
'ROLE_USER' => 'Aucun droit',
'ROLE_CONSULT' => 'Consultation',
'ROLE_SUPER_ADMIN' => 'Administrateur'
);
$builder
->add(
'id',
'entity',
array(
'class' => 'MainBundle:Users',
'property' => 'displayName',
'label' => 'Choisir l\'utilisateur',
'attr' => array(
'class' => "select2"
)
)
)
->add(
'role',
'choice',
array(
'label' => 'Rôle à attribuer',
'choices' => $permissions,
'attr' => array(
'class' => "select2"
)
)
)
->add(
'save',
'submit',
array(
'label' => 'Sauvegarder',
'attr' => array(
'class' => 'btn'
)
)
);
}
还有控制器,我真的不确定,因为我不知道什么时候 select 指定的用户可以编辑 :
public function editUserAction(Request $request)
{
$request = $this->container->get('request');
$editUser = $this->getDoctrine()->getRepository('MainBundle:Users')->findAll();
$formEditUser = $this->createForm(new ChangeUserRoleType(), $editUser);
$formEditUser->handleRequest($request);
if ($formEditUser->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($editUser);
$em->flush();
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
好的,我解决了。
窗体的 BuildForm :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$permissions = array(
'ROLE_USER' => 'First role',
'ROLE_CONSULT' => 'Second role',
'ROLE_SUPER_ADMIN' => 'Third role'
);
$builder
->add(
'id',
'entity',
array(
'class' => 'MainBundle:Users',
'property' => 'displayName',
'label' => 'Choose the user',
)
)
->add(
'role',
'choice',
array(
'label' => 'Choose the role',
'choices' => $permissions,
)
)
->add(
'save',
'submit'
);
}
控制器上的功能:
public function editRoleAction(Request $request)
{
$request = $this->container->get('request');
$formEditUser = $this->createForm(new ChangeUserRoleType());
$formEditUser->handleRequest($request);
if ($formEditUser->isValid()) {
// Getting the variable of the form
$selectedUser = $request->request->get('value');
// Getting the user infos
$editUser = $this->getDoctrine()->getRepository('MainBundle:Users')->find($selectedUser);
// Using the UserManager (from the FOSUserBundle)
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->findUserByUsername($editUser->getUsername());
// Changing the role of the user
$user->setRoles(array($selectedUser['role']));
// Updating the user
$userManager->updateUser($user);
}
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
我相信您可以根据需要调整它以添加更多角色,例如在选择字段中将选项 "multiple" 设置为 "true"。
我希望我能帮助遇到同样问题的任何人!