mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 11:47:56 +01:00
Add the possibility to enable/disable default OAuths (#303)
This commit is contained in:
committed by
GitHub
parent
719f1ad4e9
commit
32d19cbe7c
@@ -5,12 +5,16 @@ class OAuthsController < ApplicationController
|
||||
|
||||
before_action :authenticate_admin, only: [:index, :create, :update, :destroy]
|
||||
|
||||
TOKEN_STATE_SEPARATOR = '-'
|
||||
TOKEN_STATE_SEPARATOR = ','
|
||||
|
||||
# [subdomain.]base_url/o_auths/:id/start?reason=login|test|tenantsignup
|
||||
# Generates authorize url with required parameters and redirects to provider
|
||||
def start
|
||||
@o_auth = OAuth.unscoped.include_defaults.find(params[:id])
|
||||
if params[:reason] == 'tenantsignup'
|
||||
@o_auth = OAuth.include_only_defaults.find(params[:id])
|
||||
else
|
||||
@o_auth = OAuth.include_defaults.find(params[:id])
|
||||
end
|
||||
|
||||
return if params[:reason] != 'test' and not @o_auth.is_enabled?
|
||||
|
||||
@@ -31,15 +35,17 @@ class OAuthsController < ApplicationController
|
||||
return unless cookies[:token_state] == params[:state]
|
||||
cookies.delete(:token_state, domain: ".#{request.domain}")
|
||||
|
||||
@o_auth = OAuth.unscoped.include_defaults.find(params[:id])
|
||||
# if it is a default oauth, tenant is not yet set
|
||||
Current.tenant ||= Tenant.find_by(subdomain: tenant_domain)
|
||||
|
||||
if reason == 'tenantsignup'
|
||||
@o_auth = OAuth.include_only_defaults.find(params[:id])
|
||||
else
|
||||
@o_auth = OAuth.include_defaults.find(params[:id])
|
||||
end
|
||||
|
||||
return if reason != 'test' and not @o_auth.is_enabled?
|
||||
|
||||
# If it is a default OAuth we need to set the tenant
|
||||
if @o_auth.is_default?
|
||||
Current.tenant = Tenant.find_by(subdomain: tenant_domain)
|
||||
end
|
||||
|
||||
user_profile = OAuthExchangeAuthCodeForProfileWorkflow.new(
|
||||
authorization_code: params[:code],
|
||||
o_auth: @o_auth
|
||||
@@ -80,12 +86,20 @@ class OAuthsController < ApplicationController
|
||||
|
||||
elsif reason == 'tenantsignup'
|
||||
|
||||
@o_auths = []
|
||||
@o_auths = @o_auths = OAuth.unscoped.where(tenant_id: nil, is_enabled: true)
|
||||
|
||||
@user_email = query_path_from_object(user_profile, @o_auth.json_user_email_path)
|
||||
if not @o_auth.json_user_name_path.blank?
|
||||
@user_name = query_path_from_object(user_profile, @o_auth.json_user_name_path)
|
||||
end
|
||||
@o_auth_login_completed = true
|
||||
|
||||
@o_auth_login_completed = (not @user_email.blank?)
|
||||
|
||||
if not @o_auth_login_completed
|
||||
flash[:alert] = I18n.t('errors.o_auth_login_error', name: @o_auth.name)
|
||||
redirect_to signup_url
|
||||
return
|
||||
end
|
||||
|
||||
session[:o_auth_sign_up] = "#{@user_email},#{@user_name}"
|
||||
|
||||
@@ -124,7 +138,9 @@ class OAuthsController < ApplicationController
|
||||
def index
|
||||
authorize OAuth
|
||||
|
||||
@o_auths = OAuth.include_defaults.order(created_at: :asc)
|
||||
@o_auths = OAuth
|
||||
.include_all_defaults
|
||||
.order(tenant_id: :asc, created_at: :asc)
|
||||
|
||||
render json: to_json_custom(@o_auths)
|
||||
end
|
||||
@@ -175,7 +191,7 @@ class OAuthsController < ApplicationController
|
||||
|
||||
def to_json_custom(o_auth)
|
||||
o_auth.as_json(
|
||||
methods: :callback_url,
|
||||
methods: [:callback_url, :default_o_auth_is_enabled],
|
||||
except: [:client_secret]
|
||||
)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user