Module: ApiKeys::TenantResolution

Extended by:
ActiveSupport::Concern
Defined in:
lib/api_keys/tenant_resolution.rb

Overview

Controller concern to resolve and provide access to the tenant associated with the currently authenticated API key.

Instance Method Summary collapse

Instance Method Details

#current_api_tenantObject?

Returns the tenant associated with the current API key. Uses the configured ‘tenant_resolver` lambda. Returns nil if no key is authenticated, no owner exists, or the resolver returns nil.

Returns:

  • (Object, nil)

    The resolved tenant object, or nil.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/api_keys/tenant_resolution.rb', line 28

def current_api_tenant
  return @current_api_tenant if defined?(@current_api_tenant)
  return nil unless current_api_key # Requires Authentication concern to be included first

  resolver = ApiKeys.configuration.tenant_resolver
  @current_api_tenant = resolver&.call(current_api_key)
rescue StandardError => e
  # Log error but don't break the request if resolver fails
  Rails.logger.error "[ApiKeys] Tenant resolution failed: #{e.message}" if defined?(Rails.logger)
  @current_api_tenant = nil
end