Files
astuto/app/helpers/application_helper.rb

83 lines
2.3 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
2023-01-18 21:11:27 +01: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: {})
custom_domain = Current.tenant.custom_domain if not disallow_custom_domain and Current.tenant
2024-03-24 12:54:02 +01:00
if options[:subdomain].blank? && Rails.application.multi_tenancy? && (custom_domain.blank? || disallow_custom_domain)
2024-03-24 12:54:02 +01:00
options[:subdomain] = Current.tenant.subdomain
end
if custom_domain.blank? || disallow_custom_domain
options[:host] = Rails.application.base_url
else
options[:host] = custom_domain
end
if Rails.application.base_url.include?('https')
options[:protocol] = 'https'
else
options[:protocol] = 'http'
end
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
2019-08-18 14:51:37 +02:00
end