Class: FileAuthorizationHandler

Inherits:
Decidim::AuthorizationHandler
  • Object
show all
Defined in:
app/services/file_authorization_handler.rb

Overview

An AuthorizationHandler that uses information uploaded from a CSV file to authorize against the age of the user

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ FileAuthorizationHandler

Customized constructor to support decidim-initiatives.



17
18
19
20
# File 'app/services/file_authorization_handler.rb', line 17

def initialize(params)
  params[:id_document] = params.delete(:document_number) unless params.has_key?(:id_document)
  super(params)
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/services/file_authorization_handler.rb', line 45

def authorized?
  return true if census_for_user
end

#censedObject

Checks if the id_document belongs to the census



39
40
41
42
43
# File 'app/services/file_authorization_handler.rb', line 39

def censed
  return if census_for_user&.birthdate == birthdate

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

#census_for_userObject



55
56
57
58
59
60
# File 'app/services/file_authorization_handler.rb', line 55

def census_for_user
  return unless organization

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

#handler_nameObject

This is required in new 0.8.4 version of decicim however, there’s a bug and this doesn’t work



34
35
36
# File 'app/services/file_authorization_handler.rb', line 34

def handler_name
  +"file_authorization_handler"
end

#metadataObject



22
23
24
25
26
27
28
29
30
# File 'app/services/file_authorization_handler.rb', line 22

def 
  @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

#organizationObject



62
63
64
# File 'app/services/file_authorization_handler.rb', line 62

def organization
  current_organization || user&.organization
end

#unique_idObject



49
50
51
52
53
# File 'app/services/file_authorization_handler.rb', line 49

def unique_id
  return nil unless organization

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