|
|
|
@ -22,13 +22,15 @@ class SsoLoginController < ApplicationController
|
|
|
|
email = decoded_token['email']&.downcase
|
|
|
|
email = decoded_token['email']&.downcase
|
|
|
|
first_name = decoded_token['first_name']
|
|
|
|
first_name = decoded_token['first_name']
|
|
|
|
last_name = decoded_token['last_name']
|
|
|
|
last_name = decoded_token['last_name']
|
|
|
|
|
|
|
|
company_id = decoded_token['company_id'] || decoded_token['account_id'] || decoded_token['organization_id']
|
|
|
|
|
|
|
|
company_name = decoded_token['company_name'] || decoded_token['account_name'] || decoded_token['organization_name']
|
|
|
|
|
|
|
|
|
|
|
|
unless email.present?
|
|
|
|
unless email.present?
|
|
|
|
return redirect_to root_path, alert: 'Invalid token: email missing'
|
|
|
|
return redirect_to root_path, alert: 'Invalid token: email missing'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Find or create user
|
|
|
|
# Find or create user with company/account
|
|
|
|
user = find_or_create_user(email, first_name, last_name)
|
|
|
|
user = find_or_create_user(email, first_name, last_name, company_id, company_name)
|
|
|
|
|
|
|
|
|
|
|
|
if user
|
|
|
|
if user
|
|
|
|
# Sign in the user
|
|
|
|
# Sign in the user
|
|
|
|
@ -57,11 +59,28 @@ class SsoLoginController < ApplicationController
|
|
|
|
decoded[0] # Return the payload
|
|
|
|
decoded[0] # Return the payload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def find_or_create_user(email, first_name, last_name)
|
|
|
|
def find_or_create_user(email, first_name, last_name, company_id = nil, company_name = nil)
|
|
|
|
# Try to find existing user by email
|
|
|
|
# Find or create account based on company_id
|
|
|
|
|
|
|
|
account = find_or_create_account_by_company(company_id, company_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Try to find existing user by email (email is unique globally)
|
|
|
|
user = User.find_by(email: email)
|
|
|
|
user = User.find_by(email: email)
|
|
|
|
|
|
|
|
|
|
|
|
if user
|
|
|
|
if user
|
|
|
|
|
|
|
|
# User exists - check if they're in the correct account
|
|
|
|
|
|
|
|
if user.account_id != account.id
|
|
|
|
|
|
|
|
# User exists but in a different account
|
|
|
|
|
|
|
|
# Move user to the correct account if company_id is provided
|
|
|
|
|
|
|
|
if company_id.present?
|
|
|
|
|
|
|
|
Rails.logger.info("Moving user #{email} from account #{user.account_id} to account #{account.id} (company_id: #{company_id})")
|
|
|
|
|
|
|
|
user.update(account_id: account.id)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
# If no company_id provided, keep user in existing account but log warning
|
|
|
|
|
|
|
|
Rails.logger.warn("User #{email} exists in account #{user.account_id} but company_id not provided in token")
|
|
|
|
|
|
|
|
account = user.account # Use existing account
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Update user info if provided and different
|
|
|
|
# Update user info if provided and different
|
|
|
|
update_attrs = {}
|
|
|
|
update_attrs = {}
|
|
|
|
update_attrs[:first_name] = first_name if first_name.present? && user.first_name != first_name
|
|
|
|
update_attrs[:first_name] = first_name if first_name.present? && user.first_name != first_name
|
|
|
|
@ -72,10 +91,7 @@ class SsoLoginController < ApplicationController
|
|
|
|
return user
|
|
|
|
return user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# User doesn't exist, create a new one
|
|
|
|
# User doesn't exist, create a new one in the specified account
|
|
|
|
# Find or create an account
|
|
|
|
|
|
|
|
account = find_or_create_account
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Generate a random password for the new user
|
|
|
|
# Generate a random password for the new user
|
|
|
|
password = SecureRandom.hex(16)
|
|
|
|
password = SecureRandom.hex(16)
|
|
|
|
|
|
|
|
|
|
|
|
@ -96,18 +112,68 @@ class SsoLoginController < ApplicationController
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def find_or_create_account
|
|
|
|
def find_or_create_account_by_company(company_id = nil, company_name = nil)
|
|
|
|
# Try to find the first active account
|
|
|
|
# If company_id is provided, try to find account by ID or UUID
|
|
|
|
account = Account.active.first
|
|
|
|
if company_id.present?
|
|
|
|
|
|
|
|
# Try to find by ID first
|
|
|
|
|
|
|
|
account = Account.active.find_by(id: company_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# If not found by ID, try to find by UUID
|
|
|
|
|
|
|
|
account ||= Account.active.find_by(uuid: company_id.to_s) if account.nil?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return account if account
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# If company_name is provided, try to find by name
|
|
|
|
|
|
|
|
if company_name.present?
|
|
|
|
|
|
|
|
account = Account.active.find_by(name: company_name)
|
|
|
|
|
|
|
|
return account if account
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# If no company_id or company_name provided, or account not found
|
|
|
|
|
|
|
|
# Check if this is the first user (no accounts exist)
|
|
|
|
|
|
|
|
if Account.active.count.zero?
|
|
|
|
|
|
|
|
# Create the first default account
|
|
|
|
|
|
|
|
account = create_default_account(company_name || 'Default Account')
|
|
|
|
|
|
|
|
return account
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# If company_id/name not provided and accounts exist, we need to create a new account
|
|
|
|
|
|
|
|
# Use company_name if provided, otherwise generate a unique name
|
|
|
|
|
|
|
|
account_name = if company_name.present?
|
|
|
|
|
|
|
|
company_name
|
|
|
|
|
|
|
|
elsif company_id.present?
|
|
|
|
|
|
|
|
"Company #{company_id}"
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
"Company #{SecureRandom.hex(4)}"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create new account for this company
|
|
|
|
|
|
|
|
account = Account.create!(
|
|
|
|
|
|
|
|
name: account_name,
|
|
|
|
|
|
|
|
timezone: 'UTC',
|
|
|
|
|
|
|
|
locale: 'en-US'
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize account with required configs
|
|
|
|
|
|
|
|
initialize_account_configs(account)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
account
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# If no account exists, create a default one
|
|
|
|
def create_default_account(name = 'Default Account')
|
|
|
|
unless account
|
|
|
|
|
|
|
|
account = Account.create!(
|
|
|
|
account = Account.create!(
|
|
|
|
name: 'Default Account',
|
|
|
|
name: name,
|
|
|
|
timezone: 'UTC',
|
|
|
|
timezone: 'UTC',
|
|
|
|
locale: 'en-US'
|
|
|
|
locale: 'en-US'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize_account_configs(account)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
account
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize_account_configs(account)
|
|
|
|
# Create encrypted configs if needed
|
|
|
|
# Create encrypted configs if needed
|
|
|
|
if EncryptedConfig.table_exists?
|
|
|
|
if EncryptedConfig.table_exists?
|
|
|
|
app_url = Docuseal.default_url_options[:host] || request.host
|
|
|
|
app_url = Docuseal.default_url_options[:host] || request.host
|
|
|
|
@ -135,7 +201,5 @@ class SsoLoginController < ApplicationController
|
|
|
|
account.account_configs.create!(key: :fulltext_search, value: true)
|
|
|
|
account.account_configs.create!(key: :fulltext_search, value: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
account
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|