Sha256: 08e217a58935d10ea8af7a9efe1899da6fa4e0e3cff810c97c3cdc252de3b63c

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

# An AuthorizationHandler that uses information uploaded from a CSV file
# to authorize against the age of the user
class FileAuthorizationHandler < Decidim::AuthorizationHandler
  # This is the input (from the user) to validate against
  attribute :id_document, String
  # This is a hack in order to sign initiatives because in decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb
  # send to handler_for a param named document_number.
  attribute :document_number, String
  attribute :birthdate, Decidim::Attributes::LocalizedDate

  # This is the validation to perform
  # If passed, an authorization is created
  validates :id_document, presence: true
  validates :birthdate, presence: true
  validate :censed

  def metadata
    @metadata ||= begin
      meta = { birthdate: census_for_user&.birthdate&.strftime("%Y/%m/%d") }
      census_for_user&.extras&.each_pair do |key, value|
        meta[key.to_sym] = value
      end
      meta
    end
  end

  # This is required in new 0.8.4 version of decicim
  # however, there's a bug and this doesn't work
  def handler_name
    +"file_authorization_handler"
  end

  # Checks if the id_document belongs to the census
  def censed
    return if census_for_user&.birthdate == birthdate

    errors.add(:id_document, I18n.t("decidim.file_authorization_handler.errors.messages.not_censed"))
  end

  def authorized?
    return true if census_for_user
  end

  def unique_id
    return nil unless organization

    Digest::SHA256.hexdigest("#{census_for_user&.id_document}-#{organization.id}-#{Rails.application.secrets.secret_key_base}")
  end

  def census_for_user
    return unless organization

    @census_for_user ||= Decidim::FileAuthorizationHandler::CensusDatum
                         .search_id_document(organization, id_document || document_number)
  end

  def organization
    current_organization || user&.organization
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-file_authorization_handler-0.27.1.6 app/services/file_authorization_handler.rb