]> git.openstreetmap.org Git - rails.git/blob - app/models/acl.rb
Add frozen_string_literal comments to ruby files
[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) 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
25   def self.match(address, options = {})
26     acls = Acl.where("address >>= ?", address)
27
28     if options[:domain]
29       labels = options[:domain].split(".")
30
31       until labels.empty?
32         acls = acls.or(Acl.where(:domain => labels.join(".")))
33         labels.shift
34       end
35     end
36
37     acls = acls.or(Acl.where(:mx => options[:mx])) if options[:mx]
38
39     acls
40   end
41
42   def self.no_account_creation?(address, options = {})
43     match(address, options).exists?(:k => "no_account_creation")
44   end
45
46   def self.allow_account_creation?(address, options = {})
47     match(address, options).exists?(:k => "allow_account_creation")
48   end
49
50   def self.no_note_comment?(address, domain = nil)
51     match(address, :domain => domain).exists?(:k => "no_note_comment")
52   end
53
54   def self.no_trace_download?(address, domain = nil)
55     match(address, :domain => domain).exists?(:k => "no_trace_download")
56   end
57 end