Sha256: f30065bbcb1bb20567831038b8602c50710031ed40c2617e8ed55a294a066917

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 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
  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

  # Customized constructor to support decidim-initiatives.
  def initialize(params)
    params[:id_document] = params.delete(:document_number) unless params.has_key?(:id_document)
    super(params)
  end

  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)
  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.7 app/services/file_authorization_handler.rb