# People and access

## Who can open an app

An app is private to its creator until someone shares it. It can never be public.

| Level       | Who can open it                                  |
| ----------- | ------------------------------------------------ |
| `private`   | the owner                                        |
| `selected`  | the owner and the people named one by one        |
| `workspace` | every current and future member of the workspace |

Separately, people from outside the workspace can be invited to one app as guests, when IT allows it. See People outside the workspace, below.

## Roles in an app

| Role      | Use the app | Deploy, share, roll back | How to get it                                             |
| --------- | ----------- | ------------------------ | --------------------------------------------------------- |
| `manager` | yes         | yes                      | create the app, or be named manager                       |
| `viewer`  | yes         | no                       | be named viewer, or open an app shared with the workspace |

`viewer` does not mean read-only. Inside the app, a viewer can do whatever your code allows.

Workspace owners and admins can see, disable and archive every app, but cannot open one that is not shared with them.

## Share

Only when the person asks, and only with the people they name.

- MCP: `share_app` with `{"appId": "…", "target": "user", "email": "lea@company.com", "role": "viewer"}`, or `{"appId": "…", "target": "workspace"}`.
- CLI: `boringcloud share --app <id> --email lea@company.com --role viewer`, or `--workspace`.

The person must already be a member of the workspace. Otherwise an admin invites them first, in the console. Sharing an app sends no email: the URL has to be passed on.

Removing access works from the next request, even in pages that are open. To stop everything at once, a manager or an admin can disable the app in its **Settings** tab.

## People outside the workspace

A manager of an app, or a workspace admin, can invite someone from another organization to use **this app only**: a client, a supplier, a freelancer. In the console: the app's **Access** tab, **Guests from outside the workspace**.

| Control        | How it works                                                                                                                         |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| Allowed by IT  | Off by default. IT turns it on in **IT → Agent stack → Sharing outside the workspace**, and can limit the email domains and the length |
| Invitation     | Sent by email. The link works once, for 7 days, and only for the address it was sent to                                              |
| Sign-in        | The guest creates an account with that address and confirms it from their mailbox. Their password has at least 10 characters          |
| Permissions    | One app, as `viewer`. A guest never manages the app, never joins the workspace, never sees other apps, and gets no agent token or API  |
| Duration       | Up to the maximum set by IT (30 days by default, at most 365). Access ends by itself on that date                                    |
| Traceability   | The **Activity** tab lists each person who opened the app, with requests, changes and refusals per day, and the actions of one person |
| Revocation     | **End access** works from the next request, in pages already open too. The record stays in the list, with who ended it and when       |

What also ends a guest's access at once: IT turning sharing off (all guests of the workspace), IT removing the guest's domain, IT shortening the maximum (accesses are shortened), disabling or archiving the app, the guest signing out.

Inside the app, a guest has `ctx.user.kind === "guest"` (header `x-boringcloud-user-kind`). Apps cannot email guests: [email](https://boring-cloud.app/docs/platform-services.md) only reaches workspace members.

Agents cannot invite guests: `share_app` and the CLI only accept members. A person does it in the console. A member of the workspace cannot be invited as a guest: share the app with them instead.

## Disable, archive, delete

People do this in the app's **Settings** tab. Agents cannot.

| Action  | Who                                         | What happens                                                                                | Back?                     |
| ------- | ------------------------------------------- | ------------------------------------------------------------------------------------------- | ------------------------- |
| Disable | a manager or an admin                       | nobody can open the app. Everything is kept                                                 | **Enable app**            |
| Archive | a manager or an admin                       | the same, and the app can't be deployed or shared. It no longer counts in the limit of apps | **Restore app**           |
| Delete  | the owner or an admin, only an archived app | the database, files, releases and accesses are deleted. The URL never works again           | no. The audit log is kept |

To delete, the person types the name of the app again.

## Permissions inside the app

Build them in the backend, from the identity headers. See [Backend](https://boring-cloud.app/docs/backend.md).

```js
// Only managers
if (user.role !== "manager") return json({ error: "Only managers can do this." }, 403);

// Only the author, or a manager: the condition is part of the change
const { meta } = await env.APP_DB.prepare("DELETE FROM requests WHERE id = ?1 AND (created_by = ?2 OR ?3 = 1)")
  .bind(id, user.id, user.role === "manager" ? 1 : 0)
  .run();
if (meta.changes === 0) return json({ error: "Not found." }, 404);
```

For more roles, such as an approver, keep a table of user ids in the app and let managers edit it. The app receives no list of members and no email addresses: record people when they first open the app.
