mirror of
https://github.com/internetee/registry.git
synced 2025-05-19 18:59:38 +02:00
Refactor settings
This commit is contained in:
parent
26f5eda636
commit
dafcb6f78c
25 changed files with 132 additions and 282 deletions
|
@ -1,42 +0,0 @@
|
|||
class DelegationSigner < ActiveRecord::Base
|
||||
include EppErrors
|
||||
has_one :dnskeys
|
||||
|
||||
validate :validate_dnskeys_uniqueness
|
||||
validate :validate_dnskeys_count
|
||||
|
||||
def epp_code_map
|
||||
sg = SettingGroup.domain_validation
|
||||
|
||||
{
|
||||
'2004' => [ # Parameter value range error
|
||||
[:dnskeys, :out_of_range,
|
||||
{
|
||||
min: sg.setting(Setting::DNSKEYS_MIN_COUNT).value,
|
||||
max: sg.setting(Setting::DNSKEYS_MAX_COUNT).value
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
def validate_dnskeys_count
|
||||
sg = SettingGroup.domain_validation
|
||||
min, max = sg.setting(:dnskeys_min_count).value.to_i, sg.setting(:dnskeys_max_count).value.to_i
|
||||
return if dnskeys.reject(&:marked_for_destruction?).length.between?(min, max)
|
||||
errors.add(:dnskeys, :out_of_range, { min: min, max: max })
|
||||
end
|
||||
|
||||
def validate_dnskeys_uniqueness
|
||||
validated = []
|
||||
list = dnskeys.reject(&:marked_for_destruction?)
|
||||
list.each do |dnskey|
|
||||
next if dnskey.public_key.blank?
|
||||
existing = list.select { |x| x.public_key == dnskey.public_key }
|
||||
next unless existing.length > 1
|
||||
validated << dnskey.public_key
|
||||
errors.add(:dnskeys, :invalid) if errors[:dnskeys].blank?
|
||||
dnskey.errors.add(:public_key, :taken)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -66,11 +66,9 @@ class Dnskey < ActiveRecord::Base
|
|||
hex = [domain.name_in_wire_format, flags_hex, protocol_hex, alg_hex, public_key_hex].join
|
||||
bin = self.class.hex_to_bin(hex)
|
||||
|
||||
sg = SettingGroup.dnskeys.setting(Setting::DS_ALGORITHM).value
|
||||
|
||||
if sg == '1'
|
||||
if Setting.ds_algorithm == 1
|
||||
self.ds_digest = Digest::SHA1.hexdigest(bin).upcase
|
||||
elsif sg == '2'
|
||||
elsif Setting.ds_algorithm == 2
|
||||
self.ds_digest = Digest::SHA256.hexdigest(bin).upcase
|
||||
end
|
||||
end
|
||||
|
|
|
@ -85,9 +85,7 @@ class Domain < ActiveRecord::Base
|
|||
|
||||
### VALIDATIONS ###
|
||||
def validate_nameservers_count
|
||||
sg = SettingGroup.domain_validation
|
||||
min, max = sg.setting(:ns_min_count).value.to_i, sg.setting(:ns_max_count).value.to_i
|
||||
|
||||
min, max = Setting.ns_min_count, Setting.ns_max_count
|
||||
return if nameservers.reject(&:marked_for_destruction?).length.between?(min, max)
|
||||
errors.add(:nameservers, :out_of_range, { min: min, max: max })
|
||||
end
|
||||
|
@ -97,8 +95,7 @@ class Domain < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def validate_dnskeys_count
|
||||
sg = SettingGroup.domain_validation
|
||||
min, max = sg.setting(:dnskeys_min_count).value.to_i, sg.setting(:dnskeys_max_count).value.to_i
|
||||
min, max = Setting.dnskeys_min_count, Setting.dnskeys_max_count
|
||||
return if dnskeys.reject(&:marked_for_destruction?).length.between?(min, max)
|
||||
errors.add(:dnskeys, :out_of_range, { min: min, max: max })
|
||||
end
|
||||
|
|
|
@ -14,7 +14,7 @@ class DomainTransfer < ActiveRecord::Base
|
|||
before_create :set_wait_until
|
||||
|
||||
def set_wait_until
|
||||
wait_time = SettingGroup.domain_general.setting(:transfer_wait_time).value.to_i
|
||||
wait_time = Setting.transfer_wait_time
|
||||
return if wait_time == 0
|
||||
self.wait_until = transfer_requested_at + wait_time.hours
|
||||
end
|
||||
|
|
|
@ -6,8 +6,6 @@ class Epp::EppDomain < Domain
|
|||
validate :validate_admin_contacts_count
|
||||
|
||||
def epp_code_map # rubocop:disable Metrics/MethodLength
|
||||
domain_validation_sg = SettingGroup.domain_validation
|
||||
|
||||
{
|
||||
'2002' => [
|
||||
[:base, :domain_already_belongs_to_the_querying_registrar]
|
||||
|
@ -29,15 +27,15 @@ class Epp::EppDomain < Domain
|
|||
'2004' => [ # Parameter value range error
|
||||
[:nameservers, :out_of_range,
|
||||
{
|
||||
min: domain_validation_sg.setting(:ns_min_count).value,
|
||||
max: domain_validation_sg.setting(:ns_max_count).value
|
||||
min: Setting.ns_min_count,
|
||||
max: Setting.ns_max_count
|
||||
}
|
||||
],
|
||||
[:period, :out_of_range, { value: { obj: 'period', val: period } }],
|
||||
[:dnskeys, :out_of_range,
|
||||
{
|
||||
min: domain_validation_sg.setting(Setting::DNSKEYS_MIN_COUNT).value,
|
||||
max: domain_validation_sg.setting(Setting::DNSKEYS_MAX_COUNT).value
|
||||
min: Setting.dnskeys_min_count,
|
||||
max: Setting.dnskeys_max_count
|
||||
}
|
||||
]
|
||||
],
|
||||
|
@ -189,8 +187,7 @@ class Epp::EppDomain < Domain
|
|||
end
|
||||
|
||||
def attach_dnskeys(dnssec_data)
|
||||
sg = SettingGroup.dnskeys
|
||||
return false unless validate_dnssec_data(dnssec_data, sg)
|
||||
return false unless validate_dnssec_data(dnssec_data)
|
||||
|
||||
dnssec_data[:ds_data].each do |ds_data|
|
||||
dnskeys.build(ds_data)
|
||||
|
@ -200,41 +197,35 @@ class Epp::EppDomain < Domain
|
|||
dnskeys.build({
|
||||
ds_key_tag: SecureRandom.hex(5),
|
||||
ds_alg: 3,
|
||||
ds_digest_type: sg.setting(Setting::DS_ALGORITHM).value
|
||||
ds_digest_type: Setting.ds_algorithm
|
||||
}.merge(x))
|
||||
end
|
||||
end
|
||||
|
||||
def validate_dnssec_data(dnssec_data, sg)
|
||||
ds_data_allowed?(dnssec_data, sg)
|
||||
ds_data_with_keys_allowed?(dnssec_data, sg)
|
||||
key_data_allowed?(dnssec_data, sg)
|
||||
def validate_dnssec_data(dnssec_data)
|
||||
ds_data_allowed?(dnssec_data)
|
||||
ds_data_with_keys_allowed?(dnssec_data)
|
||||
key_data_allowed?(dnssec_data)
|
||||
|
||||
errors.empty?
|
||||
end
|
||||
|
||||
def ds_data_allowed?(dnssec_data, sg)
|
||||
ds_data_allowed = sg.setting(Setting::ALLOW_DS_DATA).value == '0' ? false : true
|
||||
|
||||
return if (dnssec_data[:ds_data].any? && ds_data_allowed) || dnssec_data[:ds_data].empty?
|
||||
def ds_data_allowed?(dnssec_data)
|
||||
return if (dnssec_data[:ds_data].any? && Setting.ds_data_allowed) || dnssec_data[:ds_data].empty?
|
||||
errors.add(:base, :ds_data_not_allowed)
|
||||
end
|
||||
|
||||
def ds_data_with_keys_allowed?(dnssec_data, sg)
|
||||
ds_data_with_keys_allowed = sg.setting(Setting::ALLOW_DS_DATA_WITH_KEYS).value == '0' ? false : true
|
||||
|
||||
def ds_data_with_keys_allowed?(dnssec_data)
|
||||
dnssec_data[:ds_data].each do |ds_data|
|
||||
if key_data?(ds_data) && !ds_data_with_keys_allowed
|
||||
if key_data?(ds_data) && !Setting.ds_data_with_key_allowed
|
||||
errors.add(:base, :ds_data_with_key_not_allowed)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def key_data_allowed?(dnssec_data, sg)
|
||||
key_data_allowed = sg.setting(Setting::ALLOW_KEY_DATA).value == '0' ? false : true
|
||||
|
||||
return if (dnssec_data[:key_data].any? && key_data_allowed) || dnssec_data[:key_data].empty?
|
||||
def key_data_allowed?(dnssec_data)
|
||||
return if (dnssec_data[:key_data].any? && Setting.key_data_allowed) || dnssec_data[:key_data].empty?
|
||||
errors.add(:base, :key_data_not_allowed)
|
||||
end
|
||||
|
||||
|
@ -244,9 +235,7 @@ class Epp::EppDomain < Domain
|
|||
end
|
||||
|
||||
def detach_dnskeys(dnssec_data)
|
||||
sg = SettingGroup.dnskeys
|
||||
|
||||
return false unless validate_dnssec_data(dnssec_data, sg)
|
||||
return false unless validate_dnssec_data(dnssec_data)
|
||||
|
||||
to_delete = []
|
||||
dnssec_data[:ds_data].each do |x|
|
||||
|
@ -305,9 +294,7 @@ class Epp::EppDomain < Domain
|
|||
|
||||
return true if pt
|
||||
|
||||
wait_time = SettingGroup.domain_general.setting(:transfer_wait_time).value.to_i
|
||||
|
||||
if wait_time > 0
|
||||
if Setting.transfer_wait_time > 0
|
||||
domain_transfers.create(
|
||||
status: DomainTransfer::PENDING,
|
||||
transfer_requested_at: Time.zone.now,
|
||||
|
|
|
@ -1,14 +1,2 @@
|
|||
class Setting < ActiveRecord::Base
|
||||
belongs_to :setting_group
|
||||
has_many :domain_statuses
|
||||
has_many :domains, through: :domain_statuses
|
||||
validates :code, uniqueness: { scope: :setting_group_id }
|
||||
|
||||
# dnskeys
|
||||
DS_ALGORITHM = 'ds_algorithm'
|
||||
ALLOW_DS_DATA = 'allow_ds_data'
|
||||
ALLOW_DS_DATA_WITH_KEYS = 'allow_ds_data_with_keys'
|
||||
ALLOW_KEY_DATA = 'allow_key_data'
|
||||
DNSKEYS_MAX_COUNT = 'dnskeys_max_count'
|
||||
DNSKEYS_MIN_COUNT = 'dnskeys_min_count'
|
||||
class Setting < RailsSettings::CachedSettings
|
||||
end
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
class SettingGroup < ActiveRecord::Base
|
||||
has_many :settings
|
||||
|
||||
accepts_nested_attributes_for :settings
|
||||
|
||||
validates :code, uniqueness: true
|
||||
|
||||
def setting(key)
|
||||
settings.find_by(code: key.to_s)
|
||||
end
|
||||
|
||||
class << self
|
||||
def domain_validation
|
||||
find_by(code: 'domain_validation')
|
||||
end
|
||||
|
||||
def domain_general
|
||||
find_by(code: 'domain_general')
|
||||
end
|
||||
|
||||
def dnskeys
|
||||
find_by(code: 'dnskeys')
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue