# `PgRest.Authorization.Apply`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/authorization/apply.ex#L1)

Applies a policy to one scoped source. `Authorization.check/5` runs first; the query
callback can only narrow the base query. Columns are physical database column names.

## Differences from PostgREST

Query policies are an application extension to database privileges/RLS. Stars expand
to permitted columns; an explicit hidden-column reference is 42501. Counts and aggregates
require explicit grants and permission limits cap their input rows, including totals.
PostgREST's db-max-rows limits output rows and does not cap exact totals.
With a permission cap, all count modes return the exact capped total instead of
a planner estimate, so an estimate cannot disclose rows outside the visible set.
RPCs with `result_scope: :none` discard row predicates only; hook denials, columns,
count/aggregate grants and permission limits still apply.
Hook narrowing needs a primary or unique key for source intersection; otherwise 42501.
PostgREST can apply RLS to keyless tables.

# `policy`

```elixir
@type policy() :: %{
  query: Ecto.Query.t() | nil,
  columns: :all | MapSet.t(String.t()),
  limit: pos_integer() | nil,
  aggregates?: boolean(),
  count?: boolean()
}
```

# `check_columns`

```elixir
@spec check_columns(:all | MapSet.t(), module(), [String.t()]) ::
  :ok | {:error, :denied}
```

Checks Ecto field names against a policy's physical column allowlist.

# `column_allowed?`

```elixir
@spec column_allowed?(:all | MapSet.t(), module(), atom() | String.t()) :: boolean()
```

Whether an Ecto field belongs to a physical-column grant.

# `combine`

```elixir
@spec combine(policy(), policy(), module(), keyword()) ::
  {:ok, policy()} | {:error, :denied}
```

Intersects grants, retaining the left flags; columns: :left retains a read grant.

# `explain_granted?`

```elixir
@spec explain_granted?(module(), Plug.Conn.t(), module(), Ecto.Query.t(), map()) ::
  boolean()
```

Explicit EXPLAIN admission; the plan still explains the original authorized statement.

# `intersect_columns`

```elixir
@spec intersect_columns(:all | MapSet.t(), :all | MapSet.t()) :: :all | MapSet.t()
```

Intersects physical-column grants.

# `min_limit`

```elixir
@spec min_limit(non_neg_integer() | nil, non_neg_integer() | nil) ::
  non_neg_integer() | nil
```

The smaller non-nil permission limit.

# `permits?`

```elixir
@spec permits?(:all | MapSet.t(), [String.t()]) :: boolean()
```

Whether every physical source name belongs to a column grant.

# `source`

```elixir
@spec source(
  Plug.Conn.t(),
  module() | nil,
  module(),
  PgRest.Authorization.operation(),
  Ecto.Query.t(),
  map(),
  keyword()
) :: {:ok, policy()} | {:error, :denied}
```

Authorizes and narrows one path/operation. Options `:authorize?` and `:rows?`
default to true; disabling the bare check never disables query narrowing.
With `rows?: false`, the hook still decides admission, columns, limits and grants,
but its row predicate is discarded without requiring a key.

# `unrestricted`

```elixir
@spec unrestricted(Ecto.Query.t() | nil) :: policy()
```

An unrestricted policy over a source (nil for a non-resource result).

---

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