diff --git a/migrations/Version20250413133131.php b/migrations/Version20250413133131.php new file mode 100644 index 000000000..db1fff2cf --- /dev/null +++ b/migrations/Version20250413133131.php @@ -0,0 +1,47 @@ +getTable($this->getTable('user')); + + $this->skipIf($userTable->hasColumn('google_authenticator'), 'It seems that you already played this migration.'); + + $userTable->addColumn('google_authenticator', 'boolean', [ + 'default' => false, + 'notnull' => true, + ]); + } + + /** + * Query to update data in user table, as it's not possible to perform this in the `up` method. + */ + public function postUp(Schema $schema): void + { + $this->skipIf(!$schema->getTable($this->getTable('user'))->hasColumn('google_authenticator'), 'Unable to update google_authenticator column'); + $this->connection->executeQuery( + 'UPDATE ' . $this->getTable('user') . ' SET google_authenticator = :googleAuthenticator WHERE googleAuthenticatorSecret IS NOT NULL AND googleAuthenticatorSecret <> :emptyString', + [ + 'googleAuthenticator' => true, + 'emptyString' => '', + ] + ); + } + + public function down(Schema $schema): void + { + $userTable = $schema->getTable($this->getTable('user')); + $userTable->dropColumn('google_authenticator'); + } +} diff --git a/src/Controller/ConfigController.php b/src/Controller/ConfigController.php index 39b4e48ad..fde92ddaa 100644 --- a/src/Controller/ConfigController.php +++ b/src/Controller/ConfigController.php @@ -313,6 +313,7 @@ class ConfigController extends AbstractController $user = $this->getUser(); $user->setGoogleAuthenticatorSecret(''); + $user->setGoogleAuthenticator(false); $user->setBackupCodes(null); $this->userManager->updateUser($user); @@ -354,11 +355,6 @@ class ConfigController extends AbstractController $this->userManager->updateUser($user); $this->entityManager->flush(); - $this->addFlash( - 'notice', - 'flashes.config.notice.otp_enabled' - ); - return $this->render('Config/otp_app.html.twig', [ 'backupCodes' => $backupCodes, 'qr_code' => $googleAuthenticator->getQRContent($user), @@ -408,6 +404,9 @@ class ConfigController extends AbstractController 'notice', 'flashes.config.notice.otp_enabled' ); + $user->setGoogleAuthenticator(true); + $this->userManager->updateUser($user); + $this->entityManager->flush(); return $this->redirect($this->generateUrl('config') . '#set3'); } @@ -421,8 +420,9 @@ class ConfigController extends AbstractController $user->setBackupCodes(null); $this->userManager->updateUser($user); + $this->entityManager->flush(); - return $this->redirect($this->generateUrl('config') . '#set3'); + return $this->redirect($this->generateUrl('config_otp_app'), 307); } /** diff --git a/src/Entity/User.php b/src/Entity/User.php index fc48d8737..f3668e787 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -147,6 +147,11 @@ class User extends BaseUser implements EmailTwoFactorInterface, GoogleTwoFactorI #[ORM\Column(name: 'googleAuthenticatorSecret', type: 'string', nullable: true)] private $googleAuthenticatorSecret; + // default value is explicitly set to false here to ensure that Doctrine + // does not complain about schema mapping mismatch + #[ORM\Column(name: 'google_authenticator', type: 'boolean', options: ['default' => false])] + private $googleAuthenticator = false; + /** * @var array */ @@ -264,6 +269,11 @@ class User extends BaseUser implements EmailTwoFactorInterface, GoogleTwoFactorI $this->emailTwoFactor = $emailTwoFactor; } + public function setGoogleAuthenticator(bool $googleAuthenticator): void + { + $this->googleAuthenticator = $googleAuthenticator; + } + /** * Used in the user config form to be "like" the email option. */ @@ -294,7 +304,7 @@ class User extends BaseUser implements EmailTwoFactorInterface, GoogleTwoFactorI public function isGoogleAuthenticatorEnabled(): bool { - return $this->googleAuthenticatorSecret ? true : false; + return $this->googleAuthenticator; } public function getGoogleAuthenticatorUsername(): string diff --git a/templates/Config/otp_app.html.twig b/templates/Config/otp_app.html.twig index 475d783b1..96df3955a 100644 --- a/templates/Config/otp_app.html.twig +++ b/templates/Config/otp_app.html.twig @@ -18,14 +18,14 @@
-{{ secret }}
{{ 'config.otp.app.two_factor_code_description_3'|trans }}
-{{ backupCodes|join("\n") }}
{{ backupCodes|join("\n") }}
{{ 'config.otp.app.two_factor_code_description_4'|trans }}
@@ -36,7 +36,7 @@