Role-Based Access Control (RBAC)
TopGun provides a flexible security model based on Permission Policies. You can define fine-grained access control rules for your data maps, controlling who can read (READ), write (PUT), or delete (REMOVE) data based on user roles.
Security by Default
TopGun Server denies all access by default. You must explicitly define policies to allow access to your maps.
Core Concepts
Principal
Represents the authenticated user. Contains a userId and a list of roles (e.g., ‘USER’, ‘ADMIN’).
Permission Policy
A rule that grants specific actions (READ, PUT, REMOVE) on a set of maps to a specific role.
Configuration
Policies are passed to the ServerCoordinator constructor.
import { ServerCoordinator } from '@topgunbuild/server';
import { PermissionPolicy } from '@topgunbuild/core';
const policies: PermissionPolicy[] = [
// 1. Public Read-Only Access
{
role: 'ANON',
mapNamePattern: 'public:*',
actions: ['READ']
},
// 2. User Private Data (Dynamic Pattern)
{
role: 'USER',
mapNamePattern: 'users:{userId}:*', // {userId} is replaced at runtime
actions: ['ALL']
},
// 3. Admin Full Access
{
role: 'ADMIN',
mapNamePattern: '*',
actions: ['ALL']
}
];
const server = new ServerCoordinator({
port: 8765,
securityPolicies: policies
}); Field-Level Security
You can restrict which fields are returned to the client using allowedFields.
This is useful for hiding sensitive data like password hashes or internal metadata.
{
role: 'USER',
mapNamePattern: 'users:*',
actions: ['READ'],
// Only allow reading public profile fields
allowedFields: ['username', 'displayName', 'avatarUrl']
} Field-level security currently applies only to READ operations. Write operations (PUT) validate permission to the map but do not yet validate the schema of the payload.