diff --git a/app/controllers/concerns/epp/common.rb b/app/controllers/concerns/epp/common.rb index 7c8abba3f..af2f10fc3 100644 --- a/app/controllers/concerns/epp/common.rb +++ b/app/controllers/concerns/epp/common.rb @@ -32,9 +32,11 @@ module Epp::Common @current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id] end - def handle_errors(obj) - obj.construct_epp_errors - @errors = obj.errors[:epp_errors] + def handle_errors(obj=nil) + if obj + obj.construct_epp_errors + @errors = obj.errors[:epp_errors] + end render '/epp/error' end @@ -52,15 +54,9 @@ module Epp::Common end def validate_request - type = OBJECT_TYPES[params_hash['epp']['xmlns:ns2']] - return unless type - - 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 + validation_method = "validate_#{OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]}_#{params[:command]}_request" + if respond_to?(validation_method, true) + handle_errors and return unless send(validation_method) end end end diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index 34a473f11..f682a7c1b 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -32,24 +32,30 @@ module Epp::DomainsHelper end def renew_domain - ph = params_hash['epp']['command']['renew']['renew'] + @domain = 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]}} - render '/epp/error' and return - end + handle_errors(@domain) and return unless @domain + handle_errors(@domain) and return unless @domain.renew(@ph[:curExpDate], @ph[:period]) - if @domain.renew(ph[:curExpDate], ph[:period]) - render '/epp/domains/renew' - else - handle_errors(@domain) - end + render '/epp/domains/renew' end ### HELPER METHODS ### 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) { name: ph[:name], diff --git a/app/models/domain.rb b/app/models/domain.rb index 4ff003a14..fd4ddaa16 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -12,7 +12,8 @@ class Domain < ActiveRecord::Base } EPP_ATTR_MAP = { - owner_contact: 'registrant' + owner_contact: 'registrant', + name_dirty: 'name' } belongs_to :registrar @@ -33,6 +34,7 @@ class Domain < ActiveRecord::Base validates :name_dirty, domain_name: true, uniqueness: true validates :period, numericality: { only_integer: true, greater_than: 0, less_than: 100 } validates :name, :owner_contact, presence: true + validate :validate_period validates_associated :nameservers def name=(value) @@ -118,14 +120,40 @@ class Domain < ActiveRecord::Base end def renew(cur_exp_date, period, unit='y') - if cur_exp_date.to_date == valid_to - self.valid_to = self.valid_to + period.to_i.years - self.period = period - save + # TODO Check how much time before domain exp date can it be renewed + 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_unit = unit + save + 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 - errors[:base] << {msg: I18n.t('errors.messages.epp_exp_dates_do_not_match'), obj: 'curExpDate', val: cur_exp_date} - false + 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 class << self diff --git a/db/migrate/20140808132327_add_period_unit_to_domain.rb b/db/migrate/20140808132327_add_period_unit_to_domain.rb new file mode 100644 index 000000000..e02ac2e04 --- /dev/null +++ b/db/migrate/20140808132327_add_period_unit_to_domain.rb @@ -0,0 +1,5 @@ +class AddPeriodUnitToDomain < ActiveRecord::Migration + def change + add_column :domains, :period_unit, :char + end +end diff --git a/db/schema.rb b/db/schema.rb index 6a44334c7..8e4fa739d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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 enable_extension "plpgsql" @@ -75,6 +75,7 @@ ActiveRecord::Schema.define(version: 20140804095654) do t.string "name_dirty" t.string "name_puny" t.integer "period" + t.string "period_unit", limit: 1 end create_table "domains_nameservers", force: true do |t| diff --git a/spec/fabricators/domain_fabricator.rb b/spec/fabricators/domain_fabricator.rb index 5a3da4f25..24113039d 100644 --- a/spec/fabricators/domain_fabricator.rb +++ b/spec/fabricators/domain_fabricator.rb @@ -2,5 +2,6 @@ Fabricator(:domain) do name { "#{Faker::Internet.domain_word}.ee" } valid_to Date.new(2014, 8, 7) period 1 + period_unit 'y' owner_contact(fabricator: :contact) end diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index 0338c46aa..2c0ffa78e 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -52,8 +52,8 @@ describe Domain do it 'validates period' do 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: 99).valid?).to be true + expect(Fabricate.build(:domain, period: 4).valid?).to be false + expect(Fabricate.build(:domain, period: 3).valid?).to be true end end