From c1f90754d186972079f02ba9c019104b3e349b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Fri, 15 Aug 2014 16:09:05 +0300 Subject: [PATCH 1/3] Added auth info to contact --- app/helpers/epp/contacts_helper.rb | 33 +++++++++---------- ...20140815114000_add_auth_info_to_contact.rb | 5 +++ db/schema.rb | 3 +- spec/epp/contact_spec.rb | 25 +++++++++----- .../requests/contacts/update_with_errors.xml | 3 ++ spec/fabricators/contact_fabricator.rb | 1 + spec/support/epp_contact_xml_builder.rb | 7 ++++ 7 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 db/migrate/20140815114000_add_auth_info_to_contact.rb diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 034814092..89a255dbb 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -1,18 +1,15 @@ module Epp::ContactsHelper def create_contact @contact = Contact.new( contact_and_address_attributes ) - stamp @contact - if @contact.save - render '/epp/contacts/create' - else - handle_errors(@contact) - end + render '/epp/contacts/create' and return if stamp(@contact) && @contact.save + + handle_errors(@contact) end def update_contact code = params_hash['epp']['command']['update']['update'][:id] @contact = Contact.where(code: code).first - if stamp(@contact) && @contact.update_attributes(contact_and_address_attributes(:update)) + if has_rights? && stamp(@contact) && @contact.update_attributes(contact_and_address_attributes(:update)) render 'epp/contacts/update' else epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'), value: { obj: 'id', val: code } } if @contact == [] @@ -22,6 +19,7 @@ module Epp::ContactsHelper def delete_contact #no deleting, implement PaperTrail or something similar. + #TODO check for relation before 'destroying' @contact = find_contact handle_errors(@contact) and return unless @contact @contact.destroy @@ -47,12 +45,10 @@ module Epp::ContactsHelper def validate_contact_create_request @ph = params_hash['epp']['command']['create']['create'] xml_attrs_present?(@ph, [['id'], - ['postalInfo'], + ['authInfo', 'pw'], ['postalInfo', 'name'], - ['postalInfo', 'addr'], ['postalInfo', 'addr', 'city'], - ['postalInfo', 'addr', 'cc'], - ['authInfo']]) + ['postalInfo', 'addr', 'cc']]) end ## UPDATE @@ -89,6 +85,14 @@ module Epp::ContactsHelper contact end + def has_rights? + authInfo = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || [] + id = @ph[:id] + return true if (id && authInfo && find_contact.auth_info == authInfo) + + epp_errors << { code: '2201', msg: t('errors.messages.epp_authorization_error'), value: { obj: 'pw', val: authInfo } } + return false + end def contact_and_address_attributes( type=:create ) case type @@ -105,13 +109,6 @@ module Epp::ContactsHelper contact_hash end - def has_rights - if @contact.created_by.registrar == current_epp_user.registrar - return true - end - return false - end - def ident_type result = params[:frame].slice(/(?<=\ 123456798 faulty + + 2fooBAR + diff --git a/spec/fabricators/contact_fabricator.rb b/spec/fabricators/contact_fabricator.rb index 775d7e510..989bd2620 100644 --- a/spec/fabricators/contact_fabricator.rb +++ b/spec/fabricators/contact_fabricator.rb @@ -5,5 +5,6 @@ Fabricator(:contact) do ident '37605030299' code { "sh#{Faker::Number.number(4)}" } ident_type 'op' + auth_info 'ccds4324pok' address end diff --git a/spec/support/epp_contact_xml_builder.rb b/spec/support/epp_contact_xml_builder.rb index a97f154ad..a5895e1cf 100644 --- a/spec/support/epp_contact_xml_builder.rb +++ b/spec/support/epp_contact_xml_builder.rb @@ -94,6 +94,8 @@ module EppContactXmlBuilder xml_params[:chg][:postalInfo] = postalInfo xml_params[:chg][:postalInfo][:addr] = addr + xml_params[:chg][:authInfo] = xml_params[:chg][:authInfo] || { pw: 'ccds4324pok' } + xml.instruct!(:xml, standalone: 'no') xml.epp('xmlns' => 'urn:ietf:params:xml:ns:epp-1.0') do @@ -122,6 +124,11 @@ module EppContactXmlBuilder end end end + unless xml_params[:chg][:authInfo] == [false] + xml.tag!('contact:authInfo') do + xml.tag!('contact:pw', xml_params[:chg][:authInfo][:pw] ) unless xml_params[:chg][:authInfo][:pw] == false + end + end end end end From 91f373379a4e788cfa76b5106410baf91992b36d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Mon, 18 Aug 2014 10:40:01 +0300 Subject: [PATCH 2/3] Contact#info auth info check --- app/helpers/epp/contacts_helper.rb | 4 +++- spec/epp/contact_spec.rb | 10 ++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 89a255dbb..9e1a6882b 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -33,6 +33,7 @@ module Epp::ContactsHelper end def info_contact + handle_errors and return unless has_rights? @contact = find_contact handle_errors(@contact) and return unless @contact render 'epp/contacts/info' @@ -88,7 +89,8 @@ module Epp::ContactsHelper def has_rights? authInfo = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || [] id = @ph[:id] - return true if (id && authInfo && find_contact.auth_info == authInfo) + + return true if (id && authInfo && !find_contact.nil? && find_contact.auth_info == authInfo) epp_errors << { code: '2201', msg: t('errors.messages.epp_authorization_error'), value: { obj: 'pw', val: authInfo } } return false diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index 5b73ef54b..bfcec577b 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -158,7 +158,7 @@ describe 'EPP Contact', epp: true do expect(response[:results].count).to eq 1 end - it 'returns info about contact' do + it 'returns info about contact availability' do Fabricate(:contact, code: 'check-1234') response = epp_request(contact_check_xml( ids: [{ id: 'check-1234'}, { id: 'check-4321' }] ), :xml) @@ -192,7 +192,7 @@ describe 'EPP Contact', epp: true do end it 'returns info about contact' do - Fabricate(:contact, name: "Johnny Awesome", created_by_id: '1', code: 'info-4444') + Fabricate(:contact, name: "Johnny Awesome", created_by_id: '1', code: 'info-4444', auth_info: '2fooBAR') Fabricate(:address) response = epp_request('contacts/info.xml') @@ -204,10 +204,8 @@ describe 'EPP Contact', epp: true do end - it 'doesn\'t display unassociated object', pending: true do - pending 'until new contact rights systems is implemented' - Fabricate(:contact, name:"Johnny Awesome", created_by_id: '240', code: 'info-4444') - Fabricate(:epp_user, id: 240) + it 'doesn\'t display unassociated object' do + Fabricate(:contact, name:"Johnny Awesome", code: 'info-4444') response = epp_request('contacts/info.xml') expect(response[:result_code]).to eq('2201') From 17f7a609cc09a3c29c2fae6372a8932af404cf3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Mon, 18 Aug 2014 10:58:01 +0300 Subject: [PATCH 3/3] refactor --- app/helpers/epp/contacts_helper.rb | 6 +++--- app/models/contact.rb | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 9e1a6882b..594ecf2b5 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -87,12 +87,12 @@ module Epp::ContactsHelper end def has_rights? - authInfo = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || [] + pw = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || [] id = @ph[:id] - return true if (id && authInfo && !find_contact.nil? && find_contact.auth_info == authInfo) + return true if ( !find_contact.nil? && find_contact.auth_info_matches(pw) ) - epp_errors << { code: '2201', msg: t('errors.messages.epp_authorization_error'), value: { obj: 'pw', val: authInfo } } + epp_errors << { code: '2201', msg: t('errors.messages.epp_authorization_error'), value: { obj: 'pw', val: pw } } return false end diff --git a/app/models/contact.rb b/app/models/contact.rb index 18fb5bf18..92a65f384 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -66,6 +66,11 @@ class Contact < ActiveRecord::Base updated_by ? updated_by.username : nil end + def auth_info_matches pw + return true if auth_info == pw + return false + end + class << self def extract_attributes ph, type=:create