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:
entities:
User:
class: App\Entity\User
password_encoding: { algorithm: 'bcrypt', cost: 12 }
(Actual) I go to EasyAdmin page (/admin), click User, Add User, fill in email ([email protected]) 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.
New version compatible with Symfony 5:
<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
class AdminController extends EasyAdminController
{
protected function persistUserEntity($user)
{
$encodedPassword = $this->encodePassword($user, $user->getPlainPassword());
$user->setPassword($encodedPassword);
parent::persistEntity($user);
}
protected function updateUserEntity($user)
{
$encodedPassword = $this->encodePassword($user, $user->getPlainPassword());
$user->setPassword($encodedPassword);
parent::updateEntity($user);
}
private function encodePassword($user, $password)
{
$passwordEncoderFactory = new EncoderFactory([
User::class => new MessageDigestPasswordEncoder('sha512', true, 5000)
]);
$encoder = $passwordEncoderFactory->getEncoder($user);
return $encoder->encodePassword($password, $user->getSalt());
}
}
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 <[email protected]>
*/
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);
}
}
config/routes/easy_admin.yaml
:easy_admin_bundle:
resource: 'App\Controller\AdminController'
prefix: /admin
type: annotation
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;
}
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' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With