Users, RBAC, and AI Agent Tokens

phramecms/user (UserModule) provides PhrameCMS's deny-by-default authentication, role-based access control (RBAC), scoped AI Agent Tokens, and append-only audit logging. All state is stored in dedicated tables (phrame_users, phrame_roles, phrame_user_roles, phrame_agent_tokens, phrame_audit_log, phrame_sessions, phrame_user_one_time_tokens) installed by UserSchemaInstaller.

Actor Types and RbacPolicyEnforcer

UserModule binds PhrameCms\User\Security\RbacPolicyEnforcer as PolicyEnforcerInterface (and OperationVisibilityEnforcerInterface). Every OperationBus::execute() call evaluates authorization before input validation or handler execution according to the actor's ActorType:

Actor Type How Permissions Are Resolved Notes
ActorType::Human Union of permissions from all assigned Role entities plus any explicit scopes() on the actor Inactive users (active: false) are always denied immediately.
ActorType::Agent Strictly bound to the token's explicit scopes() allowlist Never inherits human role permissions, even when on_behalf_of points to an administrator with *.
ActorType::System Permitted only when $context['internal_system'] === true (RbacPolicyEnforcer::INTERNAL_SYSTEM_CONTEXT_KEY) Denied for all external callers.

This separation is a core AI safety invariant: attributing an AI Agent Token to a human administrator via on_behalf_of gives full accountability in the audit log without ever leaking the human's broad role permissions to the agent. Always scope tokens to the smallest subset of permissions the agent's task requires (and never broader than the supervising human's authority).

Granular Permission Syntax

RbacPolicyEnforcer::matchesPermission() evaluates dot-separated permission strings with strict boundary rules:

  • Exact permission: e.g., entity.content.article.read, config.admin.settings.write, proposal.create, audit.read.
  • Suffix segment wildcard (<prefix>.*): Matches any permission starting with <prefix>. across dot boundaries. For example, entity.content.* matches entity.content.article.read and entity.content.article.delete, but does not match entity.content_type.read.
  • Super-admin wildcard (*): Matches any valid permission (used by the default role_admin role).
  • Disallowed patterns: Partial-segment wildcards (e.g., entity.content.art*) and middle wildcards (e.g., entity.*.read) are rejected and never match when granted.

Common Official Permission Strings

Area Permissions
Content entities entity.<type>.<bundle>.read, entity.<type>.<bundle>.create, entity.<type>.<bundle>.update, entity.<type>.<bundle>.delete, entity.<type>.<bundle>.publish
Content schemas schema.read, schema.modify
Configuration config.read, config.write, config.<name>.read, config.<name>.write, config.export, config.import
Users, roles & tokens user.read, user.write, user.admin, role.read, role.admin, agent_token.read, agent_token.write
Audit trail audit.read
Change proposals proposal.read, proposal.create, proposal.review, plus propose.<permission> (see AI Change Proposals & Human Approval Queue)

RbacPolicyEnforcer::canDiscover() also uses each operation's discoveryPermissionPatterns (such as entity.*.*.read or config.*.write) to filter MCP tools/list output so agents only see tools their scopes can actually invoke.

Managing Users and Roles

Users and roles can be managed in /admin/users and /admin/roles, or via OperationBus operations (user.list, user.create, user.assign_role, role.list, role.create):

bash
# Create an editor role with content-only permissions
./bin/phrame op:run role.create --scopes=role.admin \
  --input='{"name":"editor","label":"Content Editor","permissions":["schema.read","entity.content.article.read","entity.content.article.create","entity.content.article.update"]}' \
  --json

# Create a human user account (passwords are hashed with PASSWORD_ARGON2ID)
./bin/phrame op:run user.create --scopes=user.write \
  --input='{"username":"alice","email":"alice@example.test","active":true,"role_ids":["role_<id>"]}' \
  --json

# Assign an additional role to an existing user
./bin/phrame op:run user.assign_role --scopes=user.admin \
  --input='{"user_id":"usr_<id>","role_id":"role_<id>"}'

For password resets and single-use Admin UI login links from the CLI, phramecms/skeleton also provides ./bin/phrame user:password <username-or-id> --generate (or --password-stdin) and ./bin/phrame user:login [username|email|id].

Issuing and Revoking AI Agent Tokens

PhrameCms\User\Security\AgentTokenManager generates random 256-bit bearer tokens prefixed with phm_agent_. Only the SHA-256 hash (token_hash) and a 12-character token_prefix are stored in phrame_agent_tokens; the plaintext_token is returned once at creation time and verified with hash_equals().

Requires agent_token.write. Supports --dry-run, optional on_behalf_of human attribution, and optional ISO-8601 expires_at:

bash
./bin/phrame op:run agent_token.create --scopes=agent_token.write \
  --input='{
    "name": "article-writer",
    "scopes": [
      "schema.read",
      "entity.content.article.read",
      "entity.content.article.create"
    ],
    "on_behalf_of": "usr_<admin-id>",
    "expires_at": "2027-01-01T00:00:00Z"
  }' --json

You can also mint and inspect tokens in the Admin UI at /admin/agent-tokens, or list them via agent_token.list (requires agent_token.read).

Option B: During Initial Bootstrap (site:install)

bash
./bin/phrame site:install \
  --issue-agent-token \
  --agent-name=readonly-inspector \
  --agent-scopes='schema.read,config.read,entity.content.article.read' \
  --json

Verify an Active Token (auth.whoami)

Any authenticated actor can call auth.whoami (MCP tool auth_whoami, zero extra permissions required) to inspect its resolved identity, actor_type, on_behalf_of, and granted scopes.

Revoking a Token (agent_token.revoke)

Revocation stamps revoked_at immediately. Subsequent requests presenting that token fail in AgentTokenManager::authenticate() with TokenRevokedException (HTTP 401 Unauthorized or Stdio MCP exit code 3):

bash
./bin/phrame op:run agent_token.revoke --scopes=agent_token.write \
  --input='{"token_id":"tok_<id>"}' --json

Immutable Audit Trail

Every OperationBus::execute() call — successful, replayed, dry-run, authorization-denied, or failed — emits an AuditRecord to AuditLoggerInterface, implemented by PhrameCms\User\Audit\DatabaseAuditLogger.

Each record in phrame_audit_log stores:

  • id, occurred_at (UTC), and correlation_id
  • actor_id, actor_type (human, agent, system), on_behalf_of, and session_id
  • operation, target, dry_run, and success
  • before_hash and after_hash
  • metadata (automatically redacted by DatabaseAuditLogger for any key containing password, secret, authorization, or token except safe identifiers token_id and token_prefix)

Query recent audit records via /admin/audit, MCP resource phrame://audit/recent, or the audit.list operation (requires audit.read):

bash
./bin/phrame op:run audit.list --scopes=audit.read \
  --input='{"limit":20,"operation":"config.import"}' --json