Sha256: 33da742a8d3087337e29afa0b1f58f571ac2054f27d07f741cb889eaf02e5ad0

Contents?: true

Size: 1.95 KB

Versions: 15

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module GoodJob
  class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception

    around_action :use_good_job_locale

    content_security_policy do |policy|
      policy.default_src(:none) if policy.default_src(*policy.default_src).blank?
      policy.connect_src(:self) if policy.connect_src(*policy.connect_src).blank?
      policy.base_uri(:none) if policy.base_uri(*policy.base_uri).blank?
      policy.font_src(:self) if policy.font_src(*policy.font_src).blank?
      policy.img_src(:self, :data) if policy.img_src(*policy.img_src).blank?
      policy.object_src(:none) if policy.object_src(*policy.object_src).blank?
      policy.script_src(:self) if policy.script_src(*policy.script_src).blank?
      policy.style_src(:self) if policy.style_src(*policy.style_src).blank?
      policy.form_action(:self) if policy.form_action(*policy.form_action).blank?
      policy.frame_ancestors(:none) if policy.frame_ancestors(*policy.frame_ancestors).blank?
    end

    before_action do
      next if request.content_security_policy_nonce_generator

      request.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(16) }
    end

    private

    def default_url_options(options = {})
      { locale: I18n.locale }.merge(options)
    end

    def use_good_job_locale(&action)
      @original_i18n_config = I18n.config
      I18n.config = ::GoodJob::I18nConfig.new
      I18n.with_locale(current_locale, &action)
    ensure
      I18n.config = @original_i18n_config
      @original_i18n_config = nil
    end

    def use_original_locale
      prev_config = I18n.config
      I18n.config = @original_i18n_config if @original_i18n_config
      yield
    ensure
      I18n.config = prev_config
    end

    def current_locale
      if request.GET['locale']
        request.GET['locale']
      elsif params[:locale]
        params[:locale]
      else
        I18n.default_locale
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
good_job-3.19.0 app/controllers/good_job/application_controller.rb
good_job-3.18.3 app/controllers/good_job/application_controller.rb
good_job-3.18.2 app/controllers/good_job/application_controller.rb
good_job-3.18.1 app/controllers/good_job/application_controller.rb
good_job-3.18.0 app/controllers/good_job/application_controller.rb
good_job-3.17.4 app/controllers/good_job/application_controller.rb
good_job-3.17.3 app/controllers/good_job/application_controller.rb
good_job-3.17.2 app/controllers/good_job/application_controller.rb
good_job-3.17.1 app/controllers/good_job/application_controller.rb
good_job-3.17.0 app/controllers/good_job/application_controller.rb
good_job-3.16.4 app/controllers/good_job/application_controller.rb
good_job-3.16.3 app/controllers/good_job/application_controller.rb
good_job-3.16.2 app/controllers/good_job/application_controller.rb
good_job-3.16.1 app/controllers/good_job/application_controller.rb
good_job-3.16.0 app/controllers/good_job/application_controller.rb