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