使用FOSUserBundle更改另一个用户的密码[英] Change password of another user using FOSUserBundle

本文是小编为大家收集整理的关于使用FOSUserBundle更改另一个用户的密码的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在系统中有两个角色:用户和管理员.默认情况下,当某人以用户或管理员身份登录时,他可以使用实现的FosuserBundle形式修改自己的密码.但是我想禁止用户更改自己的密码,不得不向管理员请求它,因此管理员会重置它,要么引入管理员选择的新密码,要么生成随机的密码.我还想发送并向用户发送电子邮件,告诉他他的PassWD已更改,他必须从现在开始使用新的.但是我找不到这一点.有帮助吗?

推荐答案

如果您希望管理员更改其他用户的密码,则可以使用自己的表单:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username',               TextType::class, array(
                                                    'required' => true,
                                                    'label' => "Username "
                                                    ))
            ->add('email',                  TextType::class, array(
                                                    'required' => true,
                                                    'label' => "Adresse email "
                                                    ))
            ->add('plainPassword',          RepeatedType::class, array(
                                                    'type' => PasswordType::class,
                                                    'options' => array('translation_domain' => 'FOSUserBundle'),
                                                    'first_options' => array('label' => 'form.password'),
                                                    'second_options' => array('label' => 'form.password_confirmation'),
                                                    'invalid_message' => 'fos_user.password.mismatch',
                                                    ))
            ->add('roles',                  ChoiceType::class, array(
                                                    'required' => true,
                                                    'choices' => array('Salarié' => 'ROLE_SALARIE', 'Admin' => 'ROLE_ADMIN'),
                                                    'multiple' => true,
                                                    'expanded'=>true,
                                                    'label' => "Rôle ",
                                                    'label_attr' => array('class' => 'checkbox-inline')
                                                    ))
        ;
    }

//...

然后,在您的控制器中:

public function updateAction(Request $request, Member $user)
{
    $em = $this->getDoctrine()->getManager();

    $form = $this->createEditForm($user);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $userManager = $this->container->get('fos_user.user_manager');
        $userManager->updatePassword($user);
        $em->flush();

其他推荐答案

您可以通过在app/config/routing.yml文件中删除fos_user_change_password和fos_user_resetting路由来防止用户更改密码.这样,用户和管理员都无法通过/profile页面更改自己的密码.

然后,您将需要创建一个安全的控制器操作,该操作允许管理员更改密码并发送电子邮件.为此,您可以使用fosuserbundle UserManager及其setPlainPassword()方法.

看文档:

Symfony2 Security2 Security

fosuserbundle

如何发送电子邮件

本文地址:https://www.itbaoku.cn/post/2090779.html

问题描述

I have two roles in my system: users and admins. By default, when someone logs in as an user or an admin, he can modify his own password using the implemented forms of FOSUserBundle. But I'd like to forbid the users to change their own password, having to request it to the admin, so then the admin would reset it, either introducing a new one chosen by the admin, either generating a random one. I'd also like to send and a email to the user telling him that his passwd has changed and he has to use the new one from now on. But I cannot find how do that. Any help?

推荐答案

If you want an admin to change another user's password, you can use your own form:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username',               TextType::class, array(
                                                    'required' => true,
                                                    'label' => "Username "
                                                    ))
            ->add('email',                  TextType::class, array(
                                                    'required' => true,
                                                    'label' => "Adresse email "
                                                    ))
            ->add('plainPassword',          RepeatedType::class, array(
                                                    'type' => PasswordType::class,
                                                    'options' => array('translation_domain' => 'FOSUserBundle'),
                                                    'first_options' => array('label' => 'form.password'),
                                                    'second_options' => array('label' => 'form.password_confirmation'),
                                                    'invalid_message' => 'fos_user.password.mismatch',
                                                    ))
            ->add('roles',                  ChoiceType::class, array(
                                                    'required' => true,
                                                    'choices' => array('Salarié' => 'ROLE_SALARIE', 'Admin' => 'ROLE_ADMIN'),
                                                    'multiple' => true,
                                                    'expanded'=>true,
                                                    'label' => "Rôle ",
                                                    'label_attr' => array('class' => 'checkbox-inline')
                                                    ))
        ;
    }

//...

And then, in your controller:

public function updateAction(Request $request, Member $user)
{
    $em = $this->getDoctrine()->getManager();

    $form = $this->createEditForm($user);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $userManager = $this->container->get('fos_user.user_manager');
        $userManager->updatePassword($user);
        $em->flush();

其他推荐答案

You can prevent users from changing their password by removing the fos_user_change_password and fos_user_resetting routes in your app/config/routing.yml file. This way neither users nor admin will be able to change their own password through the /profile page.

Then you will need to create a secured controller action that allows admin to change passwords and send the email. In order to do that you can use FOSUserBundle UserManager and its setPlainPassword() method.

Take a look at the documentation:

Symfony2 Security

FOSUserBundle User Manager

How to send an Email