Skip to content

Commit 517423a

Browse files
committed
Add option for administrators to disable MFA for users
Signed-off-by: Tim Goudriaan <tim@codedmonkey.com>
1 parent d239891 commit 517423a

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/Controller/Dashboard/DashboardUserController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
1010
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
1111
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
12+
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
1213
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
1314
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
1415
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
@@ -68,5 +69,8 @@ public function configureFields(string $pageName): iterable
6869
->renderExpanded()
6970
->allowMultipleChoices()
7071
->setSortable(false);
72+
yield BooleanField::new('totpAuthenticationEnabled', 'Multi-factor authentication')
73+
->setHelp('form.user.help.totp-authentication-enabled')
74+
->onlyOnForms();
7175
}
7276
}

src/Doctrine/Entity/User.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ public function isTotpAuthenticationEnabled(): bool
182182
return null !== $this->totpSecret;
183183
}
184184

185+
public function setTotpAuthenticationEnabled(bool $enabled): void
186+
{
187+
if (!$this->isTotpAuthenticationEnabled() || $enabled) {
188+
throw new \LogicException(sprintf('TOTP authentication can not be enabled through the `%s` method.', __METHOD__));
189+
}
190+
191+
$this->totpSecret = null;
192+
}
193+
185194
public function getTotpAuthenticationUsername(): string
186195
{
187196
return $this->username;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodedMonkey\Dirigent\EasyAdmin;
6+
7+
use CodedMonkey\Dirigent\Doctrine\Entity\User;
8+
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
9+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
10+
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
11+
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
12+
13+
class MfaAuthenticationConfigurator implements FieldConfiguratorInterface
14+
{
15+
public function supports(FieldDto $field, EntityDto $entityDto): bool
16+
{
17+
return User::class === $entityDto->getFqcn() && 'totpAuthenticationEnabled' === $field->getProperty();
18+
}
19+
20+
public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void
21+
{
22+
/** @var User $user */
23+
$user = $entityDto->getInstance();
24+
25+
if (!$user->isTotpAuthenticationEnabled()) {
26+
$field->setFormTypeOption('disabled', true);
27+
}
28+
}
29+
}

translations/messages.en.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,8 @@ account:
122122
qr-code-alt: QR-code containing the MFA secret.
123123
state-disabled: Multi-factor authentication is currently disabled.
124124
state-enabled: Multi-factor authentication is currently enabled.
125+
126+
form:
127+
user:
128+
help:
129+
totp-authentication-enabled: Multi-factor authentication can be disabled for users that lost their access to their MFA code, but has to be enabled by the user.

0 commit comments

Comments
 (0)