|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Login Controller |
| 4 | + * Authentication Controller to handle login, logout, and password reset |
5 | 5 | * |
6 | 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
7 | 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
|
18 | 18 | namespace phpMyFAQ\Controller\Frontend; |
19 | 19 |
|
20 | 20 | use phpMyFAQ\Core\Exception; |
| 21 | +use phpMyFAQ\Filter; |
| 22 | +use phpMyFAQ\Session\Token; |
21 | 23 | use phpMyFAQ\Translation; |
| 24 | +use phpMyFAQ\User\CurrentUser; |
| 25 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
22 | 26 | use Symfony\Component\HttpFoundation\Request; |
23 | 27 | use Symfony\Component\HttpFoundation\Response; |
24 | 28 | use Symfony\Component\Routing\Attribute\Route; |
25 | 29 | use Twig\Error\LoaderError; |
26 | 30 |
|
27 | | -final class LoginController extends AbstractFrontController |
| 31 | +final class AuthenticationController extends AbstractFrontController |
28 | 32 | { |
29 | 33 | /** |
30 | 34 | * @throws Exception |
31 | 35 | * @throws LoaderError |
32 | 36 | * @throws \Exception |
33 | 37 | */ #[Route(path: '/login', name: 'public.login')] |
34 | | - public function index(Request $request): Response |
| 38 | + public function login(Request $request): Response |
35 | 39 | { |
36 | 40 | $faqSession = $this->container->get('phpmyfaq.user.session'); |
37 | 41 | $faqSession->setCurrentUser($this->currentUser); |
@@ -83,4 +87,41 @@ public function forgotPassword(Request $request): Response |
83 | 87 | 'password' => Translation::get(key: 'ad_auth_passwd'), |
84 | 88 | ]); |
85 | 89 | } |
| 90 | + |
| 91 | + /** |
| 92 | + * @throws \Exception |
| 93 | + */ |
| 94 | + #[Route(path: '/logout', name: 'public.logout')] |
| 95 | + public function logout(Request $request): Response |
| 96 | + { |
| 97 | + $user = CurrentUser::getCurrentUser($this->configuration); |
| 98 | + $csrfToken = Filter::filterVar($request->query->get('csrf'), FILTER_SANITIZE_SPECIAL_CHARS); |
| 99 | + |
| 100 | + if (!Token::getInstance($this->container->get('session'))->verifyToken('logout', $csrfToken)) { |
| 101 | + return new RedirectResponse($this->configuration->getDefaultUrl()); |
| 102 | + } |
| 103 | + |
| 104 | + if (!$user->isLoggedIn()) { |
| 105 | + return new RedirectResponse($this->configuration->getDefaultUrl()); |
| 106 | + } |
| 107 | + |
| 108 | + $user->deleteFromSession(true); |
| 109 | + |
| 110 | + // Add a success message |
| 111 | + $session = $this->container->get('session'); |
| 112 | + $session->getFlashBag()->add('success', Translation::get('ad_logout')); |
| 113 | + |
| 114 | + // SSO Logout |
| 115 | + $ssoLogout = $this->configuration->get('security.ssoLogoutRedirect'); |
| 116 | + if ($this->configuration->get('security.ssoSupport') && !empty($ssoLogout)) { |
| 117 | + return new RedirectResponse($ssoLogout); |
| 118 | + } |
| 119 | + |
| 120 | + // Microsoft Azure Logout |
| 121 | + if ($this->configuration->isSignInWithMicrosoftActive() && $user->getUserAuthSource() === 'azure') { |
| 122 | + return new RedirectResponse($this->configuration->getDefaultUrl() . 'services/azure/logout.php'); |
| 123 | + } |
| 124 | + |
| 125 | + return new RedirectResponse($this->configuration->getDefaultUrl()); |
| 126 | + } |
86 | 127 | } |
0 commit comments