From 8e7bef39d76ae8b444b76930958dc0e26d48c5d3 Mon Sep 17 00:00:00 2001 From: mmeest Date: Mon, 9 Jun 2025 10:31:33 +0300 Subject: [PATCH 1/6] fix issue with idents with special character --- Gemfile.lock | 2 +- app/models/domain.rb | 48 ++++++++++++++++++++++++ app/services/partial_search_formatter.rb | 8 +++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index aaa2d9a94..6244daa11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -412,7 +412,7 @@ GEM request_store (~> 1.4) pdfkit (0.8.7.2) pg (1.5.9) - pg_query (2.1.2) + pg_query (0.9.0) google-protobuf (>= 3.17.1) pghero (3.1.0) activerecord (>= 6) diff --git a/app/models/domain.rb b/app/models/domain.rb index 809d18615..fcab13248 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -282,6 +282,54 @@ class Domain < ApplicationRecord authorizable_ransackable_attributes end + def ransackable_scopes(*) + [:registrant_ident_cont_any, :contacts_ident_cont_any] + end + + def registrant_ident_cont_any(value) + return all if value.blank? + + # Split the value into parts and create search conditions + parts = value.split('-') + conditions = [] + + # Add the original value + conditions << "contacts.ident ILIKE '%#{value}%'" + + # Add conditions for each part + parts.each do |part| + conditions << "contacts.ident ILIKE '%#{part}%'" + end + + # Add condition with hyphens removed + clean_value = value.gsub('-', '') + conditions << "REPLACE(contacts.ident, '-', '') ILIKE '%#{clean_value}%'" + + joins(:registrant).where(conditions.join(' OR ')) + end + + def contacts_ident_cont_any(value) + return all if value.blank? + + # Split the value into parts and create search conditions + parts = value.split('-') + conditions = [] + + # Add the original value + conditions << "contacts.ident ILIKE '%#{value}%'" + + # Add conditions for each part + parts.each do |part| + conditions << "contacts.ident ILIKE '%#{part}%'" + end + + # Add condition with hyphens removed + clean_value = value.gsub('-', '') + conditions << "REPLACE(contacts.ident, '-', '') ILIKE '%#{clean_value}%'" + + joins(:contacts).where(conditions.join(' OR ')) + end + def nameserver_required? Setting.nameserver_required end diff --git a/app/services/partial_search_formatter.rb b/app/services/partial_search_formatter.rb index aa783dc81..48cbf4cc0 100644 --- a/app/services/partial_search_formatter.rb +++ b/app/services/partial_search_formatter.rb @@ -5,7 +5,7 @@ class PartialSearchFormatter search_params.each do |key, value| next unless should_format?(key, value) - search_params[key] = format_value(value) + search_params[key] = format_value(value, key) end search_params @@ -15,9 +15,13 @@ class PartialSearchFormatter key.include?('matches') && value.present? end - def self.format_value(value) + def self.format_value(value, key) if value =~ /\A\*.*\*\z/ value.gsub(/\A\*|\*\z/, '') + elsif key.include?('ident') + # For contact identifiers, return array of values + parts = value.split('-') + parts.map { |part| "%#{part}%" } else "%#{value}%" end From 77cfa5c63523e68ece33bc8ae51fba76f06e3e3b Mon Sep 17 00:00:00 2001 From: mmeest Date: Mon, 9 Jun 2025 11:03:08 +0300 Subject: [PATCH 2/6] Updated pg_query library --- Gemfile.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6244daa11..7e13f0047 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -262,8 +262,8 @@ GEM thor (>= 0.14.0, < 2) globalid (1.2.1) activesupport (>= 6.1) - google-protobuf (3.25.2) - google-protobuf (3.25.2-x86_64-linux) + google-protobuf (3.25.8) + google-protobuf (3.25.8-x86_64-linux) googleapis-common-protos-types (1.3.0) google-protobuf (~> 3.14) grpc (1.60.0) @@ -412,8 +412,8 @@ GEM request_store (~> 1.4) pdfkit (0.8.7.2) pg (1.5.9) - pg_query (0.9.0) - google-protobuf (>= 3.17.1) + pg_query (6.1.0) + google-protobuf (>= 3.25.3) pghero (3.1.0) activerecord (>= 6) pry (0.15.2) From 3994892126b6984cf9e541be52d459541e9beb6d Mon Sep 17 00:00:00 2001 From: mmeest Date: Mon, 9 Jun 2025 14:22:07 +0300 Subject: [PATCH 3/6] added test for domain search for contacts with special chars --- test/integration/admin_area/domain_test.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 test/integration/admin_area/domain_test.rb diff --git a/test/integration/admin_area/domain_test.rb b/test/integration/admin_area/domain_test.rb new file mode 100644 index 000000000..66c1b3d0a --- /dev/null +++ b/test/integration/admin_area/domain_test.rb @@ -0,0 +1,22 @@ +require 'test_helper' + +class Admin::DomainsControllerTest < ApplicationIntegrationTest + setup do + sign_in users(:admin) + @john = contacts(:john) + @john.update!(ident: '1234-1234') + @domain = domains(:shop) + end + + def test_search_by_hyphenated_registrant_ident_should_fail + get admin_domains_path, params: { q: { registrant_ident_matches: '1234-1234' } } + assert_response :success + assert_not_includes @response.body, @domain.name, "Search should not find domain when searching for hyphenated registrant ident" + end + + def test_search_by_hyphenated_contact_ident_should_fail + get admin_domains_path, params: { q: { contacts_ident_matches: '1234-1234' } } + assert_response :success + assert_not_includes @response.body, @domain.name, "Search should not find domain when searching for hyphenated contact ident" + end +end \ No newline at end of file From 56b21440e3efc982345c67cfd94e21e2c8edf4ee Mon Sep 17 00:00:00 2001 From: mmeest Date: Mon, 9 Jun 2025 14:34:15 +0300 Subject: [PATCH 4/6] modified tests --- test/integration/admin_area/domain_test.rb | 37 ++++++++++++---------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/test/integration/admin_area/domain_test.rb b/test/integration/admin_area/domain_test.rb index 66c1b3d0a..a74b85562 100644 --- a/test/integration/admin_area/domain_test.rb +++ b/test/integration/admin_area/domain_test.rb @@ -1,22 +1,25 @@ require 'test_helper' class Admin::DomainsControllerTest < ApplicationIntegrationTest - setup do - sign_in users(:admin) - @john = contacts(:john) - @john.update!(ident: '1234-1234') - @domain = domains(:shop) - end + setup do + sign_in users(:admin) + @john = contacts(:john) + @john.update!(ident: '1234-1234') # ident with hyphen + @domain = domains(:shop) + @domain.update!(registrant: @john) # make sure the domain is linked to @john + end - def test_search_by_hyphenated_registrant_ident_should_fail - get admin_domains_path, params: { q: { registrant_ident_matches: '1234-1234' } } - assert_response :success - assert_not_includes @response.body, @domain.name, "Search should not find domain when searching for hyphenated registrant ident" - end + def test_search_by_hyphenated_registrant_ident_should_succeed + get admin_domains_path, params: { q: { registrant_ident_matches: '1234-1234' } } + assert_response :success + assert_includes @response.body, @domain.name, + "Search should find domain when searching by hyphenated registrant ident" + end - def test_search_by_hyphenated_contact_ident_should_fail - get admin_domains_path, params: { q: { contacts_ident_matches: '1234-1234' } } - assert_response :success - assert_not_includes @response.body, @domain.name, "Search should not find domain when searching for hyphenated contact ident" - end -end \ No newline at end of file + def test_search_by_hyphenated_contact_ident_should_succeed + get admin_domains_path, params: { q: { contacts_ident_matches: '1234-1234' } } + assert_response :success + assert_includes @response.body, @domain.name, + "Search should find domain when searching by hyphenated contact ident" + end +end \ No newline at end of file From 2314a4aee2030c6584b2c386cd0b598b7d5db0ca Mon Sep 17 00:00:00 2001 From: mmeest Date: Tue, 10 Jun 2025 12:55:33 +0300 Subject: [PATCH 5/6] Code logic changed for ident --- app/models/domain.rb | 48 ---------------------- app/services/partial_search_formatter.rb | 11 ++--- test/integration/admin_area/domain_test.rb | 3 +- 3 files changed, 5 insertions(+), 57 deletions(-) diff --git a/app/models/domain.rb b/app/models/domain.rb index fcab13248..809d18615 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -282,54 +282,6 @@ class Domain < ApplicationRecord authorizable_ransackable_attributes end - def ransackable_scopes(*) - [:registrant_ident_cont_any, :contacts_ident_cont_any] - end - - def registrant_ident_cont_any(value) - return all if value.blank? - - # Split the value into parts and create search conditions - parts = value.split('-') - conditions = [] - - # Add the original value - conditions << "contacts.ident ILIKE '%#{value}%'" - - # Add conditions for each part - parts.each do |part| - conditions << "contacts.ident ILIKE '%#{part}%'" - end - - # Add condition with hyphens removed - clean_value = value.gsub('-', '') - conditions << "REPLACE(contacts.ident, '-', '') ILIKE '%#{clean_value}%'" - - joins(:registrant).where(conditions.join(' OR ')) - end - - def contacts_ident_cont_any(value) - return all if value.blank? - - # Split the value into parts and create search conditions - parts = value.split('-') - conditions = [] - - # Add the original value - conditions << "contacts.ident ILIKE '%#{value}%'" - - # Add conditions for each part - parts.each do |part| - conditions << "contacts.ident ILIKE '%#{part}%'" - end - - # Add condition with hyphens removed - clean_value = value.gsub('-', '') - conditions << "REPLACE(contacts.ident, '-', '') ILIKE '%#{clean_value}%'" - - joins(:contacts).where(conditions.join(' OR ')) - end - def nameserver_required? Setting.nameserver_required end diff --git a/app/services/partial_search_formatter.rb b/app/services/partial_search_formatter.rb index 48cbf4cc0..ca364630c 100644 --- a/app/services/partial_search_formatter.rb +++ b/app/services/partial_search_formatter.rb @@ -1,11 +1,10 @@ class PartialSearchFormatter def self.format(params) search_params = params.deep_dup - + search_params.each do |key, value| next unless should_format?(key, value) - - search_params[key] = format_value(value, key) + search_params[key] = format_value(value) end search_params @@ -15,13 +14,9 @@ class PartialSearchFormatter key.include?('matches') && value.present? end - def self.format_value(value, key) + def self.format_value(value) if value =~ /\A\*.*\*\z/ value.gsub(/\A\*|\*\z/, '') - elsif key.include?('ident') - # For contact identifiers, return array of values - parts = value.split('-') - parts.map { |part| "%#{part}%" } else "%#{value}%" end diff --git a/test/integration/admin_area/domain_test.rb b/test/integration/admin_area/domain_test.rb index a74b85562..c0db6c3b5 100644 --- a/test/integration/admin_area/domain_test.rb +++ b/test/integration/admin_area/domain_test.rb @@ -5,6 +5,7 @@ class Admin::DomainsControllerTest < ApplicationIntegrationTest sign_in users(:admin) @john = contacts(:john) @john.update!(ident: '1234-1234') # ident with hyphen + registrant = @john.becomes(Registrant) @domain = domains(:shop) @domain.update!(registrant: @john) # make sure the domain is linked to @john end @@ -22,4 +23,4 @@ class Admin::DomainsControllerTest < ApplicationIntegrationTest assert_includes @response.body, @domain.name, "Search should find domain when searching by hyphenated contact ident" end -end \ No newline at end of file +end From a8f13b692055576f9d98949c86842ce36c27332d Mon Sep 17 00:00:00 2001 From: mmeest Date: Tue, 10 Jun 2025 15:10:15 +0300 Subject: [PATCH 6/6] Changed solution --- Gemfile.lock | 8 ++++---- test/integration/admin_area/domain_test.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 7e13f0047..aaa2d9a94 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -262,8 +262,8 @@ GEM thor (>= 0.14.0, < 2) globalid (1.2.1) activesupport (>= 6.1) - google-protobuf (3.25.8) - google-protobuf (3.25.8-x86_64-linux) + google-protobuf (3.25.2) + google-protobuf (3.25.2-x86_64-linux) googleapis-common-protos-types (1.3.0) google-protobuf (~> 3.14) grpc (1.60.0) @@ -412,8 +412,8 @@ GEM request_store (~> 1.4) pdfkit (0.8.7.2) pg (1.5.9) - pg_query (6.1.0) - google-protobuf (>= 3.25.3) + pg_query (2.1.2) + google-protobuf (>= 3.17.1) pghero (3.1.0) activerecord (>= 6) pry (0.15.2) diff --git a/test/integration/admin_area/domain_test.rb b/test/integration/admin_area/domain_test.rb index c0db6c3b5..bae55fc55 100644 --- a/test/integration/admin_area/domain_test.rb +++ b/test/integration/admin_area/domain_test.rb @@ -7,7 +7,7 @@ class Admin::DomainsControllerTest < ApplicationIntegrationTest @john.update!(ident: '1234-1234') # ident with hyphen registrant = @john.becomes(Registrant) @domain = domains(:shop) - @domain.update!(registrant: @john) # make sure the domain is linked to @john + @domain.update!(registrant: registrant) # make sure the domain is linked to registrant end def test_search_by_hyphenated_registrant_ident_should_succeed