2025-09-28 14:53:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Users::RegistrationsController < Devise::RegistrationsController
|
2025-10-30 14:59:31 -04:00
|
|
|
include UtmTrackable
|
|
|
|
|
|
2025-10-04 18:42:21 -04:00
|
|
|
before_action :set_invitation, only: %i[new create]
|
|
|
|
|
before_action :check_registration_allowed, only: %i[new create]
|
2025-10-30 14:59:31 -04:00
|
|
|
before_action :store_utm_params, only: %i[new], unless: -> { DawarichSettings.self_hosted? }
|
2025-09-28 14:53:50 -04:00
|
|
|
|
|
|
|
|
def new
|
|
|
|
|
build_resource({})
|
|
|
|
|
|
2025-10-04 16:39:47 -04:00
|
|
|
resource.email = @invitation.email if @invitation
|
2025-09-28 14:53:50 -04:00
|
|
|
|
|
|
|
|
yield resource if block_given?
|
2025-10-04 16:39:47 -04:00
|
|
|
|
2025-09-28 14:53:50 -04:00
|
|
|
respond_with resource
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
super do |resource|
|
2025-10-30 14:16:38 -04:00
|
|
|
if resource.persisted?
|
|
|
|
|
assign_utm_params(resource)
|
|
|
|
|
accept_invitation_for_user(resource) if @invitation
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2026-01-08 14:29:28 -05:00
|
|
|
def destroy
|
|
|
|
|
begin
|
|
|
|
|
resource.mark_as_deleted!
|
|
|
|
|
|
|
|
|
|
# Sign out immediately
|
|
|
|
|
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
|
|
|
|
|
|
|
|
|
|
# Enqueue background job
|
|
|
|
|
Users::DestroyJob.perform_later(resource.id)
|
|
|
|
|
|
|
|
|
|
set_flash_message! :notice, :destroyed
|
|
|
|
|
yield resource if block_given?
|
|
|
|
|
respond_with_navigational(resource) { redirect_to after_sign_out_path_for(resource_name) }
|
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
|
set_flash_message! :alert, :cannot_delete
|
|
|
|
|
redirect_to edit_user_registration_path, status: :unprocessable_content
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-28 14:53:50 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
|
|
def after_sign_up_path_for(resource)
|
2025-10-05 08:24:45 -04:00
|
|
|
return family_path if @invitation&.family
|
|
|
|
|
|
|
|
|
|
super(resource)
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def after_inactive_sign_up_path_for(resource)
|
2025-10-05 08:24:45 -04:00
|
|
|
return family_path if @invitation&.family
|
|
|
|
|
|
|
|
|
|
super(resource)
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def check_registration_allowed
|
2025-10-04 19:09:46 -04:00
|
|
|
return unless self_hosted_mode?
|
2025-10-04 18:42:21 -04:00
|
|
|
return if valid_invitation_token?
|
2025-11-24 13:45:09 -05:00
|
|
|
return if email_password_registration_allowed?
|
2025-09-28 14:53:50 -04:00
|
|
|
|
2025-10-04 18:42:21 -04:00
|
|
|
redirect_to root_path,
|
|
|
|
|
alert: 'Registration is not available. Please contact your administrator for access.'
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
2025-10-04 16:39:47 -04:00
|
|
|
def set_invitation
|
2025-11-07 05:08:57 -05:00
|
|
|
return if invitation_token.blank?
|
2025-09-28 14:53:50 -04:00
|
|
|
|
2025-10-07 12:38:06 -04:00
|
|
|
@invitation = Family::Invitation.find_by(token: invitation_token)
|
2025-10-04 19:09:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self_hosted_mode?
|
|
|
|
|
env_value = ENV['SELF_HOSTED']
|
|
|
|
|
return ActiveModel::Type::Boolean.new.cast(env_value) unless env_value.nil?
|
|
|
|
|
|
|
|
|
|
false
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def valid_invitation_token?
|
2025-10-04 18:42:21 -04:00
|
|
|
@invitation&.can_be_accepted?
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def invitation_token
|
|
|
|
|
@invitation_token ||= params[:invitation_token] ||
|
2025-10-30 14:59:31 -04:00
|
|
|
params.dig(:user, :invitation_token) ||
|
|
|
|
|
session[:invitation_token]
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def accept_invitation_for_user(user)
|
|
|
|
|
return unless @invitation&.can_be_accepted?
|
|
|
|
|
|
|
|
|
|
service = Families::AcceptInvitation.new(
|
|
|
|
|
invitation: @invitation,
|
|
|
|
|
user: user
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if service.call
|
|
|
|
|
flash[:notice] = "Welcome to #{@invitation.family.name}! You're now part of the family."
|
|
|
|
|
else
|
2025-10-30 14:59:31 -04:00
|
|
|
flash[:alert] =
|
|
|
|
|
"Account created successfully, but there was an issue accepting the invitation: #{service.error_message}"
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
Rails.logger.error "Error accepting invitation during registration: #{e.message}"
|
2025-10-30 14:59:31 -04:00
|
|
|
flash[:alert] =
|
|
|
|
|
'Account created successfully, but there was an issue accepting the invitation. Please try accepting it again.'
|
2025-09-28 14:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sign_up_params
|
|
|
|
|
super
|
|
|
|
|
end
|
2025-11-24 13:45:09 -05:00
|
|
|
|
|
|
|
|
def email_password_registration_allowed?
|
2025-11-24 14:04:31 -05:00
|
|
|
ALLOW_EMAIL_PASSWORD_REGISTRATION
|
2025-11-24 13:45:09 -05:00
|
|
|
end
|
2025-10-04 16:39:47 -04:00
|
|
|
end
|