# as_user

> The user a call acts for.

`with noclick.as_user(user):` sets it for everything inside, sync or async:
`.aio` calls, tasks and `asyncio.to_thread`. A thread you start yourself
begins without it (a `contextvars` rule), so pass `user=` there. A
method's `user=` argument wins over it.

## noclick.as_user

```python
class as_user
```

Act for `user` inside the block: a `User`, your id for the user, or None
for the account itself. Any string is your id; a user id that breaks the id
rules raises `InvalidRequest` here.

Usage:

Every call inside the block acts for the user: here, Ana's own Gmail and agent:

```python
with noclick.as_user("user_42"):
    gmail = noclick.Connection.from_name("gmail", integration="gmail", create_if_missing=True)
    assistant = noclick.Agent.from_name(
        "assistant", create_if_missing=True, instructions="You help the user with their email.", tools=[gmail],
    )
```

It works the same with `async with`, and follows your code into tasks and
`asyncio.to_thread`:

```python
async with noclick.as_user(noclick.User.from_id("user_42")):
    agents = await noclick.Agent.list.aio()
```

Your ids can take any shape, a UUID or an identity provider's subject:

```python
with noclick.as_user("auth0|6f1c2b7e9a"):
    connections = noclick.Connection.list()
```

### noclick.as_user

```python
noclick.as_user(user)
```

Parameters:

- `user` (`Any`, required): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

## noclick.current_user

```python
noclick.current_user() -> Any
```

The user set by the innermost `as_user` (a `User`, an id, or None).

Returns: `Any` What the innermost `as_user` was given, as given (nothing is looked up), or `None`
outside every `as_user` block, where calls act for your account.

Usage:

```python
with noclick.as_user("user_42"):
    print(noclick.current_user())   # user_42
print(noclick.current_user())       # None
```

## noclick.ALL_USERS

```python
noclick.ALL_USERS = 'all'
```

Values for a read's `user_filter`: every user plus account-level rows, or account-level only.
## noclick.NO_USER

```python
noclick.NO_USER = 'none'
```

Values for a read's `user_filter`: every user plus account-level rows, or account-level only.
