]> git.openstreetmap.org Git - rails.git/commitdiff
Allow acls to match on parent domains
authorTom Hughes <tom@compton.nu>
Tue, 6 Jul 2021 09:12:51 +0000 (10:12 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 6 Jul 2021 09:13:33 +0000 (10:13 +0100)
app/models/acl.rb
test/models/acl_test.rb

index 90dd7f3cf48d19f30f9b6a5da185b4af0744eed6..a65c3a35ad68977fe1a1da5f6296c280006145a0 100644 (file)
@@ -23,7 +23,15 @@ class Acl < ApplicationRecord
   def self.match(address, options = {})
     acls = Acl.where("address >>= ?", address)
 
-    acls = acls.or(Acl.where(:domain => options[:domain])) if options[:domain]
+    if options[:domain]
+      labels = options[:domain].split(".")
+
+      until labels.empty?
+        acls = acls.or(Acl.where(:domain => labels.join(".")))
+        labels.shift
+      end
+    end
+
     acls = acls.or(Acl.where(:mx => options[:mx])) if options[:mx]
 
     acls
index ad17fc1b0eddcb9fb908196ee88188955ab3db1d..49f065612ffa8459d6198466e35deede9b55302a 100644 (file)
@@ -16,8 +16,10 @@ class AclTest < ActiveSupport::TestCase
 
   def test_no_account_creation_by_domain
     assert_not Acl.no_account_creation("192.168.1.1", :domain => "example.com")
+    assert_not Acl.no_account_creation("192.168.1.1", :domain => "test.example.com")
     create(:acl, :domain => "example.com", :k => "no_account_creation")
     assert Acl.no_account_creation("192.168.1.1", :domain => "example.com")
+    assert Acl.no_account_creation("192.168.1.1", :domain => "test.example.com")
   end
 
   def test_no_account_creation_by_mx