Sha256: 989d735522958fe11a1fc9c4da14eee88fd0831754e08f881872524d6b35911d

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Decidim
  # Shared behaviour for signed_in users that require the latest TOS accepted
  module NeedsTosAccepted
    extend ActiveSupport::Concern

    included do
      before_action :tos_accepted_by_user
      helper_method :terms_and_conditions_page
    end

    private

    def tos_accepted_by_user
      return true unless current_user
      return if current_user.tos_accepted?
      return if permitted_paths?

      redirect_to_tos
    end

    def terms_and_conditions_page
      @terms_and_conditions_page ||= Decidim::StaticPage.find_by(slug: "terms-and-conditions", organization: current_organization)
    end

    def permitted_paths?
      permitted_paths = [tos_path,
                         decidim.delete_account_path,
                         decidim.accept_tos_path,
                         decidim.data_portability_path,
                         decidim.export_data_portability_path,
                         decidim.download_file_data_portability_path]
      # ensure that path with or without query string pass
      permitted_paths.find { |el| el.split("?").first == request.path }
    end

    def tos_path
      decidim.page_path terms_and_conditions_page
    end

    def redirect_to_tos
      flash[:notice] = flash[:notice] if flash[:notice]
      flash[:secondary] = t("required_review.alert", scope: "decidim.pages.terms_and_conditions")
      redirect_to tos_path
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-core-0.18.1 app/controllers/concerns/decidim/needs_tos_accepted.rb
decidim-core-0.18.0 app/controllers/concerns/decidim/needs_tos_accepted.rb