Define and check permissions in code
Your application decides what its users may do. FlowCatalyst holds who has which role; your code holds what each role allows and checks it. Both halves live in your code: you declare the permissions and roles, sync them so the platform (and administrators) know them, and check permissions at runtime. Permissions and roles explains the rules behind this page.
Name things
Section titled “Name things”- A permission has four segments:
application:context:aggregate:action, for exampleorders:sales:order:cancel. The first segment is your application’s code; the platform refuses a role that holds another application’s permission (PERMISSION_OUTSIDE_APPLICATION). - A role is
application:name, for exampleorders:dispatcher. You declare the short name; sync adds the prefix and lower-cases it. - A held permission may use
*for whole segments (orders:sales:*:*). Checks match segment by segment, soorders:sales:order:*satisfiesorders:sales:order:cancel.
1. Declare permissions and roles
Section titled “1. Declare permissions and roles”set := sync.ForApplication("orders"). AddRole(sync.MakeRole("dispatcher"). WithDisplayName("Dispatcher"). WithDescription("Plans and cancels deliveries"). WithPermissions("orders:sales:order:view", "orders:sales:order:cancel"). ClientManagedEnabled()). // client administrators may assign it AddRole(sync.MakeRole("viewer").WithPermissions("orders:sales:order:view"))var set = Definitions.DefinitionSet.define("orders") .withRoles(List.of( Definitions.Role.of("dispatcher").withPermissions(List.of( Definitions.Permission.of("sales", "order", "view"), Definitions.Permission.of("sales", "order", "cancel"))), Definitions.Role.of("viewer").withPermissions(List.of( Definitions.Permission.of("sales", "order", "view")))));import { sync } from "@flowcatalyst/sdk";
const ViewOrders = sync.permission({ context: "sales", aggregate: "order", action: "view" });const CancelOrders = sync.permission({ context: "sales", aggregate: "order", action: "cancel", description: "Cancel an order" });
const definitions = sync .defineApplication("orders") .withPermissions([ViewOrders, CancelOrders]) // the catalogue, for reuse .withRoles([ { name: "dispatcher", displayName: "Dispatcher", permissions: [ViewOrders, CancelOrders], clientManaged: true }, { name: "viewer", permissions: [ViewOrders] }, ]) .build();use FlowCatalyst\Attributes\AsPermission;
// the application segment defaults to FLOWCATALYST_APP_CODE: "orders:sales:order:cancel"#[AsPermission(context: 'sales', aggregate: 'order', action: 'cancel', description: 'Cancel an order')]final class CancelOrders {}use FlowCatalyst\Attributes\AsRole;
#[AsRole(name: 'dispatcher', displayName: 'Dispatcher', permissions: [ViewOrders::class, CancelOrders::class], clientManaged: true)]final class DispatcherRole {}A permission class is declared once and linked from any number of roles. Roles may also list
plain 'orders:sales:order:view' strings.
let set = DefinitionSet::for_application("orders") .add_role(RoleDefinition::make("orders:dispatcher") .with_display_name("Dispatcher") .with_permissions(vec!["orders:sales:order:view".into(), "orders:sales:order:cancel".into()]) .with_client_managed(true));Client-managed roles. A role marked client-managed may be assigned by a client’s own administrators to users of their client (a client may only be given roles of applications it is entitled to). Leave it off for roles only your own staff should hand out.
2. Sync
Section titled “2. Sync”Sync the set with the rest of your definitions on each deploy
(php artisan flowcatalyst:scan && php artisan flowcatalyst:sync in Laravel, which also seeds
Spatie’s tables when spatie/laravel-permission is installed). Permissions have no endpoint of
their own: they reach the platform on the roles that grant them, and the console shows them under
each role.
Role sync is all or nothing (one bad role refuses the payload). With remove-unlisted, a role you
deleted from code is removed, unless it is still assigned to someone: then the sync is refused
(ROLE_HAS_ASSIGNMENTS) rather than silently taking access away.
3. Assign roles
Section titled “3. Assign roles”People get roles from administrators (the console’s Users page), from your application’s own user management (create users and invitations), from principal sync, or from their organisation’s identity provider at sign-in. Assigning a name that is not a role is refused.
4. Check permissions at runtime
Section titled “4. Check permissions at runtime”Where the permissions come from depends on how the caller arrived:
| Caller | Carries | Permissions |
|---|---|---|
| A person signed in to your app | The id_token’s roles (your application’s only) |
Mapped by your code from roles |
| A service calling your API with a platform access token | scope (the granted permissions), roles |
Read from scope |
// the same map as the roles you syncconst rbac = defineRbac() .role("orders:dispatcher").grants("orders:sales:order:view", "orders:sales:order:cancel") .role("orders:viewer").grants("orders:sales:order:view") .build();// pass { rbac } to flowcatalystAuth, then in a route:if (!req.principal!.hasPermissionTo(["orders:sales:order:cancel"])) { throw app.httpErrors.forbidden();}req.principal!.hasAnyPermissionTo(["orders:sales:order:cancel", "orders:sales:order:view"]);req.principal!.hasRole("orders:dispatcher");Wildcards are suffixes at segment boundaries: orders:* matches orders:sales:order:view.
$user->hasPermissionTo('orders:sales:order:cancel');$user->hasRole('orders:dispatcher');$user->can('orders:sales:order:cancel'); // Gate, @can and policies route here@can('orders:sales:order:cancel') <button>Cancel order</button>@endcanIn local-sync mode permissions come from Spatie’s tables, filled at sign-in from each role’s
permissions. In stateless mode bind an RbacCatalogue
(RbacBuilder::make()->role('orders:dispatcher')->grants([...])->build()). For bearer callers
(auth:fc-token) permissions come from the token’s scope, or from /api/me with
FLOWCATALYST_OIDC_PERMISSION_RESOLVER=api_me. In a policy, type-hint Authenticatable, not your
Eloquent User, or the check silently denies a token or session principal.
With the application framework, map roles to permissions once and guard routes:
auth.Permissions(auth.ScopesMapper().And(auth.ByRole(map[string][]string{ "orders:dispatcher": {"orders:sales:order:view", "orders:sales:order:cancel"},})))// route middleware answering 403 FORBIDDEN without it:auth.Require("orders:sales:order:cancel")ScopesMapper takes a bearer token’s scope; ByRole adds the permissions of each role a
session user holds.
With the application framework:
AuthModule.builder() .permissions(PermissionMapper.scopes().and(PermissionMapper.byRole(Map.of( "orders:dispatcher", Set.of("orders:sales:order:view", "orders:sales:order:cancel")))))Routes declared with RouteOptions.requiresContext() get a Context whose permissions you check
(see sessions and login).
Keep the runtime map and the synced roles in one place where you can (for example, generate the
defineRbac() catalogue from the same constants as the synced roles), so a role never means one
thing on the platform and another in your checks.
Related
Section titled “Related”- Permissions and roles and tokens.
- Use FlowCatalyst as your app’s identity provider.
- Permissions catalogue and built-in roles: the platform’s own.
- Users, roles and delegation: assigning roles in the console.
- Decision: canonical role names.