Class: Spree::Storefront::AccessPolicy
- Inherits:
-
Object
- Object
- Spree::Storefront::AccessPolicy
- Defined in:
- app/models/spree/storefront/access_policy.rb
Overview
Storefront record access in one swappable object. The Store API never consults CanCanCan — customer authorization is ownership, and this policy is where ownership is decided, for any resource:
readable?/writable?answer per-record access,scopenarrows a relation to what the caller may list.
The default answer is "the caller owns the record" (guests own nothing); carts and orders specialize it with guest-token access and a completed?-guard on writes. New resources — core or extension — get the ownership default with no wiring.
This class is the storefront's extension seam: replace it via
Spree::Dependencies.storefront_access_policy_class when access must
widen or narrow beyond the owner — e.g. B2B company governance where
only an approver sees the subtree's purchases
(docs/plans/6.0-b2b-companies-and-catalogs.md). Override the entry
points and fall through to super:
class Enterprise::CompanyAccessPolicy < Spree::Storefront::AccessPolicy
def scope(base, token: nil)
return base.where(company_id: standing_company_ids) if
base.klass <= Spree::Order && approver?
super
end
end
Two things deliberately stay out of the policy: action vetoes (approval
required, spending limits) are checkout workflow validate hooks, and
the /customers/me/* endpoints stay owner-scoped by definition —
company-wide surfaces are their own endpoints built on this policy.
Instance Attribute Summary collapse
- #store ⇒ Spree::Store readonly
-
#user ⇒ Object?
readonly
The authenticated customer, or nil for guests.
Instance Method Summary collapse
-
#initialize(user:, store:) ⇒ AccessPolicy
constructor
A new instance of AccessPolicy.
-
#readable?(record, token: nil) ⇒ Boolean
Whether the caller may read a record: its owner by default; for carts and orders, the owner (by JWT) or the bearer of the guest token.
-
#scope(base, token: nil) ⇒ ActiveRecord::Relation
Narrows a relation to the records the caller may list: the caller's own by default; for orders, the guest-token branch serves order-confirmation lookups.
-
#writable?(record, token: nil) ⇒ Boolean
Whether the caller may mutate a record.
Constructor Details
#initialize(user:, store:) ⇒ AccessPolicy
Returns a new instance of AccessPolicy.
42 43 44 45 |
# File 'app/models/spree/storefront/access_policy.rb', line 42 def initialize(user:, store:) @user = user @store = store end |
Instance Attribute Details
#store ⇒ Spree::Store (readonly)
40 41 42 |
# File 'app/models/spree/storefront/access_policy.rb', line 40 def store @store end |
#user ⇒ Object? (readonly)
Returns the authenticated customer, or nil for guests.
37 38 39 |
# File 'app/models/spree/storefront/access_policy.rb', line 37 def user @user end |
Instance Method Details
#readable?(record, token: nil) ⇒ Boolean
Whether the caller may read a record: its owner by default; for carts and orders, the owner (by JWT) or the bearer of the guest token.
53 54 55 56 57 58 |
# File 'app/models/spree/storefront/access_policy.rb', line 53 def readable?(record, token: nil) return purchase_readable?(record, token: token) if purchase?(record) return company_standing?(record) if company_record?(record) owned?(record) end |
#scope(base, token: nil) ⇒ ActiveRecord::Relation
Narrows a relation to the records the caller may list: the caller's own by default; for orders, the guest-token branch serves order-confirmation lookups.
81 82 83 84 85 86 |
# File 'app/models/spree/storefront/access_policy.rb', line 81 def scope(base, token: nil) return purchases_scope(base, token: token) if purchase_class?(base.klass) return company_scope(base) if base.klass <= Spree::Company owned_scope(base) end |
#writable?(record, token: nil) ⇒ Boolean
Whether the caller may mutate a record. Same as readable? by
default; carts and orders additionally refuse once completed.
66 67 68 69 70 71 |
# File 'app/models/spree/storefront/access_policy.rb', line 66 def writable?(record, token: nil) return purchase_writable?(record, token: token) if purchase?(record) return company_standing?(record) if company_record?(record) owned?(record) end |