MishkaPay.Ash.Subscribed (Mishka Pay v0.0.1)

Copy Markdown View Source

Whether a user has a subscription that gives access now: a policy check for the host's own resources, and subscribed?/3 for code and templates. Members-only posts and files sold by plan are its first use.

policies do
  policy action(:read_members_post) do
    authorize_if {MishkaPay.Ash.Subscribed, resource: MyApp.Pay.Subscription}
  end

  policy action(:download_pro_file) do
    authorize_if {MishkaPay.Ash.Subscribed,
                  resource: MyApp.Pay.Subscription, product: ["prod_pro", "prod_team"]}
  end
end

It reads resource (a kind :subscription resource with belongs_to_user) for a row of the user with a status that gives access, in one query each time, so a cancel or a failed renewal the webhooks brought counts at once. The row's user comes from the checkout's owner (MishkaPay.Ash.Owner).

Options

  • :resource: the subscription resource. Required.
  • :price, :product: a provider price or product id, or a list of them; the user needs a subscription to one of them. Left out, any subscription counts.
  • :statuses: the statuses that give access, [:active, :trialing] unless named. A trialing row counts until its trial_end. Add :past_due to keep access while the provider retries a failed renewal.
  • :tenant: the site, for subscribed?/3; the check takes the action's tenant. A resource with Ash multitenancy reads in that tenant, any other compares its :tenant attribute.

The check fails closed: no actor, an actor that is not a resource record, or a read that fails, and access is refused.

Summary

Functions

Whether user (a record of the host's user resource, or its id) has a subscription in resource that gives access now. Takes the options of the check.

Callback implementation for Ash.Policy.Check.type/0.

Functions

eager_evaluate?()

Callback implementation for Ash.Policy.Check.eager_evaluate?/0.

prefer_expanded_description?()

Callback implementation for Ash.Policy.Check.prefer_expanded_description?/0.

requires_original_data?(_, _)

Callback implementation for Ash.Policy.Check.requires_original_data?/2.

strict_check(actor, context, opts)

Callback implementation for Ash.Policy.Check.strict_check/3.

subscribed?(resource, user, opts \\ [])

@spec subscribed?(Ash.Resource.t(), term(), keyword()) :: boolean()

Whether user (a record of the host's user resource, or its id) has a subscription in resource that gives access now. Takes the options of the check.

type()

Callback implementation for Ash.Policy.Check.type/0.