Class: Clickwrap::AuthorityRule

Inherits:
Data
  • Object
show all
Defined in:
lib/clickwrap/authority.rb

Overview

The server-owned rule that says which represented parties a policy allows and which adapter has to establish the actor's authority. It is compiled into the policy revision and repeated in the signed presentation, so a browser cannot choose a weaker role or a different authority source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(represented_party_types: [], adapter_name: :host, minimum_role: nil, required_permission: nil, allow_represented_party_creation: false) ⇒ AuthorityRule

Returns a new instance of AuthorityRule.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/clickwrap/authority.rb', line 23

def initialize(represented_party_types: [], adapter_name: :host,
               minimum_role: nil, required_permission: nil,
               allow_represented_party_creation: false)
  normalized_types = Array(represented_party_types).filter_map do |type|
    name = type.is_a?(Class) ? type.name : type.to_s
    name unless name.strip.empty?
  end.uniq.freeze

  if normalized_types.empty?
    raise DefinitionError,
          "A represented-party authority rule must name at least one represented-party " \
          "class. An empty list would let the rule authorize every kind of record."
  end

  super(
    represented_party_types: normalized_types,
    adapter_name: adapter_name.to_s,
    minimum_role: minimum_role&.to_s,
    required_permission: required_permission&.to_s,
    allow_represented_party_creation: allow_represented_party_creation == true
  )
end

Instance Attribute Details

#adapter_nameObject (readonly)

Returns the value of attribute adapter_name

Returns:

  • (Object)

    the current value of adapter_name



16
17
18
# File 'lib/clickwrap/authority.rb', line 16

def adapter_name
  @adapter_name
end

#allow_represented_party_creationObject (readonly)

Returns the value of attribute allow_represented_party_creation

Returns:

  • (Object)

    the current value of allow_represented_party_creation



16
17
18
# File 'lib/clickwrap/authority.rb', line 16

def allow_represented_party_creation
  @allow_represented_party_creation
end

#minimum_roleObject (readonly)

Returns the value of attribute minimum_role

Returns:

  • (Object)

    the current value of minimum_role



16
17
18
# File 'lib/clickwrap/authority.rb', line 16

def minimum_role
  @minimum_role
end

#represented_party_typesObject (readonly)

Returns the value of attribute represented_party_types

Returns:

  • (Object)

    the current value of represented_party_types



16
17
18
# File 'lib/clickwrap/authority.rb', line 16

def represented_party_types
  @represented_party_types
end

#required_permissionObject (readonly)

Returns the value of attribute required_permission

Returns:

  • (Object)

    the current value of required_permission



16
17
18
# File 'lib/clickwrap/authority.rb', line 16

def required_permission
  @required_permission
end

Instance Method Details

#allows_represented_party_creation?Boolean

Returns:

  • (Boolean)


46
# File 'lib/clickwrap/authority.rb', line 46

def allows_represented_party_creation? = allow_represented_party_creation == true

#permits?(represented_party) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/clickwrap/authority.rb', line 48

def permits?(represented_party)
  return false if represented_party.nil?

  represented_party.class.ancestors.any? do |ancestor|
    ancestor.respond_to?(:name) && represented_party_types.include?(ancestor.name)
  end
end

#to_snapshotObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/clickwrap/authority.rb', line 56

def to_snapshot
  {
    "represented_party_types" => represented_party_types,
    "authority_adapter" => adapter_name,
    "when_actor_is_at_least" => minimum_role,
    "when_actor_has_permission" => required_permission,
    "including_when_this_action_creates_the_represented_party" =>
      allows_represented_party_creation?
  }.compact
end