mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 14:44:47 +02:00
Merge pull request #1817 from internetee/1764-registrar-bulk-admin-change
Domain admin contacts bulk change feature
This commit is contained in:
commit
a46b04856f
22 changed files with 524 additions and 136 deletions
18
app/controllers/registrar/admin_contacts_controller.rb
Normal file
18
app/controllers/registrar/admin_contacts_controller.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
class Registrar
|
||||
class AdminContactsController < BulkChangeController
|
||||
BASE_URL = URI.parse("#{ENV['repp_url']}domains/admin_contacts").freeze
|
||||
ACTIVE_TAB = :admin_contact
|
||||
|
||||
def update
|
||||
authorize! :manage, :repp
|
||||
uri = BASE_URL
|
||||
request = form_request(uri)
|
||||
response = do_request(request, uri)
|
||||
start_notice = t('.replaced')
|
||||
|
||||
process_response(response: response,
|
||||
start_notice: start_notice,
|
||||
active_tab: ACTIVE_TAB)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -26,6 +26,84 @@ class Registrar
|
|||
|
||||
private
|
||||
|
||||
def form_request(uri)
|
||||
request = Net::HTTP::Patch.new(uri)
|
||||
request.set_form_data(current_contact_id: params[:current_contact_id],
|
||||
new_contact_id: params[:new_contact_id])
|
||||
request.basic_auth(current_registrar_user.username,
|
||||
current_registrar_user.plain_text_password)
|
||||
request
|
||||
end
|
||||
|
||||
def process_response(response:, start_notice: '', active_tab:)
|
||||
parsed_response = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
if response.code == '200'
|
||||
notices = success_notices(parsed_response, start_notice)
|
||||
|
||||
flash[:notice] = notices.join(', ')
|
||||
redirect_to registrar_domains_url
|
||||
else
|
||||
@error = response.code == '404' ? 'Contact(s) not found' : parsed_response[:message]
|
||||
render file: 'registrar/bulk_change/new', locals: { active_tab: active_tab }
|
||||
end
|
||||
end
|
||||
|
||||
def success_notices(parsed_response, start_notice)
|
||||
notices = [start_notice]
|
||||
|
||||
notices << "#{t('.affected_domains')}: " \
|
||||
"#{parsed_response[:data][:affected_domains].join(', ')}"
|
||||
|
||||
if parsed_response[:data][:skipped_domains]
|
||||
notices << "#{t('.skipped_domains')}: " \
|
||||
"#{parsed_response[:data][:skipped_domains].join(', ')}"
|
||||
end
|
||||
notices
|
||||
end
|
||||
|
||||
def do_request(request, uri)
|
||||
response = if Rails.env.test?
|
||||
do_test_request(request, uri)
|
||||
elsif Rails.env.development?
|
||||
do_dev_request(request, uri)
|
||||
else
|
||||
do_live_request(request, uri)
|
||||
end
|
||||
response
|
||||
end
|
||||
|
||||
def do_live_request(request, uri)
|
||||
client_cert = File.read(ENV['cert_path'])
|
||||
client_key = File.read(ENV['key_path'])
|
||||
Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
cert: OpenSSL::X509::Certificate.new(client_cert),
|
||||
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
end
|
||||
|
||||
def do_dev_request(request, uri)
|
||||
client_cert = File.read(ENV['cert_path'])
|
||||
client_key = File.read(ENV['key_path'])
|
||||
Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
verify_mode: OpenSSL::SSL::VERIFY_NONE,
|
||||
cert: OpenSSL::X509::Certificate.new(client_cert),
|
||||
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
end
|
||||
|
||||
def do_test_request(request, uri)
|
||||
Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
end
|
||||
|
||||
def ready_to_renew?
|
||||
domain_ids_for_bulk_renew.present? && params[:renew].present?
|
||||
end
|
||||
|
|
|
@ -25,32 +25,7 @@ class Registrar
|
|||
current_registrar_user.plain_text_password)
|
||||
|
||||
|
||||
if Rails.env.test?
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
elsif Rails.env.development?
|
||||
client_cert = File.read(ENV['cert_path'])
|
||||
client_key = File.read(ENV['key_path'])
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
verify_mode: OpenSSL::SSL::VERIFY_NONE,
|
||||
cert: OpenSSL::X509::Certificate.new(client_cert),
|
||||
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
else
|
||||
client_cert = File.read(ENV['cert_path'])
|
||||
client_key = File.read(ENV['key_path'])
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
cert: OpenSSL::X509::Certificate.new(client_cert),
|
||||
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
end
|
||||
response = do_request(request, uri)
|
||||
|
||||
parsed_response = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
|
|
|
@ -18,32 +18,7 @@ class Registrar
|
|||
request.basic_auth(current_registrar_user.username,
|
||||
current_registrar_user.plain_text_password)
|
||||
|
||||
if Rails.env.test?
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
elsif Rails.env.development?
|
||||
client_cert = File.read(ENV['cert_path'])
|
||||
client_key = File.read(ENV['key_path'])
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
verify_mode: OpenSSL::SSL::VERIFY_NONE,
|
||||
cert: OpenSSL::X509::Certificate.new(client_cert),
|
||||
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
else
|
||||
client_cert = File.read(ENV['cert_path'])
|
||||
client_key = File.read(ENV['key_path'])
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
cert: OpenSSL::X509::Certificate.new(client_cert),
|
||||
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
end
|
||||
response = do_request(request, uri)
|
||||
|
||||
parsed_response = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
|
|
|
@ -1,62 +1,19 @@
|
|||
class Registrar
|
||||
class TechContactsController < BulkChangeController
|
||||
BASE_URL = URI.parse("#{ENV['repp_url']}domains/contacts").freeze
|
||||
ACTIVE_TAB = :technical_contact
|
||||
|
||||
def update
|
||||
authorize! :manage, :repp
|
||||
|
||||
uri = URI.parse("#{ENV['repp_url']}domains/contacts")
|
||||
uri = BASE_URL
|
||||
request = form_request(uri)
|
||||
response = do_request(request, uri)
|
||||
start_notice = t('.replaced')
|
||||
|
||||
request = Net::HTTP::Patch.new(uri)
|
||||
request.set_form_data(current_contact_id: params[:current_contact_id],
|
||||
new_contact_id: params[:new_contact_id])
|
||||
request.basic_auth(current_registrar_user.username,
|
||||
current_registrar_user.plain_text_password)
|
||||
|
||||
if Rails.env.test?
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
elsif Rails.env.development?
|
||||
client_cert = File.read(ENV['cert_path'])
|
||||
client_key = File.read(ENV['key_path'])
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
verify_mode: OpenSSL::SSL::VERIFY_NONE,
|
||||
cert: OpenSSL::X509::Certificate.new(client_cert),
|
||||
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
else
|
||||
client_cert = File.read(ENV['cert_path'])
|
||||
client_key = File.read(ENV['key_path'])
|
||||
response = Net::HTTP.start(uri.hostname, uri.port,
|
||||
use_ssl: (uri.scheme == 'https'),
|
||||
cert: OpenSSL::X509::Certificate.new(client_cert),
|
||||
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
|
||||
http.request(request)
|
||||
end
|
||||
end
|
||||
|
||||
parsed_response = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
if response.code == '200'
|
||||
notices = [t('.replaced')]
|
||||
|
||||
notices << "#{t('.affected_domains')}: " \
|
||||
"#{parsed_response[:data][:affected_domains].join(', ')}"
|
||||
|
||||
if parsed_response[:data][:skipped_domains]
|
||||
notices << "#{t('.skipped_domains')}: " \
|
||||
"#{parsed_response[:data][:skipped_domains].join(', ')}"
|
||||
end
|
||||
|
||||
flash[:notice] = notices.join(', ')
|
||||
redirect_to registrar_domains_url
|
||||
else
|
||||
@error = response.code == '404' ? 'Contact(s) not found' : parsed_response[:message]
|
||||
render file: 'registrar/bulk_change/new', locals: { active_tab: :technical_contact }
|
||||
end
|
||||
process_response(response: response,
|
||||
start_notice: start_notice,
|
||||
active_tab: ACTIVE_TAB)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
21
app/controllers/repp/v1/domains/admin_contacts_controller.rb
Normal file
21
app/controllers/repp/v1/domains/admin_contacts_controller.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Repp
|
||||
module V1
|
||||
module Domains
|
||||
class AdminContactsController < BaseContactsController
|
||||
def update
|
||||
super
|
||||
|
||||
unless @new_contact.identical_to?(@current_contact)
|
||||
@epp_errors << { code: 2304, msg: 'Admin contacts must be identical' }
|
||||
end
|
||||
|
||||
return handle_errors if @epp_errors.any?
|
||||
|
||||
affected, skipped = AdminDomainContact.replace(@current_contact, @new_contact)
|
||||
@response = { affected_domains: affected, skipped_domains: skipped }
|
||||
render_success(data: @response)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
31
app/controllers/repp/v1/domains/base_contacts_controller.rb
Normal file
31
app/controllers/repp/v1/domains/base_contacts_controller.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
module Repp
|
||||
module V1
|
||||
module Domains
|
||||
class BaseContactsController < BaseController
|
||||
before_action :set_current_contact, only: [:update]
|
||||
before_action :set_new_contact, only: [:update]
|
||||
|
||||
def set_current_contact
|
||||
@current_contact = current_user.registrar.contacts
|
||||
.find_by!(code: contact_params[:current_contact_id])
|
||||
end
|
||||
|
||||
def set_new_contact
|
||||
@new_contact = current_user.registrar.contacts.find_by!(code: params[:new_contact_id])
|
||||
end
|
||||
|
||||
def update
|
||||
@epp_errors ||= []
|
||||
@epp_errors << { code: 2304, msg: 'New contact must be valid' } if @new_contact.invalid?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def contact_params
|
||||
params.require(%i[current_contact_id new_contact_id])
|
||||
params.permit(:current_contact_id, :new_contact_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,23 +1,9 @@
|
|||
module Repp
|
||||
module V1
|
||||
module Domains
|
||||
class ContactsController < BaseController
|
||||
before_action :set_current_contact, only: [:update]
|
||||
before_action :set_new_contact, only: [:update]
|
||||
|
||||
def set_current_contact
|
||||
@current_contact = current_user.registrar.contacts.find_by!(
|
||||
code: contact_params[:current_contact_id]
|
||||
)
|
||||
end
|
||||
|
||||
def set_new_contact
|
||||
@new_contact = current_user.registrar.contacts.find_by!(code: params[:new_contact_id])
|
||||
end
|
||||
|
||||
class ContactsController < BaseContactsController
|
||||
def update
|
||||
@epp_errors ||= []
|
||||
@epp_errors << { code: 2304, msg: 'New contact must be valid' } if @new_contact.invalid?
|
||||
super
|
||||
|
||||
if @new_contact == @current_contact
|
||||
@epp_errors << { code: 2304, msg: 'New contact must be different from current' }
|
||||
|
@ -29,13 +15,6 @@ module Repp
|
|||
@response = { affected_domains: affected, skipped_domains: skipped }
|
||||
render_success(data: @response)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def contact_params
|
||||
params.require(%i[current_contact_id new_contact_id])
|
||||
params.permit(:current_contact_id, :new_contact_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue