# Organizations and roles

> How Pontive models your customers' organizations, members, roles and permissions, which organization a session acts in, and how your app and API read roles and permissions from the token or ask the authorization API.

Source: https://www.pontive.com/docs/guides/organizations-and-roles

## The model [#the-model]

**Tenancy mode.** A project is `b2b` or `b2c`, chosen when it is created and fixed. In a `b2b` project
your customers are **organizations** (`org_…`) and users act inside them; a `b2c` project has no
organizations and its users act on their own.

**Organizations** are flat: each sits directly under the instance, and an organization is the tenant
boundary. A user can belong to several, with different roles in each.

**Roles and permissions.** A permission is an action key, such as `invoice:approve`. A role is a named
set of permissions (or a wildcard role with every permission) defined on a resource type, and assigned
to a member. Two types are built in:

| Type key | What it is | Seeded roles |
| --- | --- | --- |
| `root` | The instance itself. Roles here apply instance-wide: your own staff, or end-user tiers in a `b2c` app. | `owner`, `admin` |
| `org` | Organizations (`b2b` only). Roles here apply inside one organization. | `owner`, `admin` |

Every organization always has at least one `owner`. The instance's `owner` and `admin` reach every
organization through inheritance. You add your own roles and permissions on either type, and with the
Fine-Grained Authorization capability you declare your own resource types (documents, projects…) with
roles on individual objects.

**Membership** binds a user to the instance root or to an organization with a set of role keys.

## Which organization a session acts in [#which-organization-a-session-acts-in]

A session acts in **at most one** organization at a time. Users are never asked to pick at sign-in:

1. the organization the app names (`organizationId` on the provider, `organization_id` on
   `/oauth2/authorize`, or `?organization_id=` on the server SDK's login route);
2. otherwise the user's only organization;
3. otherwise none.

A user moves the session to another of their organizations with the `<OrganizationSwitcher>`
component, the server SDK's switch route, or `Auth.switchOrganization(id)` in the browser. A switch to
an organization the user is not a member of is refused and leaves the session as it was.

## Roles and permissions in the token [#roles-and-permissions-in-the-token]

The access token carries the session's organization and what the user holds there:

```json
{
  "sub": "usr_...",
  "org_id": "org_...",
  "roles": ["admin"],
  "permissions": ["invoice:approve", "invoice:read"]
}
```

- `roles` are the user's **effective** role keys there: held directly plus those conferred by
  inheritance. There is no single `role` claim: a member can hold several roles, and none ranks above
  another, so check for the one you need.
- With no `org_id`, `roles` and `permissions` are those the user holds on the instance root. A `b2c`
  token never has an `org_id`.
- They are re-read on every refresh, so a role granted or revoked reaches the next access token. A
  suspended organization grants nothing, and its claims are not issued.

### In a Next.js server component [#in-a-nextjs-server-component]

```tsx
import { pontive } from "@/lib/pontive"; // see the Next.js quickstart

export default async function Invoices() {
  const session = await pontive().auth();
  const org = session?.organization; // { id, roles, permissions } or null
  if (!org?.permissions.includes("invoice:read")) return <p>Not allowed.</p>;
  return <p>Invoices of {org.id}</p>;
}
```

To move the session, link to the switch route ([server sessions](/docs/sdk/server-sessions#organizations-b2b)):

```tsx
import { switchOrganizationPath } from "@pontive/pontkit-nextjs/server";

<a href={switchOrganizationPath("/api/auth", org.id, "/dashboard")}>{org.name}</a>;
```

### In the browser [#in-the-browser]

```ts
import { Auth } from "@pontive/oidc-client";

Auth.getOrganization(); // { id, roles, permissions } | null
await Auth.switchOrganization("org_...");
```

In React, `useSession()` from `@pontive/pontkit-react` reports `organization-changed` with the new
`organizationId`, so you can reload what depends on it.

### In your API [#in-your-api]

`@pontive/pontkit-server`'s resource server gives each verified caller `orgId`, `roles` and
`permissions` ([Protect an API](/docs/guides/protect-an-api)); any JWT library reads the same claims.

## Fine-grained checks [#fine-grained-checks]

The token is a snapshot of one organization. Ask the authorization API, on your request path, when
the question is about a specific object, another organization, or must reflect a change made seconds
ago. It speaks [OpenID AuthZEN 1.0](/docs/api-reference/authz/authzen):

```http
POST https://authz.{region}.pontive.com/access/v1/evaluation
Authorization: Bearer <a token or secret key holding sys:authz:check>

{
  "subject": { "type": "user", "id": "usr_..." },
  "action": { "name": "invoice:approve" },
  "resource": { "type": "org", "id": "org_..." }
}
```

```json
{ "decision": true, "context": { "roles": ["admin"] } }
```

`POST /access/v1/evaluations` checks up to 100 questions at once. With `@pontive/pontkit-server`:

```ts
import { createAuthorizationClient } from "@pontive/pontkit-server/authorization";

const authz = createAuthorizationClient({
  managementUrl: "https://api.us.pontive.com", // the authz host, https://authz.us.pontive.com, is derived from it
  issuer: process.env.PONTIVE_ISSUER!,
  credential: { clientId: "svc_...", clientSecret: process.env.PONTIVE_CLIENT_SECRET! }, // holds sys:authz:check
});

const allowed = await authz.check(principal, "invoice:approve", { type: "org", id: "org_..." });
```

`principal` is the caller your resource server verified. For an agent acting for a user, the client
checks by token, so Pontive answers for what both the user and the agent may do.

## Managing organizations, members and roles [#managing-organizations-members-and-roles]

- **From your backend or an AI client:** the [management API](/docs/api-reference/management/organizations)
  (`POST /organizations`, `POST /organizations/{organization}/members` with `user_id` and `roles`,
  `POST /authz/resource-types/{resource_type}/roles`), or the same operations as tools of the
  [Pontive MCP server](/docs/guides/pontive-mcp-server).
- **By your customers' own admins, inside your app:** the `<MemberManagement>` component lists members
  and invitations, changes roles and invites people, within the signed-in user's own roles. It needs
  the `pontive:org:member:read`, `pontive:org:member:manage` and `pontive:org:member:invite` scopes,
  allowed on the app and asked for at sign-in, and refresh tokens on.
