2015-08-10 11:05:06 +02:00
|
|
|
class ApplicationController < ActionController::Base
|
2019-02-01 17:17:10 +01:00
|
|
|
include TrustedDeviceConcern
|
2022-11-15 14:56:59 +01:00
|
|
|
include Pundit::Authorization
|
2020-03-18 16:05:04 +01:00
|
|
|
include Devise::StoreLocationExtension
|
2021-07-06 16:29:23 +02:00
|
|
|
include ApplicationController::LongLivedAuthenticityToken
|
2021-07-06 12:34:23 +02:00
|
|
|
include ApplicationController::ErrorHandling
|
2019-02-01 17:17:10 +01:00
|
|
|
|
2018-04-26 10:52:41 +02:00
|
|
|
MAINTENANCE_MESSAGE = 'Le site est actuellement en maintenance. Il sera à nouveau disponible dans un court instant.'
|
|
|
|
|
2021-01-28 14:49:22 +01:00
|
|
|
before_action :set_sentry_user
|
2019-02-01 17:17:10 +01:00
|
|
|
before_action :redirect_if_untrusted
|
2021-04-06 12:21:49 +02:00
|
|
|
before_action :reject, if: -> { ENV.fetch("MAINTENANCE_MODE", 'false') == 'true' }
|
2020-11-04 16:35:15 +01:00
|
|
|
before_action :configure_permitted_parameters, if: :devise_controller?
|
2017-07-24 12:29:09 +02:00
|
|
|
|
2018-01-16 16:31:47 +01:00
|
|
|
before_action :staging_authenticate
|
2018-04-26 14:36:27 +02:00
|
|
|
before_action :set_active_storage_host
|
2019-11-19 14:40:28 +01:00
|
|
|
before_action :setup_javascript_settings
|
2019-03-26 16:02:08 +01:00
|
|
|
before_action :setup_tracking
|
2022-01-26 18:46:43 +01:00
|
|
|
before_action :set_customizable_view_path
|
2021-05-12 16:30:35 +02:00
|
|
|
|
|
|
|
around_action :switch_locale
|
2018-01-16 16:31:47 +01:00
|
|
|
|
2021-02-09 10:24:13 +01:00
|
|
|
helper_method :multiple_devise_profile_connect?, :instructeur_signed_in?, :current_instructeur, :current_expert, :expert_signed_in?,
|
2023-08-25 22:53:30 +02:00
|
|
|
:administrateur_signed_in?, :current_administrateur, :current_account, :localization_enabled?, :set_locale, :current_expert_not_instructeur?,
|
2023-09-04 12:07:04 +02:00
|
|
|
:gestionnaire_signed_in?, :current_gestionnaire
|
2019-08-08 17:33:49 +02:00
|
|
|
|
2022-10-11 19:18:59 +02:00
|
|
|
before_action do
|
|
|
|
Current.request_id = request.uuid
|
2023-06-21 16:42:41 +02:00
|
|
|
Current.user = current_user
|
2023-10-17 12:00:16 +02:00
|
|
|
Current.browser = browser
|
2024-03-05 16:32:18 +01:00
|
|
|
# TODO: remove this block when migration to new domain is done
|
2024-03-27 11:04:46 +01:00
|
|
|
Current.host = request.host_with_port
|
2024-03-19 21:41:45 +01:00
|
|
|
|
2024-03-27 11:04:46 +01:00
|
|
|
if Current.host.include?(".gouv.fr")
|
2024-03-19 21:41:45 +01:00
|
|
|
Current.application_name = "demarches.gouv.fr"
|
2024-03-19 22:13:40 +01:00
|
|
|
Current.contact_email = "contact@demarches.gouv.fr"
|
2024-03-19 22:31:27 +01:00
|
|
|
Current.application_base_url = "https://demarches.gouv.fr"
|
2024-03-19 21:41:45 +01:00
|
|
|
else
|
|
|
|
Current.application_name = APPLICATION_NAME
|
2024-03-19 22:13:40 +01:00
|
|
|
Current.contact_email = CONTACT_EMAIL
|
2024-03-21 14:01:52 +01:00
|
|
|
Current.application_base_url = APPLICATION_BASE_URL
|
2024-03-19 21:41:45 +01:00
|
|
|
end
|
2022-10-11 19:18:59 +02:00
|
|
|
end
|
|
|
|
|
2018-01-16 16:31:47 +01:00
|
|
|
def staging_authenticate
|
|
|
|
if StagingAuthService.enabled? && !authenticate_with_http_basic { |username, password| StagingAuthService.authenticate(username, password) }
|
|
|
|
request_http_basic_authentication
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-08 17:44:33 +02:00
|
|
|
def multiple_devise_profile_connect?
|
|
|
|
user_signed_in? && instructeur_signed_in? ||
|
|
|
|
instructeur_signed_in? && administrateur_signed_in? ||
|
2023-09-04 12:07:04 +02:00
|
|
|
instructeur_signed_in? && gestionnaire_signed_in? ||
|
2021-02-09 10:24:13 +01:00
|
|
|
instructeur_signed_in? && expert_signed_in? ||
|
|
|
|
user_signed_in? && administrateur_signed_in? ||
|
2023-09-04 12:07:04 +02:00
|
|
|
user_signed_in? && gestionnaire_signed_in? ||
|
2023-08-25 22:53:30 +02:00
|
|
|
user_signed_in? && expert_signed_in? ||
|
2023-09-04 12:07:04 +02:00
|
|
|
administrateur_signed_in? && gestionnaire_signed_in?
|
2019-08-08 17:44:33 +02:00
|
|
|
end
|
|
|
|
|
2019-08-08 17:33:49 +02:00
|
|
|
def current_instructeur
|
|
|
|
current_user&.instructeur
|
|
|
|
end
|
|
|
|
|
|
|
|
def instructeur_signed_in?
|
|
|
|
user_signed_in? && current_user&.instructeur.present?
|
|
|
|
end
|
|
|
|
|
2019-08-09 10:38:51 +02:00
|
|
|
def current_administrateur
|
|
|
|
current_user&.administrateur
|
|
|
|
end
|
|
|
|
|
|
|
|
def administrateur_signed_in?
|
|
|
|
current_administrateur.present?
|
|
|
|
end
|
|
|
|
|
2023-09-04 12:07:04 +02:00
|
|
|
def current_gestionnaire
|
|
|
|
current_user&.gestionnaire
|
2023-08-25 22:53:30 +02:00
|
|
|
end
|
|
|
|
|
2023-09-04 12:07:04 +02:00
|
|
|
def gestionnaire_signed_in?
|
|
|
|
current_gestionnaire.present?
|
2023-08-25 22:53:30 +02:00
|
|
|
end
|
|
|
|
|
2021-02-09 10:24:13 +01:00
|
|
|
def current_expert
|
|
|
|
current_user&.expert
|
|
|
|
end
|
|
|
|
|
2023-09-08 13:52:38 +02:00
|
|
|
def current_expert_not_instructeur?
|
|
|
|
current_user&.expert? && !current_user&.instructeur?
|
|
|
|
end
|
|
|
|
|
2021-02-09 10:24:13 +01:00
|
|
|
def expert_signed_in?
|
|
|
|
current_expert.present?
|
|
|
|
end
|
|
|
|
|
2020-06-24 13:21:21 +02:00
|
|
|
def current_account
|
|
|
|
{
|
2023-09-04 12:07:04 +02:00
|
|
|
gestionnaire: current_gestionnaire,
|
2020-06-24 13:21:21 +02:00
|
|
|
administrateur: current_administrateur,
|
|
|
|
instructeur: current_instructeur,
|
2023-12-19 11:22:50 +01:00
|
|
|
expert: current_expert,
|
2020-06-24 13:21:21 +02:00
|
|
|
user: current_user
|
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :pundit_user, :current_account
|
|
|
|
|
2021-08-24 11:39:15 +02:00
|
|
|
def localization_enabled?
|
2021-09-01 15:52:24 +02:00
|
|
|
ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' || cookies[:locale].present? || !browser_prefers_french?
|
|
|
|
end
|
|
|
|
|
|
|
|
def browser_prefers_french?
|
|
|
|
http_accept_language.compatible_language_from(I18n.available_locales) == 'fr'
|
2021-08-24 11:39:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_locale(locale)
|
|
|
|
if locale && locale.to_sym.in?(I18n.available_locales)
|
2024-07-03 11:54:10 +02:00
|
|
|
cookies[:locale] = { value: locale, secure: Rails.env.production?, httponly: true }
|
2021-09-01 18:50:38 +02:00
|
|
|
if user_signed_in?
|
|
|
|
current_user.update(locale: locale)
|
|
|
|
end
|
2021-08-24 11:39:15 +02:00
|
|
|
locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-13 09:23:40 +02:00
|
|
|
def ajax_redirect(path)
|
|
|
|
"window.location.href='#{path}'"
|
|
|
|
end
|
|
|
|
|
2024-02-27 18:46:06 +01:00
|
|
|
def message_verifier
|
|
|
|
@message_verifier ||= ActiveSupport::MessageVerifier.new(Rails.application.secret_key_base)
|
|
|
|
end
|
|
|
|
|
2017-01-03 11:32:21 +01:00
|
|
|
protected
|
|
|
|
|
2019-07-04 12:36:17 +02:00
|
|
|
def feature_enabled?(feature_name)
|
|
|
|
Flipper.enabled?(feature_name, current_user)
|
|
|
|
end
|
|
|
|
|
2018-09-07 15:44:00 +02:00
|
|
|
def authenticate_logged_user!
|
2019-08-06 11:02:54 +02:00
|
|
|
if instructeur_signed_in?
|
|
|
|
authenticate_instructeur!
|
2021-02-09 10:24:13 +01:00
|
|
|
elsif expert_signed_in?
|
|
|
|
authenticate_expert!
|
2018-09-07 15:44:00 +02:00
|
|
|
elsif administrateur_signed_in?
|
|
|
|
authenticate_administrateur!
|
2023-09-04 12:07:04 +02:00
|
|
|
elsif gestionnaire_signed_in?
|
|
|
|
authenticate_gestionnaire!
|
2018-09-07 15:44:00 +02:00
|
|
|
else
|
|
|
|
authenticate_user!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
def authenticate_instructeur!
|
2019-08-08 17:33:49 +02:00
|
|
|
if !instructeur_signed_in?
|
2017-01-03 11:32:21 +01:00
|
|
|
redirect_to new_user_session_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-09 10:24:13 +01:00
|
|
|
def authenticate_expert!
|
|
|
|
if !expert_signed_in?
|
|
|
|
redirect_to new_user_session_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-20 15:27:59 +02:00
|
|
|
def authenticate_instructeur_or_expert!
|
|
|
|
if !instructeur_signed_in? && !expert_signed_in?
|
|
|
|
redirect_to new_user_session_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-03 11:32:21 +01:00
|
|
|
def authenticate_administrateur!
|
2019-08-09 10:38:51 +02:00
|
|
|
if !administrateur_signed_in?
|
2017-01-03 11:32:21 +01:00
|
|
|
redirect_to new_user_session_path
|
|
|
|
end
|
|
|
|
end
|
2017-06-28 07:08:25 +02:00
|
|
|
|
2023-09-04 12:07:04 +02:00
|
|
|
def authenticate_gestionnaire!
|
|
|
|
if !gestionnaire_signed_in?
|
2023-08-25 22:53:30 +02:00
|
|
|
redirect_to new_user_session_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-14 18:12:07 +02:00
|
|
|
def after_sign_out_path_for(_resource_or_scope)
|
|
|
|
stored_location_for(:user) || super
|
|
|
|
end
|
|
|
|
|
2020-11-04 16:35:15 +01:00
|
|
|
def configure_permitted_parameters
|
|
|
|
devise_parameter_sanitizer.permit(:sign_in, keys: [:otp_attempt])
|
|
|
|
end
|
|
|
|
|
2017-06-28 07:08:25 +02:00
|
|
|
private
|
|
|
|
|
2018-04-26 14:36:27 +02:00
|
|
|
def set_active_storage_host
|
2023-11-10 13:30:43 +01:00
|
|
|
ActiveStorage::Current.url_options ||= {}
|
|
|
|
ActiveStorage::Current.url_options[:host] = request.base_url
|
2018-04-26 14:36:27 +02:00
|
|
|
end
|
|
|
|
|
2019-11-19 14:40:28 +01:00
|
|
|
def setup_javascript_settings
|
|
|
|
gon.autosave = Rails.application.config.ds_autosave
|
2020-01-14 18:46:07 +01:00
|
|
|
gon.autocomplete = Rails.application.secrets.autocomplete
|
2019-11-19 14:40:28 +01:00
|
|
|
end
|
|
|
|
|
2019-03-26 16:02:08 +01:00
|
|
|
def setup_tracking
|
|
|
|
gon.matomo = matomo_config
|
|
|
|
gon.sentry = sentry_config
|
|
|
|
|
|
|
|
if administrateur_signed_in?
|
2019-05-15 14:15:48 +02:00
|
|
|
gon.crisp = crisp_config
|
2019-03-26 16:02:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-17 11:01:20 +02:00
|
|
|
def current_user_roles
|
|
|
|
@current_user_roles ||= begin
|
|
|
|
roles = [
|
|
|
|
current_user,
|
|
|
|
current_instructeur,
|
2023-12-14 10:11:54 +01:00
|
|
|
current_expert,
|
2019-09-17 11:01:20 +02:00
|
|
|
current_administrateur,
|
2023-09-04 12:07:04 +02:00
|
|
|
current_gestionnaire,
|
2020-11-05 15:09:11 +01:00
|
|
|
current_super_admin
|
2019-09-17 11:01:20 +02:00
|
|
|
].compact.map { |role| role.class.name }
|
2018-08-07 16:13:40 +02:00
|
|
|
|
2019-09-17 11:01:20 +02:00
|
|
|
roles.any? ? roles.join(', ') : 'Guest'
|
|
|
|
end
|
2018-01-17 14:40:31 +01:00
|
|
|
end
|
2017-06-28 07:08:25 +02:00
|
|
|
|
2021-01-28 14:49:22 +01:00
|
|
|
def set_sentry_user
|
|
|
|
Sentry.set_user(sentry_user)
|
2017-06-28 07:08:25 +02:00
|
|
|
end
|
2018-01-17 14:40:31 +01:00
|
|
|
|
2023-03-15 18:25:12 +01:00
|
|
|
def set_sentry_dossier(dossier)
|
|
|
|
Sentry.configure_scope do |scope|
|
|
|
|
scope.set_tags(procedure: dossier.procedure.id)
|
|
|
|
scope.set_tags(dossier: dossier.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-18 23:11:38 +01:00
|
|
|
# private method called by rails fwk
|
|
|
|
# see https://github.com/roidrage/lograge
|
2018-08-12 10:31:28 +02:00
|
|
|
def append_info_to_payload(payload)
|
|
|
|
super
|
2018-08-07 16:13:40 +02:00
|
|
|
|
2023-11-08 12:19:03 +01:00
|
|
|
payload[:to_log] = {}
|
|
|
|
request_logs(payload[:to_log])
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_logs(logs)
|
|
|
|
logs.merge!({
|
2018-01-17 14:40:31 +01:00
|
|
|
user_agent: request.user_agent,
|
2019-09-17 11:01:20 +02:00
|
|
|
user_id: current_user&.id,
|
2023-02-01 17:57:27 +01:00
|
|
|
user_roles: current_user_roles,
|
|
|
|
client_ip: request.headers['X-Forwarded-For'],
|
|
|
|
request_id: request.headers['X-Request-ID']
|
2023-11-08 12:19:03 +01:00
|
|
|
})
|
2018-01-17 14:40:31 +01:00
|
|
|
|
|
|
|
if browser.known?
|
2023-11-08 12:19:03 +01:00
|
|
|
logs.merge!({
|
2018-01-17 14:40:31 +01:00
|
|
|
browser: browser.name,
|
|
|
|
browser_version: browser.version.to_s,
|
2018-10-01 14:06:08 +02:00
|
|
|
platform: browser.platform.name
|
2018-01-17 14:40:31 +01:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2018-01-30 14:43:56 +01:00
|
|
|
|
2018-04-26 10:52:41 +02:00
|
|
|
def reject
|
|
|
|
authorized_request =
|
|
|
|
request.path_info == '/' ||
|
|
|
|
request.path_info.start_with?('/manager') ||
|
2020-11-05 15:09:11 +01:00
|
|
|
request.path_info.start_with?('/super_admins')
|
2018-04-26 10:52:41 +02:00
|
|
|
|
|
|
|
api_request = request.path_info.start_with?('/api/')
|
|
|
|
|
2020-11-05 15:09:11 +01:00
|
|
|
if super_admin_signed_in? || authorized_request
|
2018-04-26 10:52:41 +02:00
|
|
|
flash.now.alert = MAINTENANCE_MESSAGE
|
|
|
|
elsif api_request
|
|
|
|
render json: { error: MAINTENANCE_MESSAGE }.to_json, status: :service_unavailable
|
|
|
|
else
|
2019-08-06 11:02:54 +02:00
|
|
|
[:user, :instructeur, :administrateur].each { |role| sign_out(role) }
|
2018-04-26 10:52:41 +02:00
|
|
|
flash[:alert] = MAINTENANCE_MESSAGE
|
|
|
|
redirect_to root_path
|
|
|
|
end
|
|
|
|
end
|
2019-02-01 17:17:10 +01:00
|
|
|
|
|
|
|
def redirect_if_untrusted
|
2019-08-06 11:02:54 +02:00
|
|
|
if instructeur_signed_in? &&
|
2019-02-01 17:17:10 +01:00
|
|
|
sensitive_path &&
|
2021-11-25 15:37:40 +01:00
|
|
|
!current_instructeur.bypass_email_login_token &&
|
2019-04-03 14:27:28 +02:00
|
|
|
!IPService.ip_trusted?(request.headers['X-Forwarded-For']) &&
|
2019-02-01 17:17:10 +01:00
|
|
|
!trusted_device?
|
|
|
|
|
2019-02-06 20:51:04 +01:00
|
|
|
# return at this location
|
|
|
|
# after the device is trusted
|
2020-03-20 10:26:21 +01:00
|
|
|
if get_stored_location_for(:user).blank?
|
|
|
|
store_location_for(:user, request.fullpath)
|
|
|
|
end
|
2019-02-06 20:51:04 +01:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
send_login_token_or_bufferize(current_instructeur)
|
2024-02-27 18:52:59 +01:00
|
|
|
signed_email = message_verifier.generate(current_instructeur.email, purpose: :reset_link, expires_in: 1.hour)
|
|
|
|
redirect_to link_sent_path(email: signed_email)
|
2019-02-01 17:17:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sensitive_path
|
|
|
|
path = request.path_info
|
|
|
|
|
|
|
|
if path == '/' ||
|
|
|
|
path == '/users/sign_out' ||
|
2019-08-13 19:45:51 +02:00
|
|
|
path == '/contact' ||
|
|
|
|
path == '/contact-admin' ||
|
2019-02-01 17:17:10 +01:00
|
|
|
path.start_with?('/connexion-par-jeton') ||
|
|
|
|
path.start_with?('/api/') ||
|
2024-06-13 14:01:05 +02:00
|
|
|
path.start_with?('/lien-envoye') ||
|
|
|
|
path.start_with?('/faq')
|
2019-02-01 17:17:10 +01:00
|
|
|
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2019-03-26 16:02:08 +01:00
|
|
|
|
2019-04-03 12:13:34 +02:00
|
|
|
def sentry_user
|
2022-09-28 12:40:44 +02:00
|
|
|
if user_signed_in?
|
|
|
|
{ id: "User##{current_user.id}" }
|
|
|
|
elsif administrateur_signed_in?
|
|
|
|
{ id: "Administrateur##{current_administrateur.id}" }
|
|
|
|
else
|
|
|
|
{ id: 'Guest' }
|
|
|
|
end
|
2019-04-03 12:13:34 +02:00
|
|
|
end
|
|
|
|
|
2019-03-26 16:02:08 +01:00
|
|
|
def sentry_config
|
|
|
|
sentry = Rails.application.secrets.sentry
|
|
|
|
|
|
|
|
{
|
2021-05-12 15:00:13 +02:00
|
|
|
key: sentry[:js_client_key],
|
2019-03-26 16:02:08 +01:00
|
|
|
enabled: sentry[:enabled],
|
2019-04-03 12:13:34 +02:00
|
|
|
environment: sentry[:environment],
|
2020-03-04 16:23:54 +01:00
|
|
|
browser: { modern: BrowserSupport.supported?(browser) },
|
2023-05-23 14:25:52 +02:00
|
|
|
user: sentry_user,
|
2024-04-22 16:45:06 +02:00
|
|
|
release: ApplicationVersion.current
|
2019-03-26 16:02:08 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def matomo_config
|
|
|
|
matomo = Rails.application.secrets.matomo
|
|
|
|
|
|
|
|
{
|
2022-02-15 11:55:24 +01:00
|
|
|
cookieDomain: matomo[:cookie_domain],
|
2022-01-26 13:57:49 +01:00
|
|
|
domain: matomo[:domain],
|
2022-01-18 12:47:01 +01:00
|
|
|
enabled: matomo[:enabled],
|
|
|
|
host: matomo[:host],
|
|
|
|
key: matomo[:client_key]
|
2019-03-26 16:02:08 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-05-15 14:15:48 +02:00
|
|
|
def crisp_config
|
|
|
|
crisp = Rails.application.secrets.crisp
|
|
|
|
|
2019-09-25 17:51:28 +02:00
|
|
|
nb_demarches_by_state = if current_administrateur.present?
|
|
|
|
current_administrateur.procedures.group(:aasm_state).count
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2019-05-15 14:15:48 +02:00
|
|
|
{
|
|
|
|
key: crisp[:client_key],
|
|
|
|
enabled: crisp[:enabled],
|
|
|
|
administrateur: {
|
2019-09-17 11:01:20 +02:00
|
|
|
email: current_user&.email,
|
|
|
|
DS_SIGN_IN_COUNT: current_user&.sign_in_count,
|
2019-05-15 14:15:48 +02:00
|
|
|
DS_CREATED_AT: current_administrateur&.created_at,
|
2019-07-11 16:05:41 +02:00
|
|
|
DS_ID: current_administrateur&.id,
|
2022-07-07 11:39:03 +02:00
|
|
|
DS_NB_DEMARCHES_BROUILLONS: nb_demarches_by_state['brouillon'] || 0,
|
|
|
|
DS_NB_DEMARCHES_ACTIVES: nb_demarches_by_state['publiee'] || 0,
|
|
|
|
DS_NB_DEMARCHES_ARCHIVES: nb_demarches_by_state['close'] || 0
|
2019-05-15 14:15:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-26 16:02:08 +01:00
|
|
|
def current_email
|
2019-09-17 11:01:20 +02:00
|
|
|
current_user&.email
|
2019-03-26 16:02:08 +01:00
|
|
|
end
|
2020-08-27 11:37:03 +02:00
|
|
|
|
2021-05-12 16:30:35 +02:00
|
|
|
def switch_locale(&action)
|
2024-04-02 21:53:56 +02:00
|
|
|
unchecked_locale = extract_locale_from_query_params ||
|
2021-08-24 11:39:15 +02:00
|
|
|
extract_locale_from_cookie ||
|
2021-09-01 18:50:38 +02:00
|
|
|
extract_locale_from_user ||
|
2021-08-24 11:39:15 +02:00
|
|
|
extract_locale_from_accept_language_header ||
|
|
|
|
I18n.default_locale
|
|
|
|
|
2024-04-02 21:53:56 +02:00
|
|
|
locale = unchecked_locale.to_sym.in?(I18n.available_locales) ? unchecked_locale : I18n.default_locale
|
|
|
|
|
2023-02-22 17:08:36 +01:00
|
|
|
gon.locale = locale
|
|
|
|
|
2021-05-12 16:30:35 +02:00
|
|
|
I18n.with_locale(locale, &action)
|
2020-08-27 11:37:03 +02:00
|
|
|
end
|
2021-08-24 11:39:15 +02:00
|
|
|
|
|
|
|
def extract_locale_from_query_params
|
|
|
|
set_locale(request.query_parameters[:locale])
|
|
|
|
end
|
|
|
|
|
2021-09-01 18:50:38 +02:00
|
|
|
def extract_locale_from_user
|
|
|
|
current_user&.locale
|
|
|
|
end
|
|
|
|
|
2021-08-24 11:39:15 +02:00
|
|
|
def extract_locale_from_cookie
|
|
|
|
cookies[:locale]
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_locale_from_accept_language_header
|
|
|
|
if localization_enabled?
|
|
|
|
http_accept_language.compatible_language_from(I18n.available_locales)
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 18:46:43 +01:00
|
|
|
|
|
|
|
def set_customizable_view_path
|
|
|
|
prepend_view_path "app/custom_views"
|
|
|
|
end
|
2022-05-03 10:09:58 +02:00
|
|
|
|
2024-03-28 09:45:05 +01:00
|
|
|
def try_nav_bar_profile_from_referrer
|
|
|
|
# detect context from referer, simple (no detection when refreshing the page)
|
|
|
|
params = Rails.application.routes.recognize_path(request&.referer)
|
|
|
|
|
|
|
|
controller_class = "#{params[:controller].camelize}Controller".safe_constantize
|
|
|
|
return if controller_class.nil?
|
|
|
|
|
|
|
|
controller_instance = controller_class.new
|
|
|
|
controller_instance.try(:nav_bar_profile)
|
|
|
|
end
|
|
|
|
|
2022-11-09 12:33:20 +01:00
|
|
|
def cast_bool(value)
|
|
|
|
ActiveRecord::Type::Boolean.new.deserialize(value)
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|