Skip to content

Commit

Permalink
♻️ Fix adding Users in Admin (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b authored Sep 29, 2024
1 parent 00ac8f7 commit 5c5e559
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ services:
- App\Entity\User
- App\Controller\UserCRUDController
calls:
- [setPasswordUpdater, ['@App\Helper\PasswordUpdater']]
- [setDomainGuesser, ['@App\Guesser\DomainGuesser']]
- [setMailCryptKeyHandler, ['@App\Handler\MailCryptKeyHandler']]
- [setMailCryptVar, ["%env(MAIL_CRYPT)%"]]

userli.admin.domain:
class: App\Admin\DomainAdmin
Expand Down
48 changes: 46 additions & 2 deletions src/Admin/UserAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace App\Admin;

use Exception;
use App\Entity\User;
use App\Enum\Roles;
use App\Handler\MailCryptKeyHandler;
use App\Helper\PasswordUpdater;
use App\Traits\DomainGuesserAwareTrait;
use Exception;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
Expand All @@ -26,6 +26,10 @@ class UserAdmin extends Admin
{
use DomainGuesserAwareTrait;

private PasswordUpdater $passwordUpdater;
private MailCryptKeyHandler $mailCryptKeyHandler;
private int $mailCrypt;

protected function generateBaseRoutePattern(bool $isChildAdmin = false): string
{
return 'user';
Expand All @@ -49,6 +53,13 @@ protected function configureFormFields(FormMapper $form): void

$form
->add('email', EmailType::class, ['disabled' => !$this->isNewObject()])
->add('plainPassword', PasswordType::class, [
'label' => 'form.password',
'required' => $this->isNewObject(),
'disabled' => (null !== $userId) ? $user->hasMailCryptSecretBox() : false,
'help' => (null !== $userId && $user->hasMailCryptSecretBox()) ?
'Disabled because user has a MailCrypt key pair defined' : null,
])
->add('totp_confirmed', CheckboxType::class, [
'label' => 'form.twofactor',
'required' => false,
Expand Down Expand Up @@ -157,6 +168,22 @@ protected function configureBatchActions($actions): array
return $actions;
}

/**
* @param User $object
*
* @throws Exception
*/
public function prePersist($object): void
{
$this->passwordUpdater->updatePassword($object, $object->getPlainPassword());
if (null !== $object->hasMailCrypt()) {
$this->mailCryptKeyHandler->create($object, $object->getPlainPassword());
}
if (null === $object->getDomain() && null !== $domain = $this->domainGuesser->guess($object->getEmail())) {
$object->setDomain($domain);
}
}

/**
* @param User $object
*/
Expand All @@ -167,12 +194,29 @@ public function preUpdate($object): void
throw new AccessDeniedException('Not allowed to edit admin user');
}

$object->updateUpdatedTime();
if (!empty($object->getPlainPassword())) {
$this->passwordUpdater->updatePassword($object, $object->getPlainPassword());
} else {
$object->updateUpdatedTime();
}

if (false === $object->getTotpConfirmed()) {
$object->setTotpSecret(null);
$object->setTotpConfirmed(false);
$object->clearBackupCodes();
}
}

public function setPasswordUpdater(PasswordUpdater $passwordUpdater): void
{
$this->passwordUpdater = $passwordUpdater;
}
public function setMailCryptKeyHandler(MailCryptKeyHandler $mailCryptKeyHandler): void
{
$this->mailCryptKeyHandler = $mailCryptKeyHandler;
}
public function setMailCryptVar(string $mailCrypt): void
{
$this->mailCrypt = (int) $mailCrypt;
}
}

0 comments on commit 5c5e559

Please sign in to comment.