Class: Clickwrap::Integrations::OrganizationsAuthority

Inherits:
Object
  • Object
show all
Defined in:
lib/clickwrap/integrations/organizations_authority.rb

Overview

Optional adapter for https://github.com/rameerez/organizations.

There is deliberately no runtime dependency on that gem. The adapter is selected only by a policy that says permit_acting_for_organization, and it talks to the public Membership association/API when capture occurs. A missing gem, wrong represented party type, missing membership, stale/insufficient role, or insufficient permission all deny authority.

Constant Summary collapse

SOURCE =
"organizations.membership"
ADAPTER_VERSION =
"1"

Instance Method Summary collapse

Instance Method Details

#verify(actor:, represented_party:, authority_rule:, tenant:, authentication_context:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clickwrap/integrations/organizations_authority.rb', line 17

def verify(actor:, represented_party:, authority_rule:, tenant:,
           authentication_context:)
  return denied unless organizations_available?
  return denied unless represented_party.is_a?(::Organizations::Organization)
  return denied if represented_party.respond_to?(:persisted?) && !represented_party.persisted?
  return denied unless actor.respond_to?(:id) && actor.id.present?
  return denied if actor.respond_to?(:persisted?) && !actor.persisted?
  return denied unless represented_party.respond_to?(:memberships)
  return denied if tenant.present? && Reference.tenant(tenant) != Reference.tenant(represented_party)

  # Capture calls authority adapters inside its transaction. Taking the
  # membership row lock makes a concurrent removal or demotion serialize
  # with the evidence write instead of authorizing from a stale role.
  membership = represented_party.memberships.lock.find_by(user_id: actor.id)
  return denied unless membership
  return denied unless sufficient_role?(membership, authority_rule.minimum_role)
  return denied unless sufficient_permission?(membership, authority_rule.required_permission)

  AuthorityDecision.new(
    authorized: true,
    source: SOURCE,
    role: membership_role(membership),
    verified_at: Clickwrap.now,
    details: {
      "adapter_version" => ADAPTER_VERSION,
      "membership_reference" => Reference.record(membership),
      "represented_party_reference" => Reference.record(represented_party),
      "minimum_role" => authority_rule.minimum_role,
      "required_permission" => authority_rule.required_permission,
      "required_permission_was_granted" => authority_rule.required_permission.present? || nil,
      "authentication_method" => authentication_context.to_h[:method]&.to_s
    }.compact
  )
rescue NoMethodError
  denied
end