Symfony 5.4 更改密码表单

Symfony 5.4 change password form

我正在使用 Symfony 5.4 来学习如何使用这个框架,现在我在管理面板 (EasyAdmin) 上工作,但我在更改密码功能上遇到了困难。

我的 DashboardController 中有这个:

#[Route('/panel/settings', name: 'settings')]
    public function settings(UserPasswordEncoderInterface $passwordEncoder): Response
    {

        return $this->render('admin/settings.html.twig');


    }

我试过使用这里的文档: https://symfony.com/doc/5.4/forms.html#creating-forms-in-controllers将代码添加到上面的函数中,但它是关于在当前实体中创建一个新条目。

我只想在 EasyAdmin 的设置页面中添加一个更改密码字段。我怎样才能做到这一点?

编辑

DashboardController.php修改后:

#[Route('/panel/settings', name: 'settings')]
    public function settings(): Response
    {

        $user = $this->security->getUser();

        $changePasswordForm = $this->createForm(UserFormType::class, $user);
        $changePasswordForm->handleRequest($adminContext->getRequest());

        if ($changePasswordForm->isSubmitted() && $changePasswordForm->isValid()) {
            $user->setPassword(HASHED_PASSWORD_HERE);
            $this->manager->persist($user);
            $this->manager->flush();
        }

    return $this->render('admin/settings.html.twig', [
    'change_password_form' => $changePasswordForm->createView(),
    ]);

    }

编辑 2 如果有人正在努力寻找信息,下面我将粘贴工作代码:

这进入控制器(例如DashboardController.php)

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Form\Type\UserFormType;
    
#[Route('/panel/settings', name: 'settings')]
        public function settings(Request $req, UserPasswordEncoderInterface $passwordEncoder): Response
        {
    
            $user = $this->security->getUser();
    
            $changePasswordForm = $this->createForm(UserFormType::class, $user);
            $changePasswordForm->handleRequest($req);
    
            if ($changePasswordForm->isSubmitted() && $changePasswordForm->isValid()) {
                $user->setPassword(
                    $passwordEncoder->encodePassword(
                        $user,
                        $changePasswordForm->get('plainPassword')->getData()
                    )
                );
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->persist($user);
                $entityManager->flush();
            }
    
        return $this->render('admin/settings.html.twig', [
        'change_password_form' => $changePasswordForm->createView(),
        ]);
    
        }

您没有提供很多细节,但我想这可能会有所帮助:

  1. 您必须像这样将 configureCrud 方法添加到您的 crud 控制器(以便您可以在那里添加另一个操作):
public function configureActions(Actions $actions): Actions
{
  $changePassword = Action::new('changePassword', 'Change Password', 'fas fa-key')->setCssClass('btn btn-label')->linkToCrudAction('changePassword');

  return $actions->add(Action::EDIT, $changePassword);
}
  1. 然后你应该在下面创建 changePassword crud action/method,(我认为你知道如何创建 symfony 表单是理所当然的):
public function changePassword(AdminContext $adminContext): Response
{
  $user = $adminContext->getEntity()->getInstance();
  $changePasswordForm = $this->createForm(UserFormType::class, $user);
  $changePasswordForm->handleRequest($adminContext->getRequest());

  if ($changePasswordForm->isSubmitted() && $changePasswordForm->isValid()) {
       $user->setPassword(HASHED_PASSWORD_HERE);
       $this->manager->persist($user);
       $this->manager->flush();
    }
  }

  return $this->render('security/change-password.html.twig', [
     'change_password_form' => $changePasswordForm->createView(),
  ]);
}
  1. 我将用户窗体放在这里也是为了更改密码:):
<?php

declare(strict_types=1);

namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('plainPassword', RepeatedType::class, [
                'type' => PasswordType::class,
                'invalid_message' => 'The password fields must match.',
                'options' => ['attr' => ['class' => 'password-field']],
                'required' => true,
                'first_options' => ['label' => 'New Password'],
                'second_options' => ['label' => 'Repeat Password'],
                'attr' => ['autocomplete' => 'off'],
            ])
            ->add('submit', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}