Give roles through groups
Put people in groups, give the groups roles, and let a person inherit the permissions.
What it shows
- A role holds the permission
invoices.approve. Maya Chen holds no role, so a permission check for her says no. - The script creates the group Finance and gives it the role.
- It adds Maya to the group. The same check now says yes.
- 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:writeandauthz: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:writeandadmin:users:write. - For the CLI version: the
lumoCLI. With an API key it needs nolumo 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.
| Variable | Value |
|---|---|
LUMOAUTH_URL | The address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region. |
LUMOAUTH_ORG_ID | Your organization, for example acme-corp. |
LUMOAUTH_API_KEY | An API key with the scopes admin:groups:write and authz:check. |
ANALYST_USER_ID | The 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.
- lumo CLI
- curl
#!/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
#!/bin/sh
# Give a person a permission through a group, with curl: create a group, give
# it a role, add a member, and ask for the answer before and after.
#
# sh authorization/roles-through-groups/http/groups.sh
set -eu
PERMISSION="${PERMISSION:-invoices.approve}"
ADMIN="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1/admin"
# One call to the Admin API. The answer is not shown.
admin() {
method="$1"; path="$2"; shift 2
curl --silent --show-error --fail --output /dev/null --request "$method" "$ADMIN$path" \
--header "X-API-Key: $LUMOAUTH_API_KEY" --header "Content-Type: application/json" "$@"
}
# Asks whether the analyst holds the permission, and prints one line.
check() {
answer=$(curl --silent --show-error "$LUMOAUTH_URL/api/v1/authz/check" \
--header "X-API-Key: $LUMOAUTH_API_KEY" --header "Content-Type: application/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. A path takes the slug of a group in place of its ID.
admin POST /groups --data '{"name": "Finance", "slug": "finance", "description": "The finance team"}'
admin PUT /groups/finance/roles --data '{"roles": ["invoice-approver"]}'
admin POST /groups/finance/members --data "{\"userId\": \"$ANALYST_USER_ID\"}"
check "member of finance"
admin DELETE "/groups/finance/members/$ANALYST_USER_ID"
check "removed from finance"
# Delete the group, so that the script can run again.
admin DELETE /groups/finance
sh authorization/roles-through-groups/http/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
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.assignedfor the role of the group, thengroup.member_addedandgroup.member_removed. - A webhook endpoint that subscribes to
group.member_addedandgroup.member_removedreceives 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.assignedwhen a user gets a role directly. It sends no webhook when a group gets a role.