src/EventSubscriber/UserSystem/PasswordChangeNeededSubscriber.php line 94

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  4.  *
  5.  * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU Affero General Public License as published
  9.  * by the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU Affero General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Affero General Public License
  18.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  19.  */
  20. declare(strict_types=1);
  21. /**
  22.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  23.  *
  24.  * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
  25.  *
  26.  * This program is free software; you can redistribute it and/or
  27.  * modify it under the terms of the GNU General Public License
  28.  * as published by the Free Software Foundation; either version 2
  29.  * of the License, or (at your option) any later version.
  30.  *
  31.  * This program is distributed in the hope that it will be useful,
  32.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.  * GNU General Public License for more details.
  35.  *
  36.  * You should have received a copy of the GNU General Public License
  37.  * along with this program; if not, write to the Free Software
  38.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  39.  */
  40. namespace App\EventSubscriber\UserSystem;
  41. use App\Entity\UserSystem\User;
  42. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  43. use Symfony\Component\HttpFoundation\Session\Session;
  44. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  45. use Symfony\Component\HttpKernel\Event\RequestEvent;
  46. use Symfony\Component\HttpKernel\KernelEvents;
  47. use Symfony\Component\Security\Core\Security;
  48. use Symfony\Component\Security\Http\HttpUtils;
  49. /**
  50.  * This event subscriber redirects a user to its settings page, when it needs to change its password or is enforced
  51.  * to setup a 2FA method (enforcement can be set per group).
  52.  * In this cases the user is unable to access sites other than the whitelisted (see ALLOWED_ROUTES).
  53.  */
  54. final class PasswordChangeNeededSubscriber implements EventSubscriberInterface
  55. {
  56.     /**
  57.      * @var string[] The routes the user is allowed to access without being redirected.
  58.      *               This should be only routes related to login/logout and user settings
  59.      */
  60.     public const ALLOWED_ROUTES = [
  61.         '2fa_login',
  62.         '2fa_login_check',
  63.         'user_settings',
  64.         'club_base_register_u2f',
  65.         'logout',
  66.     ];
  67.     /**
  68.      * @var string The route the user will redirected to, if he needs to change this password
  69.      */
  70.     public const REDIRECT_TARGET 'user_settings';
  71.     private $security;
  72.     private $flashBag;
  73.     private $httpUtils;
  74.     public function __construct(Security $securitySessionInterface $sessionHttpUtils $httpUtils)
  75.     {
  76.         /** @var Session $session */
  77.         $this->security $security;
  78.         $this->flashBag $session->getFlashBag();
  79.         $this->httpUtils $httpUtils;
  80.     }
  81.     /**
  82.      * This function is called when the kernel encounters a request.
  83.      * It checks if the user must change its password or add an 2FA mehtod and redirect it to the user settings page,
  84.      * if needed.
  85.      */
  86.     public function redirectToSettingsIfNeeded(RequestEvent $event): void
  87.     {
  88.         $user $this->security->getUser();
  89.         $request $event->getRequest();
  90.         if (!$event->isMainRequest()) {
  91.             return;
  92.         }
  93.         if (!$user instanceof User) {
  94.             return;
  95.         }
  96.         //Abort if we dont need to redirect the user.
  97.         if (!$user->isNeedPwChange() && !static::TFARedirectNeeded($user)) {
  98.             return;
  99.         }
  100.         //Check for a whitelisted URL
  101.         foreach (static::ALLOWED_ROUTES as $route) {
  102.             //Dont do anything if we encounter an allowed route
  103.             if ($this->httpUtils->checkRequestPath($request$route)) {
  104.                 return;
  105.             }
  106.         }
  107.         /* Dont redirect tree endpoints, as this would cause trouble and creates multiple flash
  108.         warnigs for one page reload */
  109.         if (false !== strpos($request->getUri(), '/tree/')) {
  110.             return;
  111.         }
  112.         //Show appropriate message to user about the reason he was redirected
  113.         if ($user->isNeedPwChange()) {
  114.             $this->flashBag->add('warning''user.pw_change_needed.flash');
  115.         }
  116.         if (static::TFARedirectNeeded($user)) {
  117.             $this->flashBag->add('warning''user.2fa_needed.flash');
  118.         }
  119.         $event->setResponse($this->httpUtils->createRedirectResponse($request, static::REDIRECT_TARGET));
  120.     }
  121.     /**
  122.      * Check if a redirect because of a missing 2FA method is needed.
  123.      * That is the case if the group of the user enforces 2FA, but the user has neither Google Authenticator nor an
  124.      * U2F key setup.
  125.      *
  126.      * @param User $user the user for which should be checked if it needs to be redirected
  127.      *
  128.      * @return bool true if the user needs to be redirected
  129.      */
  130.     public static function TFARedirectNeeded(User $user): bool
  131.     {
  132.         $tfa_enabled $user->isU2FAuthEnabled() || $user->isGoogleAuthenticatorEnabled();
  133.         return null !== $user->getGroup() && $user->getGroup()->isEnforce2FA() && !$tfa_enabled;
  134.     }
  135.     public static function getSubscribedEvents(): array
  136.     {
  137.         return [
  138.             KernelEvents::REQUEST => 'redirectToSettingsIfNeeded',
  139.         ];
  140.     }
  141. }