mirror of
https://github.com/internetee/registry.git
synced 2025-08-04 17:01:44 +02:00
parent
c5e1516d89
commit
feb8d83a26
17 changed files with 809 additions and 195 deletions
|
@ -1,6 +1,7 @@
|
|||
class Admin::ContactsController < AdminController
|
||||
load_and_authorize_resource
|
||||
before_action :set_contact, only: [:show]
|
||||
helper_method :ident_types
|
||||
|
||||
def index
|
||||
params[:q] ||= {}
|
||||
|
@ -78,4 +79,8 @@ class Admin::ContactsController < AdminController
|
|||
|
||||
params[:q][:created_at_lteq] = ca_cache
|
||||
end
|
||||
|
||||
def ident_types
|
||||
Contact::Ident.types
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,6 +2,7 @@ class Registrar
|
|||
class ContactsController < DeppController
|
||||
before_action :init_epp_contact
|
||||
helper_method :address_processing?
|
||||
helper_method :ident_types
|
||||
|
||||
def index
|
||||
authorize! :view, Depp::Contact
|
||||
|
@ -140,5 +141,9 @@ class Registrar
|
|||
def address_processing?
|
||||
Contact.address_processing?
|
||||
end
|
||||
|
||||
def ident_types
|
||||
Contact::Ident.types
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,6 +11,12 @@ module EppErrors
|
|||
epp_errors << collect_child_errors(attr)
|
||||
end
|
||||
|
||||
if self.class.reflect_on_aggregation(attr)
|
||||
aggregation = send(attr)
|
||||
epp_errors << collect_aggregation_errors(aggregation)
|
||||
next
|
||||
end
|
||||
|
||||
epp_errors << collect_parent_errors(attr, errors)
|
||||
end
|
||||
|
||||
|
@ -46,6 +52,31 @@ module EppErrors
|
|||
epp_errors
|
||||
end
|
||||
|
||||
def collect_aggregation_errors(aggregation)
|
||||
epp_errors = []
|
||||
|
||||
aggregation.errors.details.each do |attr, error_details|
|
||||
error_details.each do |error_detail|
|
||||
aggregation.class.epp_code_map.each do |epp_code, attr_to_error|
|
||||
epp_code_found = attr_to_error.any? { |i| i == [attr, error_detail[:error]] }
|
||||
|
||||
if epp_code_found
|
||||
message = aggregation.errors.generate_message(attr, error_detail[:error], error_detail)
|
||||
message = aggregation.errors.full_message(attr, message)
|
||||
|
||||
if attr != :base
|
||||
message = "#{aggregation.model_name.human} #{message.camelize(:lower)}"
|
||||
end
|
||||
|
||||
epp_errors << { code: epp_code, msg: message }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
epp_errors
|
||||
end
|
||||
|
||||
def find_epp_code_and_value(msg)
|
||||
epp_code_map.each do |code, values|
|
||||
values.each do |x|
|
||||
|
|
|
@ -19,27 +19,22 @@ class Contact < ActiveRecord::Base
|
|||
|
||||
accepts_nested_attributes_for :legal_documents
|
||||
|
||||
validates :name, :email, :ident, :ident_type, presence: true
|
||||
validates :name, :email, presence: true
|
||||
validates :street, :city, :zip, :country_code, presence: true, if: 'self.class.address_processing?'
|
||||
|
||||
validates :phone, presence: true, e164: true, phone: true
|
||||
|
||||
validates :email, format: /@/
|
||||
validates :email, email_format: { message: :invalid }, if: proc { |c| c.email_changed? }
|
||||
validates :ident,
|
||||
format: { with: /\d{4}-\d{2}-\d{2}/, message: :invalid_birthday_format },
|
||||
if: proc { |c| c.ident_type == 'birthday' }
|
||||
validates :ident_country_code, presence: true, if: proc { |c| %w(org priv).include? c.ident_type }, on: :create
|
||||
|
||||
validates :code,
|
||||
uniqueness: { message: :epp_id_taken },
|
||||
format: { with: /\A[\w\-\:\.\_]*\z/i, message: :invalid },
|
||||
length: { maximum: 100, message: :too_long_contact_code }
|
||||
validates_associated :identifier
|
||||
|
||||
validate :val_ident_type
|
||||
validate :val_ident_valid_format?
|
||||
validate :validate_html
|
||||
validate :validate_country_code
|
||||
validate :validate_ident_country_code
|
||||
|
||||
after_initialize do
|
||||
self.status_notes = {} if status_notes.nil?
|
||||
|
@ -49,8 +44,15 @@ class Contact < ActiveRecord::Base
|
|||
before_validation :to_upcase_country_code
|
||||
before_validation :strip_email
|
||||
before_create :generate_auth_info
|
||||
|
||||
before_update :manage_emails
|
||||
|
||||
composed_of :identifier,
|
||||
class_name: 'Contact::Ident',
|
||||
constructor: Proc.new { |code, type, country_code| Contact::Ident.new(code: code,
|
||||
type: type,
|
||||
country_code: country_code) },
|
||||
mapping: [%w[ident code], %w[ident_type type], %w[ident_country_code country_code]]
|
||||
|
||||
def manage_emails
|
||||
return nil unless email_changed?
|
||||
return nil unless deliver_emails == true
|
||||
|
@ -76,12 +78,6 @@ class Contact < ActiveRecord::Base
|
|||
BIRTHDAY = 'birthday'.freeze
|
||||
PASSPORT = 'passport'
|
||||
|
||||
IDENT_TYPES = [
|
||||
ORG, # Company registry code (or similar)
|
||||
PRIV, # National idendtification number
|
||||
BIRTHDAY # Birthday date
|
||||
]
|
||||
|
||||
attr_accessor :deliver_emails
|
||||
attr_accessor :domain_transfer # hack but solves problem faster
|
||||
|
||||
|
@ -299,28 +295,6 @@ class Contact < ActiveRecord::Base
|
|||
name || '[no name]'
|
||||
end
|
||||
|
||||
def val_ident_type
|
||||
errors.add(:ident_type, :epp_ident_type_invalid, code: code) if !%w(org priv birthday).include?(ident_type)
|
||||
end
|
||||
|
||||
def val_ident_valid_format?
|
||||
case ident_country_code
|
||||
when 'EE'.freeze
|
||||
err_msg = "invalid_EE_identity_format#{"_update" if id}".to_sym
|
||||
case ident_type
|
||||
when 'priv'.freeze
|
||||
errors.add(:ident, err_msg) unless Isikukood.new(ident).valid?
|
||||
when 'org'.freeze
|
||||
# !%w(1 7 8 9).freeze.include?(ident.first) ||
|
||||
if ident.size != 8 || !(ident =~/\A[0-9]{8}\z/)
|
||||
errors.add(:ident, err_msg)
|
||||
end
|
||||
when BIRTHDAY
|
||||
errors.add(:ident, err_msg) if id.blank? # only for create action right now. Later for all of them
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def validate_html
|
||||
self.class.columns.each do |column|
|
||||
next unless column.type == :string
|
||||
|
@ -334,7 +308,6 @@ class Contact < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
def org?
|
||||
ident_type == ORG
|
||||
end
|
||||
|
@ -344,10 +317,6 @@ class Contact < ActiveRecord::Base
|
|||
!org?
|
||||
end
|
||||
|
||||
def birthday?
|
||||
ident_type == BIRTHDAY
|
||||
end
|
||||
|
||||
def generate_auth_info
|
||||
return if @generate_auth_info_disabled
|
||||
return if auth_info.present?
|
||||
|
@ -424,10 +393,6 @@ class Contact < ActiveRecord::Base
|
|||
errors.add(:country_code, :invalid) unless Country.new(country_code)
|
||||
end
|
||||
|
||||
def validate_ident_country_code
|
||||
errors.add(:ident, :invalid_country_code) unless Country.new(ident_country_code)
|
||||
end
|
||||
|
||||
def related_domain_descriptions
|
||||
ActiveSupport::Deprecation.warn('Use #domain_names_with_roles')
|
||||
|
||||
|
|
72
app/models/contact/ident.rb
Normal file
72
app/models/contact/ident.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
class Contact::Ident
|
||||
include ActiveModel::Model
|
||||
|
||||
attr_accessor :code
|
||||
attr_accessor :type
|
||||
attr_accessor :country_code
|
||||
|
||||
validates :code, presence: true, code: true
|
||||
validates :code, iso8601: { date_only: true }, if: :birthday?
|
||||
validates :type, presence: true, inclusion: { in: Proc.new { types } }
|
||||
validates :country_code, presence: true, iso31661_alpha2: true
|
||||
validate :mismatched
|
||||
|
||||
def self.epp_code_map
|
||||
{
|
||||
'2003' => [
|
||||
[:code, :blank],
|
||||
[:type, :blank],
|
||||
[:country_code, :blank],
|
||||
],
|
||||
'2005' => [
|
||||
[:base, :mismatch],
|
||||
[:code, :invalid_national_id],
|
||||
[:code, :invalid_reg_no],
|
||||
[:code, :invalid_iso8601],
|
||||
[:country_code, :invalid_iso31661_alpha2],
|
||||
],
|
||||
}
|
||||
end
|
||||
|
||||
def self.types
|
||||
%w[org priv birthday]
|
||||
end
|
||||
|
||||
Mismatch = Struct.new(:type, :country)
|
||||
|
||||
def self.mismatches
|
||||
[
|
||||
Mismatch.new('birthday', Country.new('EE')),
|
||||
]
|
||||
end
|
||||
|
||||
def marked_for_destruction?
|
||||
false
|
||||
end
|
||||
|
||||
def birthday?
|
||||
type == 'birthday'
|
||||
end
|
||||
|
||||
def national_id?
|
||||
type == 'priv'
|
||||
end
|
||||
|
||||
def reg_no?
|
||||
type == 'org'
|
||||
end
|
||||
|
||||
def country
|
||||
Country.new(country_code)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# https://github.com/rails/rails/issues/1513
|
||||
def validation_context=(value); end
|
||||
|
||||
def mismatched
|
||||
mismatched = self.class.mismatches.include?(Mismatch.new(type, country))
|
||||
errors.add(:base, :mismatch, type: type, country: country) if mismatched
|
||||
end
|
||||
end
|
|
@ -109,7 +109,7 @@ class Epp::Contact < Contact
|
|||
end
|
||||
delegate :ident_attr_valid?, to: :class
|
||||
|
||||
def epp_code_map # rubocop:disable Metrics/MethodLength
|
||||
def epp_code_map
|
||||
{
|
||||
'2003' => [ # Required parameter missing
|
||||
[:name, :blank],
|
||||
|
@ -124,27 +124,16 @@ class Epp::Contact < Contact
|
|||
[:name, :invalid],
|
||||
[:phone, :invalid],
|
||||
[:email, :invalid],
|
||||
[:ident, :invalid],
|
||||
[:ident, :invalid_EE_identity_format],
|
||||
[:ident, :invalid_EE_identity_format_update],
|
||||
[:ident, :invalid_birthday_format],
|
||||
[:ident, :invalid_country_code],
|
||||
[:country_code, :invalid],
|
||||
[:ident_type, :missing],
|
||||
[:code, :invalid],
|
||||
[:code, :too_long_contact_code]
|
||||
],
|
||||
'2302' => [ # Object exists
|
||||
[:code, :epp_id_taken]
|
||||
],
|
||||
'2304' => [ # Object status prohibits operation
|
||||
[:ident_type, :epp_ident_type_invalid, { value: { obj: 'code', val: code}, interpolation: {code: code}}]
|
||||
],
|
||||
'2305' => [ # Association exists
|
||||
[:domains, :exist]
|
||||
],
|
||||
'2306' => [ # Parameter policy error
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -164,36 +153,26 @@ class Epp::Contact < Contact
|
|||
|
||||
self.deliver_emails = true # turn on email delivery for epp
|
||||
|
||||
# Allow ident data update for legacy contacts
|
||||
if frame.css('ident').first
|
||||
self.ident_updated_at ||= Time.zone.now
|
||||
ident_frame = frame.css('ident').first
|
||||
|
||||
if ident_frame && ident_attr_valid?(ident_frame)
|
||||
org_priv = %w(org priv).freeze
|
||||
|
||||
if ident_frame.text == ident
|
||||
if ident_type.present? && ident_country_code.present?
|
||||
at.merge!(ident_type: ident_frame.attr('type'))
|
||||
at.merge!(ident_country_code: ident_frame.attr('cc'))
|
||||
elsif ident_country_code.blank? && org_priv.include?(ident_type) && org_priv.include?(ident_frame.attr('type'))
|
||||
at.merge!(ident_country_code: ident_frame.attr('cc'), ident_type: ident_frame.attr('type'))
|
||||
elsif ident_type == 'birthday' && !ident[/\A\d{4}-\d{2}-\d{2}\z/] && (Date.parse(ident) rescue false)
|
||||
at.merge!(ident: ident_frame.text)
|
||||
at.merge!(ident_country_code: ident_frame.attr('cc'))
|
||||
elsif ident_type == 'birthday' && ident_country_code.blank?
|
||||
at.merge!(ident_country_code: ident_frame.attr('cc'))
|
||||
elsif ident_type.blank? && ident_country_code.blank?
|
||||
at.merge!(ident_type: ident_frame.attr('type'))
|
||||
at.merge!(ident_country_code: ident_frame.attr('cc'))
|
||||
else
|
||||
deny_ident_update
|
||||
end
|
||||
else
|
||||
deny_ident_update
|
||||
end
|
||||
else
|
||||
# https://github.com/internetee/registry/issues/576
|
||||
if identifier.valid?
|
||||
deny_ident_update
|
||||
else
|
||||
if ident_frame && ident_attr_valid?(ident_frame)
|
||||
deny_ident_update if ident_frame.text != ident
|
||||
|
||||
identifier = Ident.new(code: ident,
|
||||
type: ident_frame.attr('type'),
|
||||
country_code: ident_frame.attr('cc'),
|
||||
)
|
||||
|
||||
identifier.validate
|
||||
|
||||
self.identifier = identifier
|
||||
self.ident_updated_at ||= Time.zone.now
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
29
app/validators/contact/ident/code_validator.rb
Normal file
29
app/validators/contact/ident/code_validator.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
class Contact::Ident::CodeValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
if record.country_code == 'EE'
|
||||
if record.national_id?
|
||||
record.errors.add(attribute,
|
||||
:invalid_national_id,
|
||||
country: record.country) unless valid_national_id_ee?(value)
|
||||
end
|
||||
|
||||
if record.reg_no?
|
||||
validator = ActiveModel::Validations::FormatValidator.new(with: reg_no_ee_format,
|
||||
attributes: attribute,
|
||||
message: :invalid_reg_no,
|
||||
country: record.country)
|
||||
validator.validate(record)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reg_no_ee_format
|
||||
/\A[0-9]{8}\z/
|
||||
end
|
||||
|
||||
def valid_national_id_ee?(ident)
|
||||
Isikukood.new(ident).valid?
|
||||
end
|
||||
end
|
|
@ -19,7 +19,7 @@
|
|||
.col-md-3
|
||||
.form-group
|
||||
= label_tag t(:ident_type)
|
||||
= select_tag '[q][ident_type_eq]', options_for_select(Contact::IDENT_TYPES, params[:q][:ident_type_eq]), { include_blank: true, placeholder: t(:choose), class: 'form-control selectize' }
|
||||
= select_tag '[q][ident_type_eq]', options_for_select(ident_types, params[:q][:ident_type_eq]), { include_blank: true, placeholder: t(:choose), class: 'form-control selectize' }
|
||||
.row
|
||||
.col-md-3
|
||||
.form-group
|
||||
|
@ -75,7 +75,7 @@
|
|||
.row
|
||||
.col-md-12
|
||||
.table-responsive
|
||||
%table.table.table-hover.table-bordered.table-condensed
|
||||
%table.table.table-hover.table-bordered.table-condensed.contacts
|
||||
%thead
|
||||
%tr
|
||||
%th{class: 'col-xs-2'}
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
.col-md-3
|
||||
.form-group
|
||||
= label_tag t(:ident_type)
|
||||
= select_tag '[q][ident_type_eq]', options_for_select(Contact::IDENT_TYPES, params[:q][:ident_type_eq]), { include_blank: true, placeholder: t(:choose), class: 'form-control selectize' }
|
||||
= select_tag '[q][ident_type_eq]', options_for_select(ident_types, params[:q][:ident_type_eq]), { include_blank: true, placeholder: t(:choose), class: 'form-control selectize' }
|
||||
.row
|
||||
.col-md-3
|
||||
.form-group
|
||||
|
@ -85,7 +85,7 @@
|
|||
.row
|
||||
.col-md-12
|
||||
.table-responsive
|
||||
%table.table.table-hover.table-bordered.table-condensed
|
||||
%table.table.table-hover.table-bordered.table-condensed.contacts
|
||||
%thead
|
||||
%tr
|
||||
%th{class: 'col-xs-2'}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue