# Looking up one person by email

People find each other by email: a workspace's owner adds a colleague
by the address of their account. A person's address is theirs, and a
query may filter on a field only when everyone who may run it may read
the field ([What else shows a field](/docs/field-restrictions.md#what-else-shows-a-field)).
So an action finds one person by an exact address with a lookup, behind
its own permission check:

```jadpo
principal {
    user: {
        subject: Member.handle
    }
}

authentication {
    api_token: {}
}

policy {
    actions: {
        has_account: [Access.authenticated]
    }
}

type Handle = Text {
    max_length: 40
}

type AddressStanding = Enum {
    account_held
    no_account
}

entity Member {
    id: Uuid
    handle: Handle {
        unique: true
    }
    email: Email {
        unique: true
    }

    policy: {
        create: [Access.public]
        read: [id]
    }
}

/// Whether an address has an account.
action has_account(email: Member.email) -> AddressStanding {
    match Member.lookup(email: email) {
        none -> {
            return AddressStanding.no_account
        }
        some -> {
            return AddressStanding.account_held
        }
    }
}
```

`<Entity>.lookup(<field>: address)` gives the one record whose field is
exactly `address`, as the field keeps it, or `none`.

- The field is `unique: true`, built on `Email`, on an entity every
  tenant shares ([`data.lookup_not_unique`](/docs/errors/data.lookup_not_unique.md),
  [`data.lookup_not_email`](/docs/errors/data.lookup_not_email.md)). There
  is no partial match, no list and no ordering by it, so a lookup can't
  walk the table.
- Only an action calls it ([`type.lookup_context`](/docs/errors/type.lookup_context.md)),
  and the action is listed under root `policy { actions: }` with a line
  other than `Access.public`: that line is the lookup's permission check,
  in place of the entity's `read:`, checked as the action starts
  ([`policy.lookup_unchecked`](/docs/errors/policy.lookup_unchecked.md)).
- The found record's `id` always comes back. Its other fields reach a
  caller only where the entity's `read:`, and the field's own, let in
  everyone the line lets in, proved as any output is
  ([Outputs](/docs/field-restrictions.md#outputs)). Here a member's `handle` stays
  their own.
- A record its lifecycle hides isn't found, and a narrowed token never
  looks anyone up.

A line naming a role held in a record, such as `WorkspaceRole.owner`, is
checked as the action starts in the record whose `id` the action is
given as its one parameter of that type. So an owner adds a colleague to
their workspace by email, and anyone else, an owner of another workspace
too, is refused with `not_permitted` before the lookup runs, whatever
the address:

```jadpo
// policy.jadpo: desk.add_member_by_email: [WorkspaceRole.owner]
action add_member_by_email(workspace_id: Workspace.id, input: NewMemberByEmail) fails NoOneByThatEmail, AlreadyMember, WorkspaceNotFound -> MemberView {
    match User.lookup(email: input.email) {
        none -> {
            reject NoOneByThatEmail {}
        }
        some(person) -> {
            var member: Membership = attempt Membership.create {
                set: {
                    workspace_id: workspace_id
                    user_id: person.id
                    role: input.role
                }
                ...
            }
            ...
        }
    }
}
```
