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

查看:59
本文介绍了使用FOSUserBundle更改另一个用户的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();

这篇关于使用FOSUserBundle更改另一个用户的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆