2023-05-21 17:59:36 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class ApplicationController < ActionController::Base
|
2024-01-09 01:25:11 +02:00
|
|
|
BROWSER_LOCALE_REGEXP = /\A\w{2}(?:-\w{2})?/
|
|
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
include ActiveStorage::SetCurrent
|
2023-06-24 01:01:54 +03:00
|
|
|
include Pagy::Backend
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2023-09-17 00:45:57 +03:00
|
|
|
check_authorization unless: :devise_controller?
|
|
|
|
|
|
2024-09-28 01:21:35 +03:00
|
|
|
around_action :with_locale
|
2023-07-05 00:22:05 +03:00
|
|
|
before_action :sign_in_for_demo, if: -> { Docuseal.demo? }
|
2023-05-21 17:59:36 +03:00
|
|
|
before_action :maybe_redirect_to_setup, unless: :signed_in?
|
|
|
|
|
before_action :authenticate_user!, unless: :devise_controller?
|
|
|
|
|
|
|
|
|
|
helper_method :button_title,
|
2023-06-11 12:42:13 +00:00
|
|
|
:current_account,
|
|
|
|
|
:svg_icon
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-01-20 23:53:14 +02:00
|
|
|
impersonates :user, with: ->(uuid) { User.find_by(uuid:) }
|
|
|
|
|
|
2023-06-29 02:04:17 +03:00
|
|
|
rescue_from Pagy::OverflowError do
|
|
|
|
|
redirect_to request.path
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-16 23:19:47 +02:00
|
|
|
rescue_from RateLimit::LimitApproached do |e|
|
|
|
|
|
Rollbar.error(e) if defined?(Rollbar)
|
|
|
|
|
|
|
|
|
|
redirect_to request.referer, alert: 'Too many requests', status: :too_many_requests
|
|
|
|
|
end
|
|
|
|
|
|
2023-09-17 00:45:57 +03:00
|
|
|
if Rails.env.production?
|
|
|
|
|
rescue_from CanCan::AccessDenied do |e|
|
2024-02-16 19:44:59 +02:00
|
|
|
Rollbar.warning(e) if defined?(Rollbar)
|
2023-09-17 00:45:57 +03:00
|
|
|
|
2024-01-20 23:53:14 +02:00
|
|
|
redirect_to root_path, alert: e.message
|
2023-09-17 00:45:57 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-20 23:42:56 +03:00
|
|
|
def default_url_options
|
|
|
|
|
Docuseal.default_url_options
|
|
|
|
|
end
|
|
|
|
|
|
2024-01-20 23:53:14 +02:00
|
|
|
def impersonate_user(user)
|
|
|
|
|
raise ArgumentError unless user
|
|
|
|
|
raise Pretender::Error unless true_user
|
|
|
|
|
|
|
|
|
|
@impersonated_user = user
|
|
|
|
|
|
|
|
|
|
request.session[:impersonated_user_id] = user.uuid
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
private
|
|
|
|
|
|
2024-09-28 01:21:35 +03:00
|
|
|
def with_locale(&)
|
|
|
|
|
return yield unless current_account
|
|
|
|
|
|
|
|
|
|
locale = params[:lang].presence if Rails.env.development?
|
|
|
|
|
locale ||= current_account.locale
|
|
|
|
|
|
|
|
|
|
I18n.with_locale(locale, &)
|
2024-09-23 15:22:07 +03:00
|
|
|
end
|
|
|
|
|
|
2024-01-09 01:25:11 +02:00
|
|
|
def with_browser_locale(&)
|
2024-09-28 01:21:35 +03:00
|
|
|
return yield if I18n.locale != :'en-US'
|
|
|
|
|
|
2024-06-08 23:02:29 +03:00
|
|
|
locale = params[:lang].presence
|
|
|
|
|
locale ||= request.env['HTTP_ACCEPT_LANGUAGE'].to_s[BROWSER_LOCALE_REGEXP].to_s
|
2024-01-09 01:25:11 +02:00
|
|
|
|
|
|
|
|
locale =
|
|
|
|
|
if locale.starts_with?('en-') && locale != 'en-US'
|
|
|
|
|
'en-GB'
|
|
|
|
|
else
|
|
|
|
|
locale.split('-').first.presence || 'en-GB'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
locale = 'en-GB' unless I18n.locale_available?(locale)
|
|
|
|
|
|
|
|
|
|
I18n.with_locale(locale, &)
|
|
|
|
|
end
|
|
|
|
|
|
2023-07-05 00:22:05 +03:00
|
|
|
def sign_in_for_demo
|
2023-10-14 21:19:21 +03:00
|
|
|
sign_in(User.active.order('random()').take) unless signed_in?
|
2023-07-05 00:22:05 +03:00
|
|
|
end
|
|
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
def current_account
|
|
|
|
|
current_user&.account
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def maybe_redirect_to_setup
|
|
|
|
|
redirect_to setup_index_path unless User.exists?
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-18 19:47:47 +03:00
|
|
|
def button_title(title: I18n.t('submit'), disabled_with: I18n.t('submitting'), title_class: '', icon: nil,
|
|
|
|
|
icon_disabled: nil)
|
2023-11-30 01:50:22 +02:00
|
|
|
render_to_string(partial: 'shared/button_title',
|
|
|
|
|
locals: { title:, disabled_with:, title_class:, icon:, icon_disabled: })
|
2023-06-11 12:42:13 +00:00
|
|
|
end
|
|
|
|
|
|
2023-06-11 15:51:52 +03:00
|
|
|
def svg_icon(icon_name, class: '')
|
|
|
|
|
render_to_string(partial: "icons/#{icon_name}", locals: { class: })
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
end
|