Official Dynamic Database-Backed RBAC Plugin for Payload CMS #16899
Mhmod-Hsn
started this conversation in
Feature Requests & Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Introduction
Hi Payload Team and Community! 👋
Following up on the work with audit logs, I’ve been working on another plugin to solve a very common architectural challenge in enterprise production environments: dynamic, database-backed Role-Based Access Control (RBAC).
While Payload provides excellent code-based access control, real-world applications often require administrators to create roles, define custom permissions, and assign them to users on the fly directly from the Admin UI, without redeploying code or modifying source files.
I’d love to share the plugin I built, gather feedback, and see if there is interest in listing it as a recommended community plugin or adapting parts of its architecture into the official Payload ecosystem!
The Plugin: payload-rbac-plugin
The goal of this plugin is to deliver a robust, database-backed RBAC system that is simple to configure, clean, and has zero database overhead during runtime access checks.
Core Features:
RolesandPermissionscollections.rolesrelationship field into your designated auth collection (e.g.,users). This field is configured withsaveToJWT: true, caching roles and permissions in the user's JWT to ensure runtime authorization checks require zero additional database queries.posts) and check CRUD operations to instantly generate separate permissions (e.g.,posts:create,posts:read,posts:update,posts:delete) in one save.RolesandPermissionsschemas viarolesFieldsandpermissionsFieldsoptions.RolesandPermissionscollections dynamically based on the current user's permissions, ensuring security for the RBAC panel itself.checkPermission(an access control higher-order function) andhasPermission(a function to check a user's permissions manually).Example Usage
1. Register the Plugin
Configure the plugin in your
payload.config.ts:2. Protect Collections (Access Control)
Apply standard Payload access control using the
checkPermissionhelper:3. Verification in Custom Endpoints or Hooks
For custom routes, hooks, or conditional logic, verify permissions programmatically:
Why this belongs in the Payload Ecosystem:
posts:create) and mapping them dynamically to roles database-side means you don't need to change your codebase or redeploy when user roles shift.Links
I’d love to hear your thoughts and suggestions on this implementation!
Beta Was this translation helpful? Give feedback.
All reactions