-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom census authorization handler (#10)
* Add custom census authorization handler * Store census in the database instead of in a CSV
- Loading branch information
Showing
9 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,25 @@ 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 records in a table. | ||
|
||
You need to create records for the model `Decidim::CustomCensusRecord`. For example: | ||
|
||
```ruby | ||
[ | ||
{ email: "[email protected]", date_of_birth: "1956-03-14" }, | ||
{ email: "[email protected]", date_of_birth: "1998-12-06" } | ||
].each do |record| | ||
Decidim::CustomCensusRecord.create(email: record[:email], metadata: { date_of_birth: record[:date_of_birth] }) | ||
end | ||
``` | ||
|
||
The verification will succeed if the user is in the census and introduces the same birthdate as the one in the database. | ||
|
||
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
class CustomCensusRecord < ApplicationRecord | ||
include Decidim::RecordEncryptor | ||
|
||
validates :email, uniqueness: true | ||
|
||
encrypt_attribute :metadata, type: :hash | ||
|
||
def date_of_birth | ||
metadata["date_of_birth"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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 | ||
record = Decidim::CustomCensusRecord.find_by(email: user.email) | ||
return errors.add(:base, I18n.t("custom_census_authorization_handler.errors.not_found")) unless record | ||
|
||
errors.add(:date_of_birth, I18n.t("custom_census_authorization_handler.errors.invalid_date_of_birth")) unless record.date_of_birth == parsed_date_of_birth | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
db/migrate/20250115084854_create_decidim_custom_census_records.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateDecidimCustomCensusRecords < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :decidim_custom_census_records do |t| | ||
t.string :email, null: false | ||
t.jsonb :metadata | ||
|
||
Decidim::Authorization | ||
t.timestamps | ||
end | ||
|
||
add_index :decidim_custom_census_records, :email, unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.