]> git.openstreetmap.org Git - rails.git/blob - app/models/acl.rb
Merge remote-tracking branch 'upstream/pull/3264'
[rails.git] / app / models / acl.rb
1 # == Schema Information
2 #
3 # Table name: acls
4 #
5 #  id      :bigint(8)        not null, primary key
6 #  address :inet
7 #  k       :string           not null
8 #  v       :string
9 #  domain  :string
10 #  mx      :string
11 #
12 # Indexes
13 #
14 #  acls_k_idx             (k)
15 #  index_acls_on_address  (address) USING gist
16 #  index_acls_on_domain   (domain)
17 #  index_acls_on_mx       (mx)
18 #
19
20 class Acl < ApplicationRecord
21   validates :k, :presence => true
22
23   def self.match(address, options = {})
24     acls = Acl.where("address >>= ?", address)
25
26     if options[:domain]
27       labels = options[:domain].split(".")
28
29       until labels.empty?
30         acls = acls.or(Acl.where(:domain => labels.join(".")))
31         labels.shift
32       end
33     end
34
35     acls = acls.or(Acl.where(:mx => options[:mx])) if options[:mx]
36
37     acls
38   end
39
40   def self.no_account_creation(address, options = {})
41     match(address, options).exists?(:k => "no_account_creation")
42   end
43
44   def self.no_note_comment(address, domain = nil)
45     match(address, :domain => domain).exists?(:k => "no_note_comment")
46   end
47
48   def self.no_trace_download(address, domain = nil)
49     match(address, :domain => domain).exists?(:k => "no_trace_download")
50   end
51 end