> For the complete documentation index, see [llms.txt](https://he-said.tdreyno.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://he-said.tdreyno.com/conceptual-from-scratch/acl-implementation.md).

# Implementing ACL

This is the conceptual/core-algebra ACL implementation guide. For the dedicated ACL facade package, see [ACL Guide](/package-guides/acl-guide.md) and [ACL API Reference](/package-guides/acl-api.md). If you're starting from scratch, prefer `@tdreyno/he-said/acl` and use this page when you need the underlying relation-backed model.

This guide shows a simple, real-world ACL model in he-said.

Scenario:

* A company stores documents in shared workspaces.
* Access is granted by explicit ACL entries, not by role.
* Each ACL entry links a user to a document with a permission.
* Permissions are `read`, `write`, or `owner`.
* `owner` implies write and read.
* `write` implies read.

The example keeps ACL explicit and concrete, using relations that match common database tables.

## Domain Model

### Types

```ts
type Permission = "read" | "write" | "owner"

type User = { id: string }
type Document = { id: string; workspaceId: string }
type AclEntry = { id: string }
```

### Terms and Relations

```ts
import { and, eq, oneOf, or, relation, term } from "@tdreyno/he-said"

const actor = term<User>()
const document = term<Document>()
const aclEntry = term<AclEntry>()
const aclPermission = term<Permission>()

const aclEntryUser = relation<AclEntry, User>()
const aclEntryDocument = relation<AclEntry, Document>()
const aclEntryPermission = relation<AclEntry, Permission>()
```

## ACL Rules

This section maps plain ACL semantics directly into policy rules.

```ts
const grantsReadPermission = aclPermission.is(value => {
  return value === "read" || value === "write" || value === "owner"
})

const grantsWritePermission = aclPermission.is(value => {
  return value === "write" || value === "owner"
})

const grantsOwnerPermission = aclPermission.is(value => {
  return value === "owner"
})

const canReadDocument = through(aclEntry)
  .to(aclEntryUser, actor)
  .to(aclEntryDocument, document)
  .to(aclEntryPermission, grantsReadPermission)

const canWriteDocument = through(aclEntry)
  .to(aclEntryUser, actor)
  .to(aclEntryDocument, document)
  .to(aclEntryPermission, grantsWritePermission)

const canManageDocument = through(aclEntry)
  .to(aclEntryUser, actor)
  .to(aclEntryDocument, document)
  .to(aclEntryPermission, grantsOwnerPermission)
```

If your endpoint carries a requested action, add an action term and branch once:

```ts
type Action = "read" | "download" | "write" | "manage"

const action = term<Action>()

const canAccessDocument = or(
  and(oneOf(action, ["read", "download"]), canReadDocument),
  and(eq(action, "write"), canWriteDocument),
  and(eq(action, "manage"), canManageDocument),
)
```

## Suggested Migration Path

1. Start from one ACL-backed endpoint, like view document.
2. Model your ACL table as relations: entry to user, entry to document, entry to permission.
3. Define one rule per operation: read, write, manage.
4. Move imperative checks into rule evaluation one endpoint at a time.
5. Add permission inheritance with derived terms instead of scattered conditionals.

## Common Pitfalls

* Treating ACL as a boolean flag and losing permission levels.
* Encoding inheritance in endpoint code instead of rule definitions.
* Forgetting document scope and matching only on user permission.
* Mixing tenant-wide admin bypass logic into ACL rules instead of composing a separate rule.

## Practical Rule of Thumb

Keep ACL close to the data model:

* Relations represent ACL table joins.
* Derived terms represent permission semantics.
* Composed rules represent endpoint decisions.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://he-said.tdreyno.com/conceptual-from-scratch/acl-implementation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
