Class: OodSupport::ACL

Inherits:
Object
  • Object
show all
Defined in:
lib/ood_support/acl.rb

Overview

A helper object that describes an access control list (ACL) with entries

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries:, default: false) ⇒ ACL


26
27
28
29
# File 'lib/ood_support/acl.rb', line 26

def initialize(entries:, default: false)
  @entries = entries
  @default = default
end

Instance Attribute Details

#defaultBoolean (readonly)

Whether this ACL defaults to allow, otherwise default deny


10
11
12
# File 'lib/ood_support/acl.rb', line 10

def default
  @default
end

#entriesArray<ACLEntry> (readonly)

The entries of this ACL


6
7
8
# File 'lib/ood_support/acl.rb', line 6

def entries
  @entries
end

Class Method Details

.parse(acl, **kwargs) ⇒ ACL

Generate an ACL by parsing a string along with options


16
17
18
19
20
21
22
# File 'lib/ood_support/acl.rb', line 16

def self.parse(acl, **kwargs)
  entries = []
  acl.to_s.strip.split(/\n|,/).grep(/^[^#]/).each do |entry|
    entries << entry_class.parse(entry)
  end
  new(entries: entries, **kwargs)
end

Instance Method Details

#==(other) ⇒ Boolean

The comparison operator


54
55
56
# File 'lib/ood_support/acl.rb', line 54

def ==(other)
  to_h == other.to_h
end

#allow?(principle:) ⇒ Boolean

Check if queried principle has access to resource


34
35
36
37
# File 'lib/ood_support/acl.rb', line 34

def allow?(principle:)
  # Check in array order
  ordered_check(principle: principle)
end

#eql?(other) ⇒ Boolean

Checks whether two ACL objects are completely identical to each other


61
62
63
# File 'lib/ood_support/acl.rb', line 61

def eql?(other)
  self.class == other.class && self == other
end

#hashFixnum

Generates a hash value for this object


67
68
69
# File 'lib/ood_support/acl.rb', line 67

def hash
  [self.class, to_h].hash
end

#to_hHash

Convert object to hash


47
48
49
# File 'lib/ood_support/acl.rb', line 47

def to_h
  { entries: entries, default: default }
end

#to_sString

Convert object to string


41
42
43
# File 'lib/ood_support/acl.rb', line 41

def to_s
  entries.join("\n")
end