Skip to content

Commit

Permalink
Add custom census authorization handler
Browse files Browse the repository at this point in the history
  • Loading branch information
fblupi committed Jan 13, 2025
1 parent dba6abe commit f8121dd
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ tailwind.config.js
Capfile
config/deploy.rb
config/deploy

lib/assets/custom_census.csv
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ Free Open-Source participatory democracy, citizen participation and open governm

This is the open-source repository for decidim_inspire, based on [Decidim](https://github.com/decidim/decidim).

## Custom census authorization handler

This authorization handler allows users to be directly verified with their birthdates by checking the census in a CSV file.

You need to create a CSV file `lib/asses/census.csv` with two columns: `email` and `birthdate`. For example:

```csv
email,date_of_birth
[email protected],1956-03-14
[email protected],1998-12-06
```

The verification will succeed if the user is in the census and introduces the same birthdate as the one in the CSV.

This authorization handler will allow us to work with the [Decidim Kids](https://github.com/AjuntamentdeBarcelona/decidim-module-kids) module.

## Setting up the application

You will need to do some steps before having the app working properly once you have deployed it:
Expand Down
38 changes: 38 additions & 0 deletions app/services/custom_census_authorization_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# Checks the authorization against the census for Barcelona.
require "digest/md5"

# This class performs a check against the census file in order to verify the citizen's residence.
class CustomCensusAuthorizationHandler < Decidim::AuthorizationHandler
attribute :date_of_birth, Date

validates :date_of_birth, presence: true

validate :present_in_census

def metadata
super.merge(date_of_birth: parsed_date_of_birth)
end

def unique_id
Digest::MD5.hexdigest("#{user.email}-#{Rails.application.secrets.secret_key_base}")
end

private

def parsed_date_of_birth
@parsed_date_of_birth ||= date_of_birth&.strftime("%Y-%m-%d")
end

def present_in_census
result = census.find { |row| row["email"] == user.email }
return errors.add(:base, I18n.t("custom_census_authorization_handler.errors.not_found")) unless result

errors.add(:date_of_birth, I18n.t("custom_census_authorization_handler.errors.invalid_date_of_birth")) unless result["date_of_birth"] == parsed_date_of_birth
end

def census
@census ||= CSV.read(Rails.root.join("lib/assets/custom_census.csv"), headers: true)
end
end
7 changes: 7 additions & 0 deletions app/views/custom_census_authorization/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="form__wrapper">
<div class="field date">
<%= form.date_select :date_of_birth, start_year: 1900, end_year: 1.year.ago.year, default: 20.years.ago, prompt: { day: t(".date_select.day"), month: t(".date_select.month"), year: t(".date_select.year") } %>
</div>

<%= form.hidden_field :handler_name %>
</div>
2 changes: 2 additions & 0 deletions config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ ignore_missing:
- layouts.decidim.footer.cc_by_license
- layouts.decidim.footer.decidim_logo
- layouts.decidim.footer.made_with_open_source
- custom_census_authorization.form.*
- custom_census_authorization_handler.errors.*
6 changes: 6 additions & 0 deletions config/initializers/decidim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,9 @@

# Inform Decidim about the assets folder
Decidim.register_assets_path File.expand_path("app/packs", Rails.application.root)

Decidim::Verifications.register_workflow(:custom_census_authorization_handler) do |auth|
auth.form = "CustomCensusAuthorizationHandler"
auth.renewable = true
auth.time_between_renewals = 1.day
end
26 changes: 26 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
en:
activemodel:
attributes:
custom_census_authorization_handler:
date_of_birth: Date of birth
custom_census_authorization:
form:
date_select:
day: Day
month: Month
year: Year
custom_census_authorization_handler:
errors:
not_found: The user is not present in the census
invalid_date_of_birth: The date of birth is not correct
decidim:
authorization_handlers:
custom_census_authorization_handler:
explanation: Verify against the custom census authorization handler
fields:
date_of_birth: Date of birth
name: Custom census
verifications:
authorizations:
first_login:
actions:
custom_census_authorization_handler: Verify against the custom census authorization handler

0 comments on commit f8121dd

Please sign in to comment.