# `AshHooks.Tenancy`
[🔗](https://github.com/baselabs/ash_hooks/blob/v3.3.0/lib/ash_hooks/tenancy.ex#L1)

Validates an operation's resource modules against a tenant.

`resolve/2` takes the resource modules an entry point touches and the tenant:

* If none declares multitenancy, the tenant passes through unchanged.
* If all declare attribute multitenancy on the same attribute with
  `global?: false`, a tenant is required. Missing tenants return
  `{:error, :tenant_required}`.
* Mixed declarations, different tenant attributes, context multitenancy,
  or `global?: true` return `{:error, :tenancy_mismatch}`.

Entry points supply every resource they read or write, including optional
event-log and attempt-history resources. They reject missing or mismatched
tenancy before data access. Event-log replay forwards its tenant to resend
when rearming an existing terminal delivery.

# `error`

```elixir
@type error() :: :tenant_required | :tenancy_mismatch
```

Errors returned when the supplied resource set or tenant is invalid.

# `from_row`

```elixir
@spec from_row(struct()) :: term()
```

Recovers the tenant from a stored row using its resource's
`tenant_from_attribute` callback. Resources without attribute multitenancy
return `nil`. Use this value for tenant options and custody canonicalization.

# `resolve`

```elixir
@spec resolve([module()], term()) :: {:ok, term()} | {:error, error()}
```

Validates the resources an operation touches against the caller's tenant.

`modules` is the caller-supplied resource set. Only these modules are
checked. `tenant` is the caller-supplied tenant, or `nil` when absent.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
