Claude
Skills
Sign in
โ† Back

role-permission-table-builder

Included with Lifetime
$97 forever

Generates comprehensive role-based permission matrices in markdown or SQL format for pages, components, and data access patterns. This skill should be used when designing authorization systems, documenting permissions, creating RBAC tables, or planning access control. Use for RBAC, role permissions, access control, authorization matrix, permission mapping, or security policies.

Backend & APIs

What this skill does


# Role Permission Table Builder

Generate and maintain comprehensive role-based access control (RBAC) permission matrices for worldbuilding applications.

## Overview

To build role permission systems:

1. Define user roles and hierarchies
2. Identify protected resources (pages, components, data, actions)
3. Create permission matrices mapping roles to resources
4. Generate implementation code for middleware and components
5. Document permission policies for team reference

## Role Definitions

### Standard Roles

Define common application roles:

- **Guest**: Unauthenticated users (read-only public content)
- **User**: Basic authenticated users (read own data, limited writes)
- **Creator**: Content creators (create/edit entities, manage own content)
- **Editor**: Content editors (edit any content, moderate submissions)
- **Admin**: System administrators (full access, user management)
- **Super Admin**: Platform owners (all permissions, system configuration)

### Custom Roles

To define worldbuilding-specific roles:

- **World Owner**: Creator of worldbuilding project
- **Collaborator**: Invited contributor to world
- **Viewer**: Read-only access to private world
- **Game Master**: Special permissions for RPG campaigns
- **Publisher**: Can publish worlds publicly

Consult `references/role-hierarchy.md` for role inheritance patterns.

## Permission Matrix

### Page-Level Permissions

To define page access:

| Page Route            | Guest | User | Creator | Editor | Admin |
| --------------------- | ----- | ---- | ------- | ------ | ----- |
| /                     | [OK]     | [OK]    | [OK]       | [OK]      | [OK]     |
| /login                | [OK]     | [OK]    | [OK]       | [OK]      | [OK]     |
| /dashboard            | [ERROR]     | [OK]    | [OK]       | [OK]      | [OK]     |
| /worlds/create        | [ERROR]     | [OK]    | [OK]       | [OK]      | [OK]     |
| /worlds/[id]          | ๐Ÿ”’     | ๐Ÿ”’    | [OK]       | [OK]      | [OK]     |
| /worlds/[id]/edit     | [ERROR]     | [ERROR]    | [OK]       | [OK]      | [OK]     |
| /admin/users          | [ERROR]     | [ERROR]    | [ERROR]       | [ERROR]      | [OK]     |
| /admin/settings       | [ERROR]     | [ERROR]    | [ERROR]       | [ERROR]      | [OK]     |

Legend:
- [OK] Full access
- ๐Ÿ”’ Conditional access (ownership/invitation)
- [ERROR] No access

### Component-Level Permissions

To define component visibility:

| Component              | Guest | User | Creator | Editor | Admin |
| ---------------------- | ----- | ---- | ------- | ------ | ----- |
| WorldList              | [OK] Public | [OK] All | [OK] All | [OK] All | [OK] All |
| WorldCreateButton      | [ERROR]     | [OK]    | [OK]       | [OK]      | [OK]     |
| EntityEditor           | [ERROR]     | [ERROR]    | [OK] Own   | [OK] All  | [OK] All |
| DeleteButton           | [ERROR]     | [ERROR]    | [OK] Own   | [OK] All  | [OK] All |
| ShareWorldButton       | [ERROR]     | [ERROR]    | [OK] Own   | [OK] All  | [OK] All |
| AdminPanel             | [ERROR]     | [ERROR]    | [ERROR]       | [ERROR]      | [OK]     |
| UserManagement         | [ERROR]     | [ERROR]    | [ERROR]       | [ERROR]      | [OK]     |

### Data Access Permissions

To define data operations:

| Operation                   | Guest | User     | Creator  | Editor | Admin |
| --------------------------- | ----- | -------- | -------- | ------ | ----- |
| Read public worlds          | [OK]     | [OK]        | [OK]        | [OK]      | [OK]     |
| Read private worlds         | [ERROR]     | ๐Ÿ”’ Invited | ๐Ÿ”’ Own    | [OK]      | [OK]     |
| Create world                | [ERROR]     | [OK]        | [OK]        | [OK]      | [OK]     |
| Update own world            | [ERROR]     | [OK]        | [OK]        | [OK]      | [OK]     |
| Update any world            | [ERROR]     | [ERROR]        | [ERROR]        | [OK]      | [OK]     |
| Delete own world            | [ERROR]     | [OK]        | [OK]        | [OK]      | [OK]     |
| Delete any world            | [ERROR]     | [ERROR]        | [ERROR]        | [ERROR]      | [OK]     |
| Create entity               | [ERROR]     | [ERROR]        | [OK]        | [OK]      | [OK]     |
| Update own entity           | [ERROR]     | [ERROR]        | [OK]        | [OK]      | [OK]     |
| Update any entity           | [ERROR]     | [ERROR]        | [ERROR]        | [OK]      | [OK]     |
| Delete entity               | [ERROR]     | [ERROR]        | [OK] Own    | [OK]      | [OK]     |
| Manage collaborators        | [ERROR]     | [ERROR]        | [OK] Own    | [OK]      | [OK]     |
| Publish world               | [ERROR]     | [ERROR]        | [OK] Own    | [OK]      | [OK]     |
| Moderate content            | [ERROR]     | [ERROR]        | [ERROR]        | [OK]      | [OK]     |
| Manage users                | [ERROR]     | [ERROR]        | [ERROR]        | [ERROR]      | [OK]     |

### Action Permissions

To define Server Action permissions:

| Server Action          | Guest | User | Creator | Editor | Admin |
| ---------------------- | ----- | ---- | ------- | ------ | ----- |
| createWorld            | [ERROR]     | [OK]    | [OK]       | [OK]      | [OK]     |
| updateWorld            | [ERROR]     | ๐Ÿ”’    | ๐Ÿ”’       | [OK]      | [OK]     |
| deleteWorld            | [ERROR]     | ๐Ÿ”’    | ๐Ÿ”’       | [ERROR]      | [OK]     |
| createEntity           | [ERROR]     | [ERROR]    | [OK]       | [OK]      | [OK]     |
| updateEntity           | [ERROR]     | [ERROR]    | ๐Ÿ”’       | [OK]      | [OK]     |
| deleteEntity           | [ERROR]     | [ERROR]    | ๐Ÿ”’       | [OK]      | [OK]     |
| inviteCollaborator     | [ERROR]     | [ERROR]    | ๐Ÿ”’       | [OK]      | [OK]     |
| publishWorld           | [ERROR]     | [ERROR]    | ๐Ÿ”’       | [OK]      | [OK]     |
| deleteUser             | [ERROR]     | [ERROR]    | [ERROR]       | [ERROR]      | [OK]     |

Use `scripts/generate_permission_matrix.py` to create customized permission tables.

## SQL Schema

To implement permissions in database:

```sql
-- Roles table
CREATE TABLE roles (
  id SERIAL PRIMARY KEY,
  name VARCHAR(50) UNIQUE NOT NULL,
  description TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Permissions table
CREATE TABLE permissions (
  id SERIAL PRIMARY KEY,
  resource VARCHAR(100) NOT NULL,
  action VARCHAR(50) NOT NULL,
  description TEXT,
  UNIQUE(resource, action)
);

-- Role permissions mapping
CREATE TABLE role_permissions (
  id SERIAL PRIMARY KEY,
  role_id INTEGER REFERENCES roles(id) ON DELETE CASCADE,
  permission_id INTEGER REFERENCES permissions(id) ON DELETE CASCADE,
  UNIQUE(role_id, permission_id)
);

-- User roles
CREATE TABLE user_roles (
  id SERIAL PRIMARY KEY,
  user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
  role_id INTEGER REFERENCES roles(id) ON DELETE CASCADE,
  assigned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(user_id, role_id)
);

-- Resource ownership
CREATE TABLE resource_ownership (
  id SERIAL PRIMARY KEY,
  user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
  resource_type VARCHAR(50) NOT NULL,
  resource_id INTEGER NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(resource_type, resource_id)
);

-- Collaborators (for conditional access)
CREATE TABLE collaborators (
  id SERIAL PRIMARY KEY,
  world_id INTEGER REFERENCES worlds(id) ON DELETE CASCADE,
  user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
  role VARCHAR(50) NOT NULL,
  invited_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(world_id, user_id)
);
```

Use `scripts/generate_rbac_schema.py` to generate database schema with seed data.

Reference `assets/rbac-schema.sql` for complete schema with indexes and constraints.

## Implementation

### Middleware Protection

To protect routes with middleware:

```typescript
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getSession } from '@/lib/auth';
import { checkPermission } fro

Related in Backend & APIs