wowprompt

Role-based access control with a permission matrix and middleware

Designs role-based access control for an app from a plain list of users and actions: a permission matrix, the database tables, middleware that checks every request, and tests that prove a user cannot reach what their role forbids. Use it when an admin panel or a team app grows past a single "is admin" flag.

A roles and permissions matrix, a database schema for roles, middleware code that denies by default, and test cases for allowed and forbidden requests
The output this prompt produced, unedited.

The prompt

Fill in these first

  • Language and framework: [e.g. PHP 8.3 with Laravel, or Node.js with Express]
  • Database: [e.g. MySQL 8]
  • Roles needed: [e.g. owner, admin, editor, viewer, support agent]
  • Resources and actions: [e.g. articles: create, edit own, edit any, publish, delete; users: invite, change role; billing: view, change plan]
  • Ownership rules: [e.g. an editor may edit only articles they created]
  • Current auth: [e.g. session login with a users table, no roles yet]

Task

Build access control that denies by default and grants only what the matrix allows, including ownership checks.

Output format

  1. Permission matrix: | Permission | owner | admin | editor | viewer | ... |, with permissions named as resource.action.
  2. Schema: roles, permissions, role_permissions and user_roles tables, with keys and indexes, plus a migration.
  3. Seed data that loads the matrix.
  4. A single authorise function or policy class: given a user, a permission and an optional resource, return allow or deny, checking ownership where the matrix says "own".
  5. Middleware or route guard that calls it on every protected route and returns 403 with a plain message on deny.
  6. A cached lookup of a user's permissions per request, and how the cache clears when a role changes.
  7. Tests: at least one allowed and one forbidden request per role, plus the ownership edge case and a user with no role.

Rules

  • Deny by default: a route with no permission set returns 403, never 200.
  • Check permissions on the server for every action; hiding a button in the UI is not access control.
  • Role changes are logged with who changed what and when.
  • Never let a user grant a role higher than their own.
role based access controlrbacpermission matrixauthorization middlewareuser roles and permissionsaccess control tests

How to run this

Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.

The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.

Related prompts

All productivity