Sha256: fc88b7009f3f72e73d703f29282e28785547403cedd3ef17643e0f046506b897

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class CustomRedirectForm < Decidim::Form
        include Decidim::TranslatableAttributes
        attribute :origin, String
        attribute :destination, String
        attribute :active, Boolean
        attribute :pass_query, Boolean

        validates :origin, :destination, presence: true
        validate :different_origin_destination

        def to_params
          [
            sanitize_url(origin),
            {
              destination: sanitize_url(destination, strip_host: false),
              active: active,
              pass_query: pass_query
            }
          ]
        end

        def sanitize_url(url, strip_host: true)
          url = url.strip.downcase
          parsed = Addressable::URI.parse(url)
          url = parsed.path if strip_host && parsed.host == current_organization.host
          url = "/#{url}" unless url.match?(%r{^https?://|^/})
          url
        end

        private

        def different_origin_destination
          return if sanitize_url(origin) != sanitize_url(destination)

          errors.add(:destination, :invalid)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.9.3 app/forms/decidim/decidim_awesome/admin/custom_redirect_form.rb
decidim-decidim_awesome-0.9.1 app/forms/decidim/decidim_awesome/admin/custom_redirect_form.rb
decidim-decidim_awesome-0.8.3 app/forms/decidim/decidim_awesome/admin/custom_redirect_form.rb
decidim-decidim_awesome-0.8.2 app/forms/decidim/decidim_awesome/admin/custom_redirect_form.rb
decidim-decidim_awesome-0.8.1 app/forms/decidim/decidim_awesome/admin/custom_redirect_form.rb
decidim-decidim_awesome-0.8.0 app/forms/decidim/decidim_awesome/admin/custom_redirect_form.rb