DEV: backport login redirect param to stable (#32865)

This backports to stable my changes to add a redirect queryParam to the
/login route.

Pulled in a couple other commits that affect the login process and the
same spec that I modified.

Had to add a commit cleaning up the modal login helper for login_spec.rb
to pass here since the modal login was already removed in main.

---------

Co-authored-by: Penar Musaraj <pmusaraj@gmail.com>
This commit is contained in:
Chris Alberti
2025-05-22 16:20:21 -05:00
committed by GitHub
parent 2f86ded82b
commit 9f1e7415fd
11 changed files with 270 additions and 32 deletions

View File

@ -27,9 +27,36 @@ class StaticController < ApplicationController
}
CUSTOM_PAGES = {} # Add via `#add_topic_static_page` in plugin API
def extract_redirect_param
redirect_path = params[:redirect]
if redirect_path.present?
begin
forum_host = URI(Discourse.base_url).host
uri = URI(redirect_path)
if uri.path.present? && !uri.path.starts_with?(login_path) &&
(uri.host.blank? || uri.host == forum_host) && uri.path =~ %r{\A\/{1}[^\.\s]*\z}
return "#{uri.path}#{uri.query ? "?#{uri.query}" : ""}"
end
rescue URI::Error, ArgumentError
# If the URI is invalid, return "/" below
end
end
"/"
end
def show
if current_user && (params[:id] == "login" || params[:id] == "signup")
return redirect_to(path "/")
if params[:id] == "login"
destination = extract_redirect_param
if current_user
return redirect_to(path(destination), allow_other_host: false)
elsif destination != "/"
cookies[:destination_url] = path(destination)
end
elsif params[:id] == "signup" && current_user
return redirect_to path("/")
end
if SiteSetting.login_required? && current_user.nil? && %w[faq guidelines].include?(params[:id])
@ -123,26 +150,7 @@ class StaticController < ApplicationController
params.delete(:username)
params.delete(:password)
destination = path("/")
redirect_location = params[:redirect]
if redirect_location.present? && !redirect_location.is_a?(String)
raise Discourse::InvalidParameters.new(:redirect)
elsif redirect_location.present? &&
begin
forum_uri = URI(Discourse.base_url)
uri = URI(redirect_location)
if uri.path.present? && !uri.path.starts_with?(login_path) &&
(uri.host.blank? || uri.host == forum_uri.host) &&
uri.path =~ %r{\A\/{1}[^\.\s]*\z}
destination = "#{uri.path}#{uri.query ? "?#{uri.query}" : ""}"
end
rescue URI::Error
# Do nothing if the URI is invalid
end
end
destination = extract_redirect_param
redirect_to(destination, allow_other_host: false)
end