Symfony 4 EasyAdmin 如何加密密码? [英] Symfony 4 EasyAdmin how to encrypt passwords?

查看:37
本文介绍了Symfony 4 EasyAdmin 如何加密密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 EasyAdmin 添加/编辑用户,想询问是否可以加密您的密码?密码加密以前在我使用 Symfony 4 make:registration-form 时有效,但我现在不能使用,我必须使用 EasyAdmin.

I am using EasyAdmin to add/edit users and wanted to ask if there is a possibility of encrypting your passwords? Password encryption worked previously when I used the Symfony 4 make:registration-form but I can't use that now, I have to use EasyAdmin.

easy_admin.yaml

easy_admin.yaml

easy_admin:
  entities:
    User:
     class: App\Entity\User
     password_encoding: { algorithm: 'bcrypt', cost: 12 }

(实际)我进入EasyAdmin页面(/admin),点击User,Add User,填写邮箱(test@gmail.com)和密码(test),点击Save Changes.

(Actual) I go to EasyAdmin page (/admin), click User, Add User, fill in email (test@gmail.com) and password (test), click Save Changes.

现在用户存储在数据库中,但使用明文密码.

Now the user is stored in the database but with plaintext password.

(预期)以上所有,但密码已加密.

(Expected) All of the above but password is encrypted.

推荐答案

来自我基于 Symfony 5 和 PHP 7.4 的工作项目的代码

  1. 扩展EasyAdminController:

<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

/**
 * Class AdminController.
 *
 * @author Dmitriy Atamaniuc <d.atamaniuc@gmail.com>
 */
final class AdminController extends EasyAdminController
{
    private UserPasswordEncoderInterface $encoder;

    private function setUserPlainPassword(User $user): void
    {
        if ($user->getPlainPassword()) {
            $user->setPassword($this->encoder->encodePassword($user, $user->getPlainPassword()));
        }
    }

    /**
     * @required
     */
    public function setEncoder(UserPasswordEncoderInterface $encoder): void
    {
        $this->encoder = $encoder;
    }

    public function persistUserEntity(User $user): void
    {
        $this->setUserPlainPassword($user);

        $this->persistEntity($user);
    }

    public function updateUserEntity(User $user): void
    {
        $this->setUserPlainPassword($user);

        $this->updateEntity($user);
    }
}

  1. 在 EasyAdminBundle 中注册您的控制器,编辑 config/routes/easy_admin.yaml:

easy_admin_bundle:
    resource: 'App\Controller\AdminController'
    prefix: /admin
    type: annotation

  1. plainPassword虚拟"属性添加到您的 User 实体:
  1. Add plainPassword "virtual" property to your User entity:

/**
* @Assert\NotBlank()
* @Assert\Length(min=5, max=128)
*/
private ?string $plainPassword = null;

public function getPlainPassword(): ?string
{
    return $this->plainPassword;
}

public function setPlainPassword(string $password): void
{
    $this->plainPassword = $password;
}

  1. 更新easy_admin.yaml

easy_admin:
    entities:
        User:
            class: App\Entity\User
            label: label.user
            new:
                title:  field.user.add_new
                fields:
                    # some fields like username here
                    - { property: username, label: field.user.username }
                    - { property: fullname, label: field.user.fullname, type: 'text' }
                    # plain password 
                    - { property: plainPassword, label: field.user.password, type: 'password' }

这篇关于Symfony 4 EasyAdmin 如何加密密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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