mirror of
https://github.com/internetee/registry.git
synced 2025-08-02 07:52:04 +02:00
feat: add age validation for admin contacts
- Add AgeValidation module for consistent age checks - Validate admin contacts must be at least 18 years old - Move age validation logic from Domain to shared module - Add tests for admin contact age validation - Fix JSON format for admin_contacts_allowed_ident_type setting This change ensures that administrative contacts must be adults (18+), using the same age validation logic as for registrants. The validation works with both birthday and Estonian ID formats. Settings are now properly stored as JSON strings for consistent parsing.
This commit is contained in:
parent
66619f12fe
commit
7799727867
10 changed files with 181 additions and 51 deletions
|
@ -1,4 +1,8 @@
|
|||
class AdminDomainContact < DomainContact
|
||||
include AgeValidation
|
||||
|
||||
validate :validate_contact_age
|
||||
|
||||
# rubocop:disable Metrics/AbcSize
|
||||
# rubocop:disable Metrics/MethodLength
|
||||
def self.replace(current_contact, new_contact)
|
||||
|
@ -23,4 +27,14 @@ class AdminDomainContact < DomainContact
|
|||
end
|
||||
# rubocop:enable Metrics/AbcSize
|
||||
# rubocop:enable Metrics/MethodLength
|
||||
|
||||
private
|
||||
|
||||
def validate_contact_age
|
||||
return unless contact&.underage?
|
||||
|
||||
errors.add(:contact, I18n.t(
|
||||
'activerecord.errors.models.admin_domain_contact.contact_too_young'
|
||||
))
|
||||
end
|
||||
end
|
||||
|
|
53
app/models/concerns/age_validation.rb
Normal file
53
app/models/concerns/age_validation.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
module AgeValidation
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def underage?
|
||||
case ident_type
|
||||
when 'birthday'
|
||||
underage_by_birthday?
|
||||
when 'priv'
|
||||
underage_by_estonian_id?
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def underage_by_birthday?
|
||||
birth_date = Date.parse(ident)
|
||||
calculate_age(birth_date) < 18
|
||||
end
|
||||
|
||||
def underage_by_estonian_id?
|
||||
return false unless estonian_id?
|
||||
|
||||
birth_date = parse_estonian_id_birth_date(ident)
|
||||
calculate_age(birth_date) < 18
|
||||
end
|
||||
|
||||
def estonian_id?
|
||||
ident_country_code == 'EE' && ident.match?(/^\d{11}$/)
|
||||
end
|
||||
|
||||
def calculate_age(birth_date)
|
||||
((Time.zone.now - birth_date.to_time) / 1.year.seconds).floor
|
||||
end
|
||||
|
||||
def parse_estonian_id_birth_date(id_code)
|
||||
century_number = id_code[0].to_i
|
||||
year_digits = id_code[1..2]
|
||||
month = id_code[3..4]
|
||||
day = id_code[5..6]
|
||||
|
||||
birth_year = case century_number
|
||||
when 1, 2 then "18#{year_digits}"
|
||||
when 3, 4 then "19#{year_digits}"
|
||||
when 5, 6 then "20#{year_digits}"
|
||||
else
|
||||
raise ArgumentError, "Invalid century number in Estonian ID"
|
||||
end
|
||||
|
||||
Date.parse("#{birth_year}-#{month}-#{day}")
|
||||
end
|
||||
end
|
|
@ -10,6 +10,7 @@ class Contact < ApplicationRecord
|
|||
include Contact::Archivable
|
||||
include Contact::CompanyRegister
|
||||
include EmailVerifable
|
||||
include AgeValidation
|
||||
|
||||
belongs_to :original, class_name: 'Contact'
|
||||
belongs_to :registrar, required: true
|
||||
|
|
|
@ -12,6 +12,7 @@ class Domain < ApplicationRecord
|
|||
include Domain::Releasable
|
||||
include Domain::Disputable
|
||||
include Domain::BulkUpdatable
|
||||
include AgeValidation
|
||||
|
||||
PERIODS = [
|
||||
['3 months', '3m'],
|
||||
|
@ -866,7 +867,7 @@ class Domain < ApplicationRecord
|
|||
return true if registrant.org? && Setting.admin_contacts_required_for_org
|
||||
return false unless registrant.priv?
|
||||
|
||||
underage_registrant? && Setting.admin_contacts_required_for_minors
|
||||
registrant.underage? && Setting.admin_contacts_required_for_minors
|
||||
end
|
||||
|
||||
def require_tech_contacts?
|
||||
|
@ -876,51 +877,7 @@ class Domain < ApplicationRecord
|
|||
private
|
||||
|
||||
def underage_registrant?
|
||||
case registrant.ident_type
|
||||
when 'birthday'
|
||||
underage_by_birthday?
|
||||
when 'priv'
|
||||
underage_by_estonian_id?
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def underage_by_birthday?
|
||||
birth_date = Date.parse(registrant.ident)
|
||||
calculate_age(birth_date) < 18
|
||||
end
|
||||
|
||||
def underage_by_estonian_id?
|
||||
return false unless estonian_id?
|
||||
|
||||
birth_date = parse_estonian_id_birth_date(registrant.ident)
|
||||
calculate_age(birth_date) < 18
|
||||
end
|
||||
|
||||
def estonian_id?
|
||||
registrant.ident_country_code == 'EE' && registrant.ident.match?(/^\d{11}$/)
|
||||
end
|
||||
|
||||
def calculate_age(birth_date)
|
||||
((Time.zone.now - birth_date.to_time) / 1.year.seconds).floor
|
||||
end
|
||||
|
||||
def parse_estonian_id_birth_date(id_code)
|
||||
century_number = id_code[0].to_i
|
||||
year_digits = id_code[1..2]
|
||||
month = id_code[3..4]
|
||||
day = id_code[5..6]
|
||||
|
||||
birth_year = case century_number
|
||||
when 1, 2 then "18#{year_digits}"
|
||||
when 3, 4 then "19#{year_digits}"
|
||||
when 5, 6 then "20#{year_digits}"
|
||||
else
|
||||
raise ArgumentError, "Invalid century number in Estonian ID"
|
||||
end
|
||||
|
||||
Date.parse("#{birth_year}-#{month}-#{day}")
|
||||
registrant.underage?
|
||||
end
|
||||
|
||||
def validate_admin_contacts_ident_type
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue