# `Ash.PlugHelpers`
[🔗](https://github.com/ash-project/ash/blob/v3.23.1/lib/ash/plug_helpers.ex#L6)

Helpers for working with the Plug connection.

# `get_actor`

```elixir
@spec get_actor(Plug.Conn.t()) :: nil | Ash.Resource.record()
```

Retrieves the actor from the Plug connection.

The actor is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).

## Deprecation warning

This function checks to see if the actor is already set in the `@actor`
assign, and if so will emit a deprecation warning.

This is to allow apps using the previous method a chance to update.

Rather than setting the actor in the assigns, please use the `set_actor/2`
method.

## Example

    iex> actor = build_actor(%{email: "marty@1985.retro"})
    ...> conn = build_conn() |> put_private(:ash, %{actor: actor})
    ...> actor = get_actor(conn)
    %{email: "marty@1985.retro"} = actor

    iex> actor = build_actor(%{email: "marty@1985.retro"})
    ...> conn = build_conn() |> assign(:actor, actor)
    ...> actor = get_actor(conn)
    %{email: "marty@1985.retro"} = actor

# `get_context`

```elixir
@spec get_context(Plug.Conn.t()) :: nil | map()
```

Retrieves the context from the Plug connection.

The context is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).

## Example

    iex> context = %{fraud_score: 0.427}
    ...> conn = build_conn() |> put_private(:ash, %{context: context})
    ...> context = get_context(conn)
    %{fraud_score: 0.427}

# `get_tenant`

```elixir
@spec get_tenant(Plug.Conn.t()) :: term()
```

Retrieves the tenant from the Plug connection.

The tenant is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).

## Deprecation warning

This function checks to see if the tenant is already set in the `@tenant`
assign, and if so will emit a deprecation warning.

This is to allow apps using the previous method a chance to update.

Rather than setting the tenant in the assigns, please use the `set_tenant/2`
method.

## Example

    iex> conn = build_conn() |> put_private(:ash, %{tenant: "my-tenant"})
    ...> tenant = get_tenant(conn)
    "my-tenant" = tenant

    iex> conn = build_conn() |> assign(:tenant, "my-tenant")
    ...> tenant = get_tenant(conn)
    "my-tenant" = tenant

# `set_actor`

```elixir
@spec set_actor(Plug.Conn.t(), term()) :: Plug.Conn.t()
```

Sets the actor inside the Plug connection.

The actor is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).

## Example

    iex> actor = build_actor(%{email: "marty@1985.retro"})
    ...> conn = build_conn() |> set_actor(actor)
    %Plug.Conn{private: %{ash: %{actor: %{email: "marty@1985.retro"}}}} = conn

# `set_context`

```elixir
@spec set_context(Plug.Conn.t(), map()) :: Plug.Conn.t()
```

Sets the context inside the Plug connection.

Context can be used to store arbitrary data about the user, connection, or
anything else you like that doesn't belong as part of the actor or tenant.

The context is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).

## Example

    iex> context = %{fraud_score: 0.427}
    ...> conn = build_conn() |> set_context(context)
    %Plug.Conn{private: %{ash: %{context: %{fraud_score: 0.427}}}} = conn

# `set_tenant`

```elixir
@spec set_tenant(Plug.Conn.t(), Ash.ToTenant.t()) :: Plug.Conn.t()
```

Sets the tenant inside the Plug connection.

The tenant is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).

## Example

    iex> conn = build_conn() |> set_tenant("my-tenant")
    %Plug.Conn{private: %{ash: %{tenant: "my-tenant"}}} = conn

# `update_actor`

```elixir
@spec update_actor(Plug.Conn.t(), (nil | Ash.Resource.record() -&gt;
                               nil | Ash.Resource.record())) ::
  Plug.Conn.t()
```

Updates the actor inside the Plug connection.

The actor is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).

## Example

    iex> actor = build_actor(%{email: "marty@1985.retro"})
    ...> conn = build_conn() |> put_private(:ash, %{actor: actor})
    ...> actor = get_actor(conn)
    %{email: "marty@1985.retro"} = actor
    ...> conn = update_actor(conn, fn actor -> Map.put(actor, :name, "Marty Retro") end)
    ...> actor = get_actor(conn)
    %{email: "marty@1985.retro", name: "Marty Retro"} = actor
    ...> conn = update_actor(conn, fn actor -> Map.delete(actor, :email) end)
    ...> actor = get_actor(conn)
    %{name: "Marty Retro"} = actor

# `update_context`

```elixir
@spec update_context(Plug.Conn.t(), (nil | map() -&gt; nil | map())) :: Plug.Conn.t()
```

Updates the context inside the Plug connection.

The context is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).

## Example

    iex> context = %{species: "Fythetropozoat"}
    ...> conn = build_conn() |> put_private(:ash, %{context: context})
    ...> context = get_context(conn)
    %{fraud_score: 0.427} = context
    ...> conn = update_context(conn, fn context -> Map.put(context, :location, "Barnard's Loop") end)
    ...> context = get_context(conn)
    %{species: "Fythetropozoat", location: "Barnard's Loop"} = context
    ...> conn = update_context(conn, fn context -> Map.delete(context, :fraud_score) end)
    ...> context = get_context(conn)
    %{location: "Barnard's Loop"} = context

---

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