Skip to content

Redirect after login and last url #3415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions phpmyfaq/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
// Get user action
//
$action = Filter::filterVar($request->query->get('action'), FILTER_SANITIZE_SPECIAL_CHARS);
$redirectAction = Filter::filterVar($request->get('redirect-action', "POST"), FILTER_SANITIZE_SPECIAL_CHARS);
if ( $action != 'logout' && $action != 'login' && $redirectAction != 'login') {
$container->get('session')->set("lastRequestUri", $request->getRequestUri());
}


//
// Authenticate current user
Expand Down Expand Up @@ -220,10 +225,30 @@
}
}

if ($user->isLoggedIn() && ( $redirectAction == 'login' || $action == 'login' ) && $user->getUserId() > 0) {
$goTo = $container->get('session')->get("lastRequestUri") ? $container->get('session')->get("lastRequestUri") : $faqConfig->getDefaultUrl();
$redirect = new RedirectResponse($goTo);
$redirect->send();
}

$faqSystem = new System(); // moved higher to support security check

//
// Check if the FAQ should be secured (moved higher so we don't do work we don't need to do)
//
if ($faqConfig->get('security.enableLoginOnly')) {
if (!$user->isLoggedIn() && ($action !== 'login' && $action !== 'password')) {
$redirect = new RedirectResponse($faqSystem->getSystemUri($faqConfig) . 'login');
$redirect->send();
}
}


//
// Logout
//
if ($csrfChecked && 'logout' === $action && $user->isLoggedIn()) {
$container->get('session')->set("lastRequestUri", "");
$user->deleteFromSession(true);
$action = 'main';
$ssoLogout = $faqConfig->get('security.ssoLogoutRedirect');
Expand Down Expand Up @@ -472,16 +497,6 @@
}
}

//
// Check if the FAQ should be secured
//
if ($faqConfig->get('security.enableLoginOnly')) {
if (!$user->isLoggedIn() && ($action !== 'login' && $action !== 'password')) {
$redirect = new RedirectResponse($faqSystem->getSystemUri($faqConfig) . 'login');
$redirect->send();
}
}

$categoryRelation = new Relation($faqConfig, $category);

$categoryHelper = new HelperCategory();
Expand Down
5 changes: 4 additions & 1 deletion phpmyfaq/src/phpMyFAQ/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ private function initializeTranslation(string $currentLanguage): void

private function handleRequest(RouteCollection $routeCollection, Request $request, RequestContext $context): void
{
if ( ! preg_match('/login|authenticate|logout|keep-alive|token|check/', $request->getRequestUri()) ) {
$this->container->get('session')->set("lastRequestUri", $request->getRequestUri());
}
$urlMatcher = new UrlMatcher($routeCollection, $context);
$this->setUrlMatcher($urlMatcher);
$controllerResolver = new ControllerResolver();
Expand Down Expand Up @@ -138,7 +141,7 @@ private function handleRequest(RouteCollection $routeCollection, Request $reques
['Content-Type' => 'application/json']
);
} else {
$response = new RedirectResponse('/login');
$response = new RedirectResponse('../login');
}
} catch (BadRequestException $exception) {
$response = new Response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function authenticate(Request $request): Response
'Login-error\nLogin: ' . $username . '\nErrors: ' . implode(', ', $this->currentUser->errors)
);
//$error = $e->getMessage();
return new RedirectResponse('./login');
return new RedirectResponse('../login');
}
}

Expand All @@ -85,6 +85,7 @@ public function authenticate(Request $request): Response
#[Route('/login', name: 'admin.auth.logout', methods: ['GET'])]
public function login(Request $request): Response
{
return new RedirectResponse('../login');
// Redirect to authenticate if SSO is enabled and the user is already authenticated
if ($this->configuration->get('security.ssoSupport') && $request->server->get('REMOTE_USER') !== null) {
return new RedirectResponse('./authenticate');
Expand Down
Loading