Class: Spree::AgentTools::Context
- Inherits:
-
Object
- Object
- Spree::AgentTools::Context
- Defined in:
- lib/spree/agent_tools/context.rb
Overview
Who is asking, and where. Every tool executes inside one of these, so a tool can never reach data the caller could not reach through the Admin API: lookups scope through #store, and #permitted? decides which tools the model is even offered.
Two kinds of caller, because the registry serves two adapters. The dashboard assistant asks on behalf of a signed-in admin, so its context carries a user and that user's ability. The MCP server asks on behalf of a secret API key, so its context carries the key and the key's scopes. Whichever it is, #principal is the thing that acted — which is what a workflow records as the canceller or approver.
Context.new(store: store, user: admin)
Context.new(store: store, api_key: key)
Instance Attribute Summary collapse
-
#ability ⇒ Spree::Ability?
readonly
The user's ability; nil for a key, whose authority is its scopes.
-
#api_key ⇒ Spree::ApiKey?
readonly
The authenticating secret key, or nil when an admin is asking.
-
#request_headers ⇒ Hash
readonly
The Rack headers the caller authenticated with, which a dispatched Admin API call repeats verbatim.
- #store ⇒ Spree::Store readonly
-
#user ⇒ Object?
readonly
The signed-in admin (Spree.admin_user_class), or nil when a key is asking.
Instance Method Summary collapse
-
#accessible(relation, action = :show) ⇒ ActiveRecord::Relation
Narrows a relation to the records this caller may act on, the same way an Admin API controller does.
-
#can?(action, record) ⇒ Boolean
Whether this caller may take an action on a specific record.
-
#granted_for?(action, record) ⇒ Boolean
Whether the grant covers an action on this record's resource.
-
#holds?(permission_key) ⇒ Boolean
Whether the caller holds a permission computed at call time — an export's or import's own scope, which comes from whichever subclass the caller named.
-
#initialize(store:, user: nil, api_key: nil, ability: nil, granted_scopes: nil, request_headers: {}) ⇒ Context
constructor
A new instance of Context.
-
#permission_keys ⇒ Array<String>
The catalog keys this caller holds, in the one currency both gates speak: an admin's activated role keys, or a secret key's scopes expanded the way the Admin API expands them (
write_ximpliesread_x,read_allandwrite_allfan out over the catalog). -
#permitted?(permission_key) ⇒ Boolean
Whether the caller holds a permission a tool declares.
-
#principal ⇒ Object
Who acted, for workflows that record it (
canceler:,approver:,created_by:). -
#user_principal? ⇒ Boolean
Whether a signed-in admin is asking.
Constructor Details
#initialize(store:, user: nil, api_key: nil, ability: nil, granted_scopes: nil, request_headers: {}) ⇒ Context
Returns a new instance of Context.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/spree/agent_tools/context.rb', line 49 def initialize(store:, user: nil, api_key: nil, ability: nil, granted_scopes: nil, request_headers: {}) raise ArgumentError, 'Spree::AgentTools::Context needs a user or an api_key' if user.nil? && api_key.nil? @store = store @user = user @api_key = api_key @granted_scopes = granted_scopes @request_headers = request_headers.to_h @ability = ability || (Spree::Dependencies.ability_class.constantize.new(user, store: store) if user) end |
Instance Attribute Details
#ability ⇒ Spree::Ability? (readonly)
Returns the user's ability; nil for a key, whose authority is its scopes.
31 32 33 |
# File 'lib/spree/agent_tools/context.rb', line 31 def ability @ability end |
#api_key ⇒ Spree::ApiKey? (readonly)
Returns the authenticating secret key, or nil when an admin is asking.
27 28 29 |
# File 'lib/spree/agent_tools/context.rb', line 27 def api_key @api_key end |
#request_headers ⇒ Hash (readonly)
Returns the Rack headers the caller authenticated with, which a dispatched Admin API call repeats verbatim. Empty for a caller that arrived without a request — a job, a console, a spec — which can only reach tools that do not dispatch.
47 48 49 |
# File 'lib/spree/agent_tools/context.rb', line 47 def request_headers @request_headers end |
#store ⇒ Spree::Store (readonly)
19 20 21 |
# File 'lib/spree/agent_tools/context.rb', line 19 def store @store end |
#user ⇒ Object? (readonly)
Returns the signed-in admin (Spree.admin_user_class), or nil when a key is asking.
23 24 25 |
# File 'lib/spree/agent_tools/context.rb', line 23 def user @user end |
Instance Method Details
#accessible(relation, action = :show) ⇒ ActiveRecord::Relation
Narrows a relation to the records this caller may act on, the same way an Admin API controller does.
For an admin, permission keys answer "may they touch products at all";
they do not answer "which products". A host app that registers a
record-level rule (can :read, Spree::Order, seller_id: ...) is
honoured by the Admin API, and must be honoured here too.
For a key there is no record-level filter to apply: a secret key's authority is its scopes, and the store is the whole tenancy answer — exactly how the Admin API treats key requests today.
90 91 92 93 94 95 |
# File 'lib/spree/agent_tools/context.rb', line 90 def accessible(relation, action = :show) return relation unless user_principal? return relation unless relation.respond_to?(:accessible_by) relation.accessible_by(ability, action) end |
#can?(action, record) ⇒ Boolean
Whether this caller may take an action on a specific record.
Checked at execution rather than at proposal time: an approval card may be decided minutes later, and roles can change in between. A key holds no record-level rules, so its scope check is the whole answer.
106 107 108 109 110 111 112 113 114 |
# File 'lib/spree/agent_tools/context.rb', line 106 def can?(action, record) return true unless user_principal? # A delegated grant narrows what the user may do here, so a record # check has to honour it too: being offered a tool is not permission # to use it on a record the grant does not cover. return false unless granted_for?(action, record) ability.can?(action, record) end |
#granted_for?(action, record) ⇒ Boolean
Whether the grant covers an action on this record's resource. A context without a grant is the user acting directly and is unlimited by this; a grant that names no scope for the resource refuses.
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/spree/agent_tools/context.rb', line 121 def granted_for?(action, record) return true if @granted_scopes.nil? scope = Spree..scope_for_resource(record.is_a?(Class) ? record : record.class) return true if scope.nil? # A write key always expands to its read key, so checking the one the # action needs is the whole answer. kind = %i[read show index].include?(action.to_sym) ? 'read' : 'write' .include?("#{kind}_#{scope.name}") end |
#holds?(permission_key) ⇒ Boolean
Whether the caller holds a permission computed at call time — an export's or import's own scope, which comes from whichever subclass the caller named.
Unlike #permitted? this refuses an unregistered key rather than raising on it: a tool's declaration is ours to get right, but a host app's export subclass may derive a key outside the catalog, and an agent asking for it should be told no, not crash the request.
161 162 163 164 165 166 167 168 |
# File 'lib/spree/agent_tools/context.rb', line 161 def holds?() return true if .blank? key = .to_s return false unless Spree..key?(key) .include?(key) end |
#permission_keys ⇒ Array<String>
The catalog keys this caller holds, in the one currency both gates
speak: an admin's activated role keys, or a secret key's scopes
expanded the way the Admin API expands them (write_x implies
read_x, read_all and write_all fan out over the catalog).
The respond_to? guard on the ability is house style:
Spree::Dependencies.ability_class is swappable and a custom class
need not implement it.
180 181 182 183 184 185 186 187 188 |
# File 'lib/spree/agent_tools/context.rb', line 180 def @permission_keys ||= if user_principal? held = ability.respond_to?(:permission_keys) ? Array(ability.).map(&:to_s) : [] @granted_scopes.nil? ? held : held & Spree..(@granted_scopes) else Spree..(api_key.scopes) end end |
#permitted?(permission_key) ⇒ Boolean
Whether the caller holds a permission a tool declares.
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/spree/agent_tools/context.rb', line 137 def permitted?() return true if .blank? key = .to_s # An unregistered key can never be held, so a typo would silently hide a # tool forever. Fail loudly in development instead. if !Spree..key?(key) && (defined?(Rails) && Rails.env.local?) raise ArgumentError, "Agent tool declares unknown permission key #{key.inspect}" end .include?(key) end |
#principal ⇒ Object
Who acted, for workflows that record it (canceler:, approver:,
created_by:). An admin user or an API key — both are registered in
Spree.actor_classes, so either can be stored polymorphically.
66 67 68 |
# File 'lib/spree/agent_tools/context.rb', line 66 def principal user || api_key end |
#user_principal? ⇒ Boolean
Returns whether a signed-in admin is asking.
71 72 73 |
# File 'lib/spree/agent_tools/context.rb', line 71 def user_principal? user.present? end |