Skip to main content

Check many permissions at once

Decide what a page shows with one request: every button of a toolbar, or every row of a table.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptPythonGoPlain HTTP

What it shows​

  1. A toolbar has four buttons: view, edit, delete and share. Each button needs one permission.
  2. Your backend sends the four permissions in one request. The answer has one result for each.
  3. The page shows a button when its result is true. Maya holds a role with three of the permissions, so the delete button stays hidden.
  4. Two more requests answer a compound rule: "any of" these permissions, and "all of" them.

Your backend can send the results with the data of the page. The browser then needs no request of its own.

Before you start​

  • An API key with the scope authz:check. Create it in the portal: open API keys, under Applications & developer tools.
  • Four permissions, a role that holds three of them, and one user. The setup script creates them. Its API key also needs admin:permissions:write, admin:roles:write and admin:users:write.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYAn API key with the scope authz:check.
EDITOR_USER_IDThe ID of a user who holds three of the four permissions. The setup script prints it.

The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.

node authorization/bulk-checks/setup.mjs
setup.mjs
// Creates what the recipe needs: four permissions, a role that holds three of
// them, and one test user with the role. Run it again at any time.
//
// node authorization/bulk-checks/setup.mjs
//
// The API key needs the scopes admin:permissions:write, admin:roles:write
// and admin:users:write.
import { randomBytes } from 'node:crypto';
import { call, ensure, printEnv } from '../../_lib/admin.mjs';

for (const [slug, name] of [
['documents.view', 'View documents'],
['documents.edit', 'Edit documents'],
['documents.delete', 'Delete documents'],
['documents.share', 'Share documents'],
]) {
await ensure('/admin/permissions', (p) => p.slug === slug, { name, slug });
}

const role = await ensure('/admin/roles', (r) => r.slug === 'document-editor', {
name: 'Document editor',
slug: 'document-editor',
description: 'May view, edit and share documents, and may not delete them.',
});
await call('PUT', `/admin/roles/${role.id}/permissions`, {
permissions: ['documents.view', 'documents.edit', 'documents.share'],
});

const editor = await ensure('/admin/users', (u) => u.email === 'document-editor@example.com', {
email: 'document-editor@example.com',
name: 'Maya Chen',
// The recipe never signs in as this user, so nobody needs the password.
password: `${randomBytes(18).toString('base64url')}#Aa1`,
emailVerified: true,
});
await call('PUT', `/admin/users/${editor.id}/roles`, { roles: [role.slug] });

console.log(`Four permissions, the role ${role.slug}, and one user are ready.`);
printEnv({ EDITOR_USER_ID: editor.id });

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. The programs were run with built-in permissions. On a new organization the setup script stops at the step that adds the new permissions to the role. The curl version prints the JSON answers of the three requests.

authorization/bulk-checks/js/toolbar.mjs
// Ask about every button of a toolbar with one request, then ask two
// compound questions.
//
// node authorization/bulk-checks/js/toolbar.mjs
import { LumoAuthBackend } from '@lumoauth/backend';

const lumo = new LumoAuthBackend({
baseUrl: process.env.LUMOAUTH_URL,
orgId: process.env.LUMOAUTH_ORG_ID,
secretKey: process.env.LUMOAUTH_API_KEY,
});

// One permission for each button: view, edit, delete, share.
const buttons = (process.env.PERMISSIONS ?? 'documents.view,documents.edit,documents.delete,documents.share').split(',');
// The key asks about another person, so it names that person as the subject.
const subject = { userId: process.env.EDITOR_USER_ID };

// One request for the whole toolbar.
const { results } = await lumo.permissions.checkBulk(buttons, undefined, subject);
for (const permission of buttons) {
console.log(`${permission} -> ${results[permission] ? 'show' : 'hide'}`);
}

// "Any of" and "all of", each with one request.
const editOrDelete = [buttons[1], buttons[2]];
const any = await lumo.permissions.checkAny(editOrDelete, undefined, subject);
const all = await lumo.permissions.checkAll(editOrDelete, undefined, subject);
console.log(`any of ${editOrDelete.join(', ')} -> ${any ? 'allowed' : 'denied'}`);
console.log(`all of ${editOrDelete.join(', ')} -> ${all ? 'allowed' : 'denied'}`);
node authorization/bulk-checks/js/toolbar.mjs

Check the result​

Output
documents.view -> show
documents.edit -> show
documents.delete -> hide
documents.share -> show
any of documents.edit, documents.delete -> allowed
all of documents.edit, documents.delete -> denied
  • Add documents.delete to the role in the portal, and run the program again: the third line says "show", and "all of" is allowed.
  • To ask from the browser, with the token of the signed-in person, use the same call on the client of @lumoauth/react. The page usePermission shows it. The network panel of the browser then shows one request for the whole toolbar.

Limits​

  • One request takes at most 100 permissions. A longer list gets the answer 400 with the code TOO_MANY_PERMISSIONS: split it.
  • The results hide controls. Check the permission again in the API that does the work, because a browser can be changed.
  • "Any of" and "all of" answer with one yes or no. They do not say which permission decided.
  • The endpoint that lists every permission of a person, GET /api/v1/authz/permissions, takes only an access token with the scope authz:read. An API key cannot call it.