mirror of
https://github.com/internetee/registry.git
synced 2025-07-03 09:43:36 +02:00
Domain renew refactor, extract XSD, new API for request validation
This commit is contained in:
parent
01cfddee95
commit
8baa2e12c9
7 changed files with 70 additions and 33 deletions
|
@ -32,9 +32,11 @@ module Epp::Common
|
||||||
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
|
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_errors(obj)
|
def handle_errors(obj=nil)
|
||||||
|
if obj
|
||||||
obj.construct_epp_errors
|
obj.construct_epp_errors
|
||||||
@errors = obj.errors[:epp_errors]
|
@errors = obj.errors[:epp_errors]
|
||||||
|
end
|
||||||
render '/epp/error'
|
render '/epp/error'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -52,15 +54,9 @@ module Epp::Common
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_request
|
def validate_request
|
||||||
type = OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]
|
validation_method = "validate_#{OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]}_#{params[:command]}_request"
|
||||||
return unless type
|
if respond_to?(validation_method, true)
|
||||||
|
handle_errors and return unless send(validation_method)
|
||||||
xsd = Nokogiri::XML::Schema(File.read("doc/schemas/#{type}-1.0.xsd"))
|
|
||||||
doc = Nokogiri::XML(params[:frame])
|
|
||||||
ext_values = xsd.validate(doc)
|
|
||||||
if ext_values.any?
|
|
||||||
epp_errors << {code: '2001', msg: 'Command syntax error', ext_values: ext_values}
|
|
||||||
render '/epp/error' and return
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,24 +32,30 @@ module Epp::DomainsHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def renew_domain
|
def renew_domain
|
||||||
ph = params_hash['epp']['command']['renew']['renew']
|
@domain = find_domain
|
||||||
|
|
||||||
@domain = Domain.find_by(name: ph[:name])
|
handle_errors(@domain) and return unless @domain
|
||||||
unless @domain
|
handle_errors(@domain) and return unless @domain.renew(@ph[:curExpDate], @ph[:period])
|
||||||
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_domain_not_found'), value: {obj: 'name', val: ph[:name]}}
|
|
||||||
render '/epp/error' and return
|
|
||||||
end
|
|
||||||
|
|
||||||
if @domain.renew(ph[:curExpDate], ph[:period])
|
|
||||||
render '/epp/domains/renew'
|
render '/epp/domains/renew'
|
||||||
else
|
|
||||||
handle_errors(@domain)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
### HELPER METHODS ###
|
### HELPER METHODS ###
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def validate_domain_renew_request
|
||||||
|
@ph = params_hash['epp']['command']['renew']['renew']
|
||||||
|
xml_attrs_present?(@ph, [['name'], ['curExpDate'], ['period']])
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_domain
|
||||||
|
@domain = Domain.find_by(name: @ph[:name])
|
||||||
|
unless @domain
|
||||||
|
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_domain_not_found'), value: {obj: 'name', val: @ph[:name]}}
|
||||||
|
end
|
||||||
|
@domain
|
||||||
|
end
|
||||||
|
|
||||||
def domain_create_params(ph)
|
def domain_create_params(ph)
|
||||||
{
|
{
|
||||||
name: ph[:name],
|
name: ph[:name],
|
||||||
|
|
|
@ -12,7 +12,8 @@ class Domain < ActiveRecord::Base
|
||||||
}
|
}
|
||||||
|
|
||||||
EPP_ATTR_MAP = {
|
EPP_ATTR_MAP = {
|
||||||
owner_contact: 'registrant'
|
owner_contact: 'registrant',
|
||||||
|
name_dirty: 'name'
|
||||||
}
|
}
|
||||||
|
|
||||||
belongs_to :registrar
|
belongs_to :registrar
|
||||||
|
@ -33,6 +34,7 @@ class Domain < ActiveRecord::Base
|
||||||
validates :name_dirty, domain_name: true, uniqueness: true
|
validates :name_dirty, domain_name: true, uniqueness: true
|
||||||
validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 }
|
validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 }
|
||||||
validates :name, :owner_contact, presence: true
|
validates :name, :owner_contact, presence: true
|
||||||
|
validate :validate_period
|
||||||
validates_associated :nameservers
|
validates_associated :nameservers
|
||||||
|
|
||||||
def name=(value)
|
def name=(value)
|
||||||
|
@ -118,14 +120,40 @@ class Domain < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def renew(cur_exp_date, period, unit='y')
|
def renew(cur_exp_date, period, unit='y')
|
||||||
if cur_exp_date.to_date == valid_to
|
# TODO Check how much time before domain exp date can it be renewed
|
||||||
self.valid_to = self.valid_to + period.to_i.years
|
validate_exp_dates(cur_exp_date)
|
||||||
|
return false if errors.any?
|
||||||
|
|
||||||
|
p = period.to_i.days if unit == 'd'
|
||||||
|
p = period.to_i.months if unit == 'm'
|
||||||
|
p = period.to_i.years if unit == 'y'
|
||||||
|
|
||||||
|
self.valid_to = self.valid_to + p
|
||||||
self.period = period
|
self.period = period
|
||||||
|
self.period_unit = unit
|
||||||
save
|
save
|
||||||
else
|
|
||||||
errors[:base] << {msg: I18n.t('errors.messages.epp_exp_dates_do_not_match'), obj: 'curExpDate', val: cur_exp_date}
|
|
||||||
false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validate_period
|
||||||
|
return unless period.present?
|
||||||
|
|
||||||
|
if period_unit == 'd'
|
||||||
|
valid_values = ['365', '366', '710', '712', '1065', '1068']
|
||||||
|
elsif period_unit == 'm'
|
||||||
|
valid_values = ['12', '24', '36']
|
||||||
|
else
|
||||||
|
valid_values = ['1', '2', '3']
|
||||||
|
end
|
||||||
|
|
||||||
|
errors.add(:period, :step_error) unless valid_values.include?(period.to_s)
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_exp_dates(cur_exp_date)
|
||||||
|
errors.add(:valid_to, {
|
||||||
|
obj: 'curExpDate',
|
||||||
|
val: cur_exp_date,
|
||||||
|
msg: I18n.t('errors.messages.epp_exp_dates_do_not_match')
|
||||||
|
}) if cur_exp_date.to_date != valid_to
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
5
db/migrate/20140808132327_add_period_unit_to_domain.rb
Normal file
5
db/migrate/20140808132327_add_period_unit_to_domain.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AddPeriodUnitToDomain < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :domains, :period_unit, :char
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20140804095654) do
|
ActiveRecord::Schema.define(version: 20140808132327) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -75,6 +75,7 @@ ActiveRecord::Schema.define(version: 20140804095654) do
|
||||||
t.string "name_dirty"
|
t.string "name_dirty"
|
||||||
t.string "name_puny"
|
t.string "name_puny"
|
||||||
t.integer "period"
|
t.integer "period"
|
||||||
|
t.string "period_unit", limit: 1
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "domains_nameservers", force: true do |t|
|
create_table "domains_nameservers", force: true do |t|
|
||||||
|
|
|
@ -2,5 +2,6 @@ Fabricator(:domain) do
|
||||||
name { "#{Faker::Internet.domain_word}.ee" }
|
name { "#{Faker::Internet.domain_word}.ee" }
|
||||||
valid_to Date.new(2014, 8, 7)
|
valid_to Date.new(2014, 8, 7)
|
||||||
period 1
|
period 1
|
||||||
|
period_unit 'y'
|
||||||
owner_contact(fabricator: :contact)
|
owner_contact(fabricator: :contact)
|
||||||
end
|
end
|
||||||
|
|
|
@ -52,8 +52,8 @@ describe Domain do
|
||||||
|
|
||||||
it 'validates period' do
|
it 'validates period' do
|
||||||
expect(Fabricate.build(:domain, period: 0).valid?).to be false
|
expect(Fabricate.build(:domain, period: 0).valid?).to be false
|
||||||
expect(Fabricate.build(:domain, period: 120).valid?).to be false
|
expect(Fabricate.build(:domain, period: 4).valid?).to be false
|
||||||
expect(Fabricate.build(:domain, period: 99).valid?).to be true
|
expect(Fabricate.build(:domain, period: 3).valid?).to be true
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue