Skip to content

add login check and logout url #1464

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 1 commit 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
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mailer_options.enable_starttls_auto=true
sso_enable=false
sso_enable_provider=false
sso_url=
sso_logout_url=
sso_secret=

# [Upload Config]
Expand All @@ -56,3 +57,6 @@ upload_bucket=
upload_aliyun_internal=
upload_aliyun_area=
upload_url=

# [Access Control]
logged_in_required=true
14 changes: 14 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ class ApplicationController < ActionController::Base
etag { Setting.footer_html }
etag { Rails.env.development? ? Time.now : Date.current }

# Add login check
before_action :check_login_required

def check_login_required
return unless ENV['logged_in_required'] == 'true'
return if current_user
return if devise_controller?
return if controller_name == 'sso' && action_name == 'show'
return if controller_name == 'sso' && action_name == 'login'

store_location
redirect_to new_user_session_path
end

rescue_from ActiveRecord::RecordNotFound do |exception|
respond_to do |format|
format.json { head :not_found }
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/users/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,28 @@ def create
format.json { render status: "201", json: resource.as_json(only: %i[login email]) }
end
end

def destroy
Rails.logger.info "Destroying session for user: #{current_user&.id}"

# Clean cookies
cookies.delete(:user_id)
cookies.delete(:remember_user_token)

# Perform logout operation
sign_out(current_user)

# If SSO logout URL is set, redirect to that URL
if ENV['sso_logout_url'].present?
Rails.logger.info "Redirecting to SSO logout URL: #{ENV['sso_logout_url']}"
redirect_to ENV['sso_logout_url'], allow_other_host: true
else
Rails.logger.info "Redirecting to root path"
redirect_to root_path
end
rescue => e
Rails.logger.error "Error during logout: #{e.message}"
Rails.logger.error e.backtrace.join("\n")
raise e
end
end
2 changes: 1 addition & 1 deletion app/views/devise/menu/_login_items.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if user_signed_in? %>
<%= link_to(t("common.logout"), destroy_user_session_path) %>
<%= link_to(t("common.logout"), destroy_user_session_path, method: :delete) %>
<% else %>
<%= link_to(t("common.login"), new_user_session_path) %>
<% end %>