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

Returns a new instance of ACL.

Parameters:

  • entries (#to_s)

    list of entries

  • default (Boolean) (defaults to: false)

    default allow, otherwise deny


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

Returns:

  • (Boolean)

    whether default allow


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

def default
  @default
end

#entriesArray<ACLEntry> (readonly)

The entries of this ACL

Returns:


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

Parameters:

  • acl (#to_s)

    string describing acl

  • kwargs (Hash)

    extra arguments defining acl

Returns:

  • (ACL)

    acl generated by string and 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

Parameters:

  • other (#to_h)

    entry to compare against

Returns:

  • (Boolean)

    how acls compare


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

Parameters:

  • principle (String)

    principle to check against

Returns:

  • (Boolean)

    does principle have access?


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

Parameters:

  • other (ACL)

    entry to compare against

Returns:

  • (Boolean)

    whether same objects


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

Returns:

  • (Fixnum)

    hash value of 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

Returns:

  • (Hash)

    the hash describing this object


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

Returns:

  • (String)

    the string describing this object


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

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