From 1f2ba55acbf35709fa51224becedb2c81a24f37c Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 28 Apr 2015 18:36:59 +0300 Subject: [PATCH] Nameserver hostname replacing --- .../registrar/nameservers_controller.rb | 38 +++++++++++++ app/models/ability.rb | 1 + app/models/nameserver.rb | 25 ++++++++ app/models/registrar.rb | 1 + app/views/registrar/nameservers/index.haml | 57 +++++++++++++++++++ config/locales/en.yml | 7 +++ config/routes.rb | 6 ++ 7 files changed, 135 insertions(+) create mode 100644 app/controllers/registrar/nameservers_controller.rb create mode 100644 app/views/registrar/nameservers/index.haml diff --git a/app/controllers/registrar/nameservers_controller.rb b/app/controllers/registrar/nameservers_controller.rb new file mode 100644 index 000000000..05e47e1dc --- /dev/null +++ b/app/controllers/registrar/nameservers_controller.rb @@ -0,0 +1,38 @@ +class Registrar::NameserversController < RegistrarController + load_and_authorize_resource + + def index + nameservers = current_user.registrar.nameservers + @q = nameservers.search(params[:q]) + @q.sorts = 'id desc' if @q.sorts.empty? + @nameservers = @q.result.page(params[:page]) + + return unless can_replace_hostnames? + + res = Nameserver.replace_hostname_ends( + current_user.registrar.domains.includes( + :registrant, :nameservers, :admin_domain_contacts, :tech_domain_contacts, :domain_statuses, + :versions, :admin_contacts, :tech_contacts, :whois_record, :dnskeys + ), + params[:q][:hostname_end], + params[:hostname_end_replacement] + ) + + if res + flash.now[:notice] = t('all_hostnames_replaced') + else + flash.now[:warning] = t('hostnames_partially_replaced') + end + end + + private + + def can_replace_hostnames? + if params[:replace] && params[:q] + flash.now[:alert] = t('hostname_end_replacement_is_required') unless params[:hostname_end_replacement].present? + flash.now[:alert] = t('hostname_end_is_required') unless params[:q][:hostname_end].present? + return true if flash[:alert].blank? + end + false + end +end diff --git a/app/models/ability.rb b/app/models/ability.rb index dd73b4c4d..61d28c505 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -48,6 +48,7 @@ class Ability def registrar can :manage, Invoice can :read, AccountActivity + can :manage, Nameserver can :view, :registrar_dashboard can :delete, :registrar_poll can :manage, :registrar_xml_console diff --git a/app/models/nameserver.rb b/app/models/nameserver.rb index 6b34947cf..20b554ea2 100644 --- a/app/models/nameserver.rb +++ b/app/models/nameserver.rb @@ -5,6 +5,8 @@ class Nameserver < ActiveRecord::Base # belongs_to :registrar belongs_to :domain + # scope :owned_by_registrar, -> (registrar) { joins(:domain).where('domains.registrar_id = ?', registrar.id) } + # rubocop: disable Metrics/LineLength validates :hostname, format: { with: /\A(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/ } validates :ipv4, format: { with: /\A(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/, allow_blank: true } @@ -40,4 +42,27 @@ class Nameserver < ActiveRecord::Base def to_s hostname end + + class << self + def replace_hostname_ends(domains, old_end, new_end) + res = true + domains.each do |d| + nameservers = d.nameservers.where('hostname LIKE ?', "%#{old_end}") + next unless nameservers + + ns_attrs = { nameservers_attributes: [] } + + nameservers.each do |ns| + hn = ns.hostname.chomp(old_end) + ns_attrs[:nameservers_attributes] << { + id: ns.id, + hostname: "#{hn}#{new_end}" + } + end + + res = false unless d.update(ns_attrs) + end + res + end + end end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index d52858e67..1723ebdd2 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -7,6 +7,7 @@ class Registrar < ActiveRecord::Base has_many :messages has_many :invoices, foreign_key: 'buyer_id' has_many :accounts + has_many :nameservers, through: :domains belongs_to :country_deprecated, foreign_key: :country_id diff --git a/app/views/registrar/nameservers/index.haml b/app/views/registrar/nameservers/index.haml new file mode 100644 index 000000000..8fdb29592 --- /dev/null +++ b/app/views/registrar/nameservers/index.haml @@ -0,0 +1,57 @@ +/ - content_for :actions do +/ = link_to(t(:add_deposit), new_registrar_deposit_path, class: 'btn btn-primary') +/ = link_to(t(:account_activity), registrar_account_activities_path, class: 'btn btn-default') += render 'shared/title', name: t(:nameservers) + +.row + .col-md-12 + = search_form_for @q, url: [:registrar, :nameservers], html: { style: 'margin-bottom: 0;' } do |f| + .row + .col-md-3 + .form-group + = f.label t(:hostname_end) + = f.search_field :hostname_end, class: 'form-control', placeholder: t(:hostname_end), autocomplete: 'off' + .col-md-3 + .form-group + = label_tag t(:hostname_end_replacement) + = text_field_tag :hostname_end_replacement, params[:hostname_end_replacement], class: 'form-control', placeholder: t(:hostname_end_replacement), autocomplete: 'off' + .col-md-4{style: 'padding-top: 25px;'} + %button.btn.btn-default +   + %span.glyphicon.glyphicon-search +   + %button.btn.btn-default{name: 'replace'} + = t(:replace) + %button.btn.btn-default.js-reset-form + = t(:clear_fields) +%hr +.row + .col-md-12 + .table-responsive + %table.table.table-hover.table-condensed + %thead + %tr + %th{class: 'col-xs-4'}= t(:hostname) + %th{class: 'col-xs-4'}= t(:ipv4) + %th{class: 'col-xs-4'}= t(:ipv6) + %tbody + - @nameservers.each do |x| + %tr + - if params[:q] && params[:q][:hostname_end] + - hn = x.hostname.chomp(params[:q][:hostname_end]) + %td + = precede hn do + %strong= params[:q][:hostname_end] + - else + %td= x.hostname + %td= x.ipv4 + %td= x.ipv4 +.row + .col-md-12 + / = paginate @nameservers + +:coffee + $(".js-reset-form").on "click", (e) -> + e.preventDefault(); + window.location = "#{registrar_nameservers_path}" + diff --git a/config/locales/en.yml b/config/locales/en.yml index a0e2f417e..531c4bfa0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -746,3 +746,10 @@ en: due_date_before: 'Due date before' minimum_total: 'Minimum total' maximum_total: 'Maximum total' + hostname_end: 'Hostname end' + hostname_end_replacement: 'Hostname end replacement' + hostname_end_is_required: 'Hostname end is required' + hostname_end_replacement_is_required: 'Hostname end replacement is required' + hostnames_replaced: 'Hostnames replaced' + all_hostnames_replaced: 'All hostnames replaced' + hostnames_partially_replaced: 'Hostnames partially replaced' diff --git a/config/routes.rb b/config/routes.rb index 6609bf9a1..7b1e3e318 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -55,6 +55,12 @@ Rails.application.routes.draw do end end + resources :nameservers do + collection do + get 'replace_hostnames' + end + end + resources :contacts do member do get 'delete'