diff --git a/config/services.yaml b/config/services.yaml index 90c5a93f..db0a8adb 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -26,6 +26,8 @@ services: _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. + bind: + bool $demo_mode: '%demo_mode%' # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name diff --git a/src/Controller/UserSettingsController.php b/src/Controller/UserSettingsController.php index 7590d109..c524481e 100644 --- a/src/Controller/UserSettingsController.php +++ b/src/Controller/UserSettingsController.php @@ -47,6 +47,13 @@ use Symfony\Component\Validator\Constraints\Length; */ class UserSettingsController extends AbstractController { + protected $demo_mode; + + public function __construct(bool $demo_mode) + { + $this->demo_mode = $demo_mode; + } + /** * @Route("/2fa_backup_codes", name="show_backup_codes") */ @@ -78,6 +85,10 @@ class UserSettingsController extends AbstractController */ public function removeU2FToken(Request $request, EntityManagerInterface $entityManager, BackupCodeManager $backupCodeManager) { + if($this->demo_mode) { + throw new \RuntimeException('You can not do 2FA things in demo mode'); + } + $user = $this->getUser(); //When user change its settings, he should be logged in fully. @@ -122,6 +133,10 @@ class UserSettingsController extends AbstractController */ public function resetTrustedDevices(Request $request, EntityManagerInterface $entityManager) { + if($this->demo_mode) { + throw new \RuntimeException('You can not do 2FA things in demo mode'); + } + $user = $this->getUser(); //When user change its settings, he should be logged in fully. @@ -170,7 +185,7 @@ class UserSettingsController extends AbstractController $form->handleRequest($request); - if ($form->isSubmitted() && $form->isValid()) { + if ($form->isSubmitted() && $form->isValid() && !$this->demo_mode) { //Check if user theme setting has changed if ($user->getTheme() !== $em->getUnitOfWork()->getOriginalEntityData($user)['theme']) { $page_need_reload = true; @@ -184,8 +199,6 @@ class UserSettingsController extends AbstractController * Password change form ****************************/ - $demo_mode = $this->getParameter('demo_mode'); - $pw_form = $this->createFormBuilder() //Username field for autocomplete ->add('username', TextType::class, [ @@ -196,11 +209,11 @@ class UserSettingsController extends AbstractController ]) ->add('old_password', PasswordType::class, [ 'label' => 'user.settings.pw_old.label', - 'disabled' => $demo_mode, + 'disabled' => $this->demo_mode, 'attr' => ['autocomplete' => 'current-password'], 'constraints' => [new UserPassword()], ]) //This constraint checks, if the current user pw was inputted. ->add('new_password', RepeatedType::class, [ - 'disabled' => $demo_mode, + 'disabled' => $this->demo_mode, 'type' => PasswordType::class, 'first_options' => ['label' => 'user.settings.pw_new.label'], 'second_options' => ['label' => 'user.settings.pw_confirm.label'], @@ -219,7 +232,7 @@ class UserSettingsController extends AbstractController $pw_form->handleRequest($request); //Check if password if everything was correct, then save it to User and DB - if ($pw_form->isSubmitted() && $pw_form->isValid()) { + if ($pw_form->isSubmitted() && $pw_form->isValid() && !$this->demo_mode) { $password = $passwordEncoder->encodePassword($user, $pw_form['new_password']->getData()); $user->setPassword($password); @@ -240,7 +253,7 @@ class UserSettingsController extends AbstractController } $google_form->handleRequest($request); - if($google_form->isSubmitted() && $google_form->isValid()) { + if($google_form->isSubmitted() && $google_form->isValid() && !$this->demo_mode) { if (!$google_enabled) { //Save 2FA settings (save secrets) $user->setGoogleAuthenticatorSecret($google_form->get('googleAuthenticatorSecret')->getData()); @@ -265,7 +278,7 @@ class UserSettingsController extends AbstractController ])->getForm(); $backup_form->handleRequest($request); - if ($backup_form->isSubmitted() && $backup_form->isValid()) { + if ($backup_form->isSubmitted() && $backup_form->isValid() && !$this->demo_mode) { $backupCodeManager->regenerateBackupCodes($user); $em->flush(); $this->addFlash('success', 'user.settings.2fa.backup_codes.regenerated'); diff --git a/src/EventSubscriber/U2FRegistrationSubscriber.php b/src/EventSubscriber/U2FRegistrationSubscriber.php index f5750225..04969fd6 100644 --- a/src/EventSubscriber/U2FRegistrationSubscriber.php +++ b/src/EventSubscriber/U2FRegistrationSubscriber.php @@ -27,6 +27,7 @@ use Doctrine\ORM\EntityManagerInterface; use R\U2FTwoFactorBundle\Event\RegisterEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class U2FRegistrationSubscriber implements EventSubscriberInterface @@ -36,14 +37,17 @@ class U2FRegistrationSubscriber implements EventSubscriberInterface protected $em; - public function __construct(UrlGeneratorInterface $router, EntityManagerInterface $entityManager) + protected $demo_mode; + protected $flashBag; + + public function __construct(UrlGeneratorInterface $router, EntityManagerInterface $entityManager, FlashBagInterface $flashBag, bool $demo_mode) { $this->router = $router; $this->em = $entityManager; + $this->demo_mode = $demo_mode; + $this->flashBag = $flashBag; } - // .. - /** @return string[] **/ public static function getSubscribedEvents(): array { @@ -54,16 +58,20 @@ class U2FRegistrationSubscriber implements EventSubscriberInterface public function onRegister(RegisterEvent $event): void { - $user = $event->getUser(); - $registration = $event->getRegistration(); - $newKey = new U2FKey(); - $newKey->fromRegistrationData($registration); - $newKey->setUser($user); - $newKey->setName($event->getKeyName()); + //Skip adding of U2F key on demo mode + if (!$this->demo_mode) { + $user = $event->getUser(); + $registration = $event->getRegistration(); + $newKey = new U2FKey(); + $newKey->fromRegistrationData($registration); + $newKey->setUser($user); + $newKey->setName($event->getKeyName()); - // persist the new key - $this->em->persist($newKey); - $this->em->flush(); + // persist the new key + $this->em->persist($newKey); + $this->em->flush(); + $this->flashBag->add('success', 'tfa_u2f.key_added_successful'); + } // generate new response, here we redirect the user to the fos user // profile diff --git a/tests/EventSubscriber/PasswordChangeNeededSubscriberTest.php b/tests/EventSubscriber/PasswordChangeNeededSubscriberTest.php index caccca4d..49a63946 100644 --- a/tests/EventSubscriber/PasswordChangeNeededSubscriberTest.php +++ b/tests/EventSubscriber/PasswordChangeNeededSubscriberTest.php @@ -1,4 +1,23 @@