Skip to main content

Give roles through groups

Put people in groups, give the groups roles, and let a person inherit the permissions.

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.
Packages
lumo
Works with
lumo CLIPlain HTTP

What it shows​

  1. A role holds the permission invoices.approve. Maya Chen holds no role, so a permission check for her says no.
  2. The script creates the group Finance and gives it the role.
  3. It adds Maya to the group. The same check now says yes.
  4. It removes her from the group, and the answer is no again.

Nobody gives Maya the role directly. She holds it for as long as she is a member of the group.

Before you start​

  • An API key with the scopes admin:groups:write and authz:check. Create it in the portal: open API keys, under Applications & developer tools.
  • A permission, a role that holds it, and one user. The setup script creates them. Its API key also needs admin:permissions:write, admin:roles:write and admin:users:write.
  • For the CLI version: the lumo CLI. With an API key it needs no lumo login.

The person who created the API key must hold every permission of the role. LumoAuth refuses the request otherwise, because a group gives its roles to every member.

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 scopes admin:groups:write and authz:check.
ANALYST_USER_IDThe ID of a user who holds no role. 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/roles-through-groups/setup.mjs
setup.mjs
// Creates what the recipe needs: a permission, a role that holds it, and one
// test user who holds no role. Run it again at any time.
//
// node authorization/roles-through-groups/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';

const permission = await ensure('/admin/permissions', (p) => p.slug === 'invoices.approve', {
name: 'Approve invoices',
slug: 'invoices.approve',
description: 'Approve an invoice for payment.',
});

const role = await ensure('/admin/roles', (r) => r.slug === 'invoice-approver', {
name: 'Invoice approver',
slug: 'invoice-approver',
description: 'May approve invoices.',
});
await call('PUT', `/admin/roles/${role.id}/permissions`, { permissions: [permission.slug] });

const analyst = await ensure('/admin/users', (u) => u.email === 'finance-analyst@example.com', {
email: 'finance-analyst@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,
});

console.log(`Permission ${permission.slug}, role ${role.slug}, and one user are ready.`);
printEnv({ ROLE_ID: role.id, ANALYST_USER_ID: analyst.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 scripts were run with a role that holds a built-in permission. On a new organization the setup script stops at the step that adds the new permission to the role.

authorization/roles-through-groups/cli/groups.sh
#!/bin/sh
# Give a person a permission through a group, with the lumo CLI: create a
# group, give it a role, add a member, and ask for the answer before and after.
#
# sh authorization/roles-through-groups/cli/groups.sh
set -eu

PERMISSION="${PERMISSION:-invoices.approve}"

# The CLI reads its own three variables. With them it needs no `lumo login`.
export LUMO_BASE_URL="$LUMOAUTH_URL" LUMO_ORG="$LUMOAUTH_ORG_ID" LUMO_API_KEY="$LUMOAUTH_API_KEY"

# Asks whether the analyst holds the permission, and prints one line.
check() {
answer=$(lumo api POST /api/v1/authz/check --output json \
--data "{\"permission\": \"$PERMISSION\", \"subject\": {\"type\": \"user\", \"id\": \"$ANALYST_USER_ID\"}}")
case "$answer" in
*'"allowed": true'*) echo "$1: $PERMISSION -> allowed" ;;
*) echo "$1: $PERMISSION -> denied" ;;
esac
}

check "before the group"

# The group gets the role. The CLI has no command for the roles of a group,
# so `lumo api` calls the endpoint. A path takes the slug of a group.
lumo groups create --name "Finance" --description "The finance team" --quiet > /dev/null
lumo api PUT /groups/finance/roles --data '{"roles": ["invoice-approver"]}' > /dev/null

lumo groups members add finance --users "$ANALYST_USER_ID" > /dev/null
check "member of finance"

lumo groups members remove finance --users "$ANALYST_USER_ID" > /dev/null
check "removed from finance"

# Delete the group, so that the script can run again.
lumo groups delete finance > /dev/null
sh authorization/roles-through-groups/cli/groups.sh

The CLI has commands for groups and their members. It has no command for the roles of a group, so the script calls that endpoint with lumo api.

Check the result​

Output
before the group: invoices.approve -> denied
member of finance: invoices.approve -> allowed
removed from finance: invoices.approve -> denied
  • Each script deletes the group at the end, so that it can run again. Remove the last line to keep the group, then open it in the portal: under Identity, open Users, then the Groups tab.
  • The audit log has one entry for each change: role.assigned for the role of the group, then group.member_added and group.member_removed.
  • A webhook endpoint that subscribes to group.member_added and group.member_removed receives one event for each change of a member.

Limits​

  • Groups are flat. A group cannot contain another group.
  • A person keeps a role that they also hold directly. Removing them from the group then changes nothing.
  • A role with a permission that opens the admin portal makes every member an administrator. Each of them takes an admin seat of your plan.
  • LumoAuth sends the webhook role.assigned when a user gets a role directly. It sends no webhook when a group gets a role.