mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 17:56:16 +08:00
Refactor ApplicationController#redirect_to_login_if_required to use session for SSO
This commit is contained in:
parent
7056db26e6
commit
8055d065f2
|
@ -413,17 +413,22 @@ class ApplicationController < ActionController::Base
|
||||||
raise Discourse::InvalidAccess.new unless current_user && current_user.staff?
|
raise Discourse::InvalidAccess.new unless current_user && current_user.staff?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def destination_url
|
||||||
|
request.original_url unless request.original_url =~ /uploads/
|
||||||
|
end
|
||||||
|
|
||||||
def redirect_to_login_if_required
|
def redirect_to_login_if_required
|
||||||
return if current_user || (request.format.json? && api_key_valid?)
|
return if current_user || (request.format.json? && api_key_valid?)
|
||||||
|
|
||||||
# save original URL in a cookie
|
|
||||||
cookies[:destination_url] = request.original_url unless request.original_url =~ /uploads/
|
|
||||||
|
|
||||||
# redirect user to the SSO page if we need to log in AND SSO is enabled
|
# redirect user to the SSO page if we need to log in AND SSO is enabled
|
||||||
if SiteSetting.login_required?
|
if SiteSetting.login_required?
|
||||||
if SiteSetting.enable_sso?
|
if SiteSetting.enable_sso?
|
||||||
|
# save original URL in a session so we can redirect after login
|
||||||
|
session[:destination_url] = destination_url
|
||||||
redirect_to path('/session/sso')
|
redirect_to path('/session/sso')
|
||||||
else
|
else
|
||||||
|
# save original URL in a cookie (javascript redirects after login in this case)
|
||||||
|
cookies[:destination_url] = destination_url
|
||||||
redirect_to :login
|
redirect_to :login
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,15 +11,15 @@ class SessionController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def sso
|
def sso
|
||||||
if params[:return_path]
|
return_path = if params[:return_path]
|
||||||
return_path = params[:return_path]
|
params[:return_path]
|
||||||
elsif cookies[:destination_url]
|
elsif session[:destination_url]
|
||||||
return_path = URI::parse(cookies[:destination_url]).path
|
URI::parse(session[:destination_url]).path
|
||||||
else
|
else
|
||||||
return_path = path('/')
|
path('/')
|
||||||
end
|
end
|
||||||
|
|
||||||
if SiteSetting.enable_sso
|
if SiteSetting.enable_sso?
|
||||||
redirect_to DiscourseSingleSignOn.generate_url(return_path)
|
redirect_to DiscourseSingleSignOn.generate_url(return_path)
|
||||||
else
|
else
|
||||||
render nothing: true, status: 404
|
render nothing: true, status: 404
|
||||||
|
|
Loading…
Reference in New Issue
Block a user