From c6073c7e87f080b535edd5049776a4499423e380 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 15 Sep 2015 14:00:19 +0300 Subject: [PATCH] Restructure white ip interfaces #2713 --- app/controllers/admin/white_ips_controller.rb | 2 +- app/models/white_ip.rb | 16 +++++++--------- app/views/admin/registrars/show.haml | 6 +++--- app/views/admin/white_ips/_form.haml | 12 +++++++----- app/views/admin/white_ips/show.haml | 4 ++-- config/locales/en.yml | 1 + ...94707_add_multiple_interfaces_for_white_ip.rb | 6 ++++++ db/schema-read-only.rb | 4 ++-- db/structure.sql | 4 +++- 9 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 db/migrate/20150915094707_add_multiple_interfaces_for_white_ip.rb diff --git a/app/controllers/admin/white_ips_controller.rb b/app/controllers/admin/white_ips_controller.rb index 7c0ecb184..059094614 100644 --- a/app/controllers/admin/white_ips_controller.rb +++ b/app/controllers/admin/white_ips_controller.rb @@ -51,6 +51,6 @@ class Admin::WhiteIpsController < AdminController end def white_ip_params - params.require(:white_ip).permit(:ipv4, :ipv6, :interface, :registrar_id) + params.require(:white_ip).permit(:ipv4, :ipv6, :registrar_id, { interfaces: [] }) end end diff --git a/app/models/white_ip.rb b/app/models/white_ip.rb index 7a35a33f6..918315004 100644 --- a/app/models/white_ip.rb +++ b/app/models/white_ip.rb @@ -17,19 +17,17 @@ class WhiteIp < ActiveRecord::Base REGISTRAR = 'registrar' INTERFACES = [API, REGISTRAR] - scope :api, -> { where(interface: API) } - scope :registrar, -> { where(interface: REGISTRAR) } + scope :api, -> { where("interfaces @> ?::varchar[]", "{#{API}}") } + scope :registrar, -> { where("interfaces @> ?::varchar[]", "{#{REGISTRAR}}") } + + def interfaces=(interfaces) + super(interfaces.reject(&:blank?)) + end class << self def registrar_ip_white?(ip) return true unless Setting.registrar_ip_whitelist_enabled - - at = WhiteIp.arel_table - WhiteIp.where( - at[:interface].eq(REGISTRAR).and( - at[:ipv4].eq(ip) - ) - ).any? + WhiteIp.where(ipv4: ip).registrar.any? end end end diff --git a/app/views/admin/registrars/show.haml b/app/views/admin/registrars/show.haml index d41a0f9fd..c44523d87 100644 --- a/app/views/admin/registrars/show.haml +++ b/app/views/admin/registrars/show.haml @@ -92,10 +92,10 @@ %tr %th{class: 'col-xs-4'}= t(:ipv4) %th{class: 'col-xs-6'}= t(:ipv6) - %th{class: 'col-xs-2'}= t(:interface) + %th{class: 'col-xs-2'}= t(:interfaces) %tbody - - @registrar.white_ips.order(:interface).each do |x| + - @registrar.white_ips.each do |x| %tr %td= link_to(x.ipv4, [:admin, @registrar, x]) %td= link_to(x.ipv6, [:admin, @registrar, x]) - %td= x.interface.upcase + %td= x.interfaces.join(', ').upcase diff --git a/app/views/admin/white_ips/_form.haml b/app/views/admin/white_ips/_form.haml index 253501e24..7a0371697 100644 --- a/app/views/admin/white_ips/_form.haml +++ b/app/views/admin/white_ips/_form.haml @@ -19,11 +19,13 @@ = f.label :ipv6 .col-md-7 = f.text_field(:ipv6, class: 'form-control', ipv6: true, autocomplete: 'off') - .form-group - .col-md-4.control-label - = f.label :interface - .col-md-7 - = f.select :interface, WhiteIp::INTERFACES.map {|x| [x.upcase, x]}, {}, class: 'form-control selectize' + - WhiteIp::INTERFACES.each do |x| + .form-group + .col-md-4.control-label + = f.label x + .col-md-7 + = f.check_box :interfaces, { multiple: true }, x, nil + = hidden_field_tag "white_ip[interfaces][]", nil %hr .row .col-md-8.text-right diff --git a/app/views/admin/white_ips/show.haml b/app/views/admin/white_ips/show.haml index da1da9616..0cfec654c 100644 --- a/app/views/admin/white_ips/show.haml +++ b/app/views/admin/white_ips/show.haml @@ -20,5 +20,5 @@ %dt= t(:ipv6) %dd= @white_ip.ipv6 - %dt= t(:interface) - %dd= @white_ip.interface.upcase + %dt= t(:interfaces) + %dd= @white_ip.interfaces.join(', ').upcase diff --git a/config/locales/en.yml b/config/locales/en.yml index 675226716..40e9ce62b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -921,3 +921,4 @@ en: contact_is_not_valid: 'Contact %{value} is not valid, please fix the invalid contact' force_delete_subject: 'Kustutusmenetluse teade' welcome_to_eis_registrar_portal: 'Welcome to EIS Registrar portal' + interfaces: 'Interfaces' diff --git a/db/migrate/20150915094707_add_multiple_interfaces_for_white_ip.rb b/db/migrate/20150915094707_add_multiple_interfaces_for_white_ip.rb new file mode 100644 index 000000000..59840e474 --- /dev/null +++ b/db/migrate/20150915094707_add_multiple_interfaces_for_white_ip.rb @@ -0,0 +1,6 @@ +class AddMultipleInterfacesForWhiteIp < ActiveRecord::Migration + def change + change_column :white_ips, :interface, "varchar[] USING (string_to_array(interface, ','))" + rename_column :white_ips, :interface, :interfaces + end +end diff --git a/db/schema-read-only.rb b/db/schema-read-only.rb index f7de8c32d..1903fb52f 100644 --- a/db/schema-read-only.rb +++ b/db/schema-read-only.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150910113839) do +ActiveRecord::Schema.define(version: 20150915094707) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1064,7 +1064,7 @@ ActiveRecord::Schema.define(version: 20150910113839) do t.integer "registrar_id" t.string "ipv4" t.string "ipv6" - t.string "interface" + t.string "interfaces", array: true t.datetime "created_at" t.datetime "updated_at" t.string "creator_str" diff --git a/db/structure.sql b/db/structure.sql index c517c6576..01c6e60cf 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2717,7 +2717,7 @@ CREATE TABLE white_ips ( registrar_id integer, ipv4 character varying, ipv6 character varying, - interface character varying, + interfaces character varying[], created_at timestamp without time zone, updated_at timestamp without time zone, creator_str character varying, @@ -4934,3 +4934,5 @@ INSERT INTO schema_migrations (version) VALUES ('20150827151906'); INSERT INTO schema_migrations (version) VALUES ('20150910113839'); +INSERT INTO schema_migrations (version) VALUES ('20150915094707'); +