Files
astuto/app/helpers/application_helper.rb

104 lines
3.1 KiB
Ruby
Raw Normal View History

2019-08-18 14:51:37 +02:00
module ApplicationHelper
def check_user_signed_in
2022-05-01 18:00:38 +02:00
unless user_signed_in?
2022-07-23 13:32:40 +02:00
flash[:alert] = t('errors.not_logged_in')
2022-05-01 18:00:38 +02:00
redirect_to new_user_session_path
return false
end
end
2024-05-03 18:11:07 +02:00
def authenticate_owner
return if check_user_signed_in == false
unless current_user.owner?
flash[:alert] = t('errors.not_enough_privileges')
redirect_to root_path
return
end
end
def authenticate_admin
return if check_user_signed_in == false
unless current_user.admin?
2022-07-23 13:32:40 +02:00
flash[:alert] = t('errors.not_enough_privileges')
redirect_to root_path
2022-05-01 18:00:38 +02:00
return
end
end
2023-01-18 21:11:27 +01:00
def authenticate_moderator
return if check_user_signed_in == false
2022-05-01 18:00:38 +02:00
2024-07-12 20:38:46 +02:00
unless current_user.moderator?
2022-07-23 13:32:40 +02:00
flash[:alert] = t('errors.not_enough_privileges')
2022-05-01 18:00:38 +02:00
redirect_to root_path
return
end
end
2022-07-18 10:47:54 +02:00
2024-03-24 12:54:02 +01:00
def get_url_for(url_helper, resource: nil, disallow_custom_domain: false, options: {})
2024-11-25 17:31:46 +01:00
logger.info { "(start) Call to get_url_for with tenant: #{Current.tenant&.inspect}, url_helper = #{url_helper}, resource = #{resource}, disallow_custom_domain = #{disallow_custom_domain}, options = #{options}" }
custom_domain = Current.tenant.custom_domain if not disallow_custom_domain and Current.tenant
2024-11-25 17:31:46 +01:00
subdomain = ''
host = ''
2024-03-24 12:54:02 +01:00
2024-11-25 17:31:46 +01:00
if not options[:subdomain].blank?
subdomain = options[:subdomain]
end
if options[:subdomain].blank? && Rails.application.multi_tenancy? && (custom_domain.blank? || disallow_custom_domain)
2024-11-25 17:31:46 +01:00
subdomain = Current.tenant.subdomain
2024-03-24 12:54:02 +01:00
end
if custom_domain.blank? || disallow_custom_domain
2024-11-25 17:31:46 +01:00
host = Rails.application.base_url
host = host.gsub(%r{\Ahttps?://|/$}, '')
host = "#{subdomain}.#{host}" if subdomain.present?
2024-03-24 12:54:02 +01:00
else
2024-11-25 17:31:46 +01:00
host = custom_domain
2024-03-24 12:54:02 +01:00
end
2024-11-25 17:31:46 +01:00
options[:host] = host
2024-03-24 12:54:02 +01:00
if Rails.application.base_url.include?('https')
options[:protocol] = 'https'
else
options[:protocol] = 'http'
end
2022-07-18 10:47:54 +02:00
2024-11-25 17:31:46 +01:00
logger.info { "(end) Call to get_url_for with options = #{options}, resulting url = #{resource ? url_helper.call(resource, options) : url_helper.call(options)}" }
2022-07-18 10:47:54 +02:00
resource ? url_helper.call(resource, options) : url_helper.call(options)
end
2024-03-24 12:54:02 +01:00
def get_tenant_from_request(request)
if Rails.application.multi_tenancy?
request_host = request.headers['HTTP_X_FORWARDED_HOST'] || request.host
request_host_splitted = request_host.split('.')
2024-03-24 12:54:02 +01:00
app_host_splitted = URI.parse(Rails.application.base_url).host.split('.')
if app_host_splitted.join('.') == request_host_splitted.last(app_host_splitted.length).join('.')
return nil if request.subdomain.blank? or RESERVED_SUBDOMAINS.include?(request.subdomain)
2024-03-24 12:54:02 +01:00
tenant = Tenant.find_by(subdomain: request.subdomain)
else
tenant = Tenant.find_by(custom_domain: request_host)
2024-03-24 12:54:02 +01:00
end
else
tenant = Tenant.first
end
tenant
end
# Redirect to previous page if present; otherwise redirect to root
def safe_return_to_redirect(url)
uri = URI.parse(url)
uri.host.present? && uri.host != request.host ? yield : url
rescue URI::InvalidURIError
yield
end
2019-08-18 14:51:37 +02:00
end