RBAC RoleBasedSecurity - spinningideas/resources GitHub Wiki

Role-based access control (RBAC)

Role-based access control (RBAC) is an authorization approach that assigns permissions to roles rather than directly to individual users. Users receive permissions by being assigned to one or more roles. This document describes the database schema, relational structure, and UI navigation integration for an enterprise RBAC system using universally unique identifiers (UUIDs) for primary and foreign keys.


Core RBAC data model

The core access control system manages identities, roles, privileges, and granular rights.

Entity descriptions

  • Account (User). Represents an authenticated identity in the system. An account can have one or more assigned roles.
  • ApplicationRole. Represents a named authorization group (such as ADMIN, BILLING_MANAGER, or SUPPORT_AGENT).
  • AccountRole. A junction table that assigns accounts to roles with active status flags and audit metadata.
  • ApplicationPrivilege. Represents a functional capability in the system (such as VIEW_INVOICES, MANAGE_USERS, or EXPORT_REPORTS).
  • RolePrivilege. A junction table that maps which privileges belong to each role.
  • ApplicationRight. Represents granular operations or actions (such as CREATE, READ, UPDATE, DELETE, or EXECUTE).
  • PrivilegeRight / ApplicationRolePrivilegeRight. Associates granular rights with privileges and roles.

Core RBAC entity relationship diagram

erDiagram
    ACCOUNT ||--o{ ACCOUNT_ROLE : "assigned"
    APPLICATION_ROLE ||--o{ ACCOUNT_ROLE : "has"
    APPLICATION_ROLE ||--o{ ROLE_PRIVILEGE : "grants"
    APPLICATION_PRIVILEGE ||--o{ ROLE_PRIVILEGE : "assigned to"
    APPLICATION_PRIVILEGE ||--o{ PRIVILEGE_RIGHT : "defines"
    APPLICATION_RIGHT ||--o{ PRIVILEGE_RIGHT : "scoped to"

    ACCOUNT {
        uuid account_id PK
        string email_alias
        string first_name
        string last_name
        boolean all_roles_ind
        boolean is_active
        timestamp created_at
        timestamp updated_at
    }

    ACCOUNT_ROLE {
        uuid account_id FK, PK
        string role_code FK, PK
        boolean is_active
        timestamp created_at
    }

    APPLICATION_ROLE {
        string role_code PK
        string role_name
        string role_description
        boolean all_users_ind
        boolean all_privileges_ind
        boolean is_active
    }

    ROLE_PRIVILEGE {
        string role_code FK, PK
        string privilege_code FK, PK
        boolean is_active
        timestamp created_at
    }

    APPLICATION_PRIVILEGE {
        string privilege_code PK
        string privilege_name
        string privilege_description
        boolean all_roles_ind
        boolean is_active
    }

    APPLICATION_RIGHT {
        string right_code PK
        string right_name
        string right_description
    }

    PRIVILEGE_RIGHT {
        string privilege_code FK, PK
        string right_code FK, PK
    }
Loading

Core SQL DDL

-- Enable pgcrypto or uuid-ossp for gen_random_uuid() in PostgreSQL
CREATE EXTENSION IF NOT EXISTS "pgcrypto";

-- 1. Account Table
CREATE TABLE account (
    account_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    email_alias VARCHAR(255) NOT NULL UNIQUE,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    all_roles_ind BOOLEAN NOT NULL DEFAULT FALSE,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100)
);

-- 2. Role Definition Table
CREATE TABLE application_role (
    role_code VARCHAR(50) PRIMARY KEY,
    role_name VARCHAR(100) NOT NULL,
    role_description TEXT,
    all_users_ind BOOLEAN NOT NULL DEFAULT FALSE,
    all_privileges_ind BOOLEAN NOT NULL DEFAULT FALSE,
    display_order INT DEFAULT 0,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100)
);

-- 3. Privilege Definition Table
CREATE TABLE application_privilege (
    privilege_code VARCHAR(50) PRIMARY KEY,
    privilege_name VARCHAR(100) NOT NULL,
    privilege_description TEXT,
    all_roles_ind BOOLEAN NOT NULL DEFAULT FALSE,
    all_rights_ind BOOLEAN NOT NULL DEFAULT FALSE,
    display_order INT DEFAULT 0,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100)
);

-- 4. Rights Definition Table
CREATE TABLE application_right (
    right_code VARCHAR(50) PRIMARY KEY,
    right_name VARCHAR(100) NOT NULL,
    right_description TEXT,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100)
);

-- 5. User to Role Assignment
CREATE TABLE account_role (
    account_id UUID NOT NULL REFERENCES account(account_id) ON DELETE CASCADE,
    role_code VARCHAR(50) NOT NULL REFERENCES application_role(role_code) ON DELETE CASCADE,
    description VARCHAR(255),
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100),
    PRIMARY KEY (account_id, role_code)
);

-- 6. Role to Privilege Mapping
CREATE TABLE role_privilege (
    role_code VARCHAR(50) NOT NULL REFERENCES application_role(role_code) ON DELETE CASCADE,
    privilege_code VARCHAR(50) NOT NULL REFERENCES application_privilege(privilege_code) ON DELETE CASCADE,
    description VARCHAR(255),
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100),
    PRIMARY KEY (role_code, privilege_code)
);

-- 7. Privilege to Right Mapping
CREATE TABLE privilege_right (
    privilege_code VARCHAR(50) NOT NULL REFERENCES application_privilege(privilege_code) ON DELETE CASCADE,
    right_code VARCHAR(50) NOT NULL REFERENCES application_right(right_code) ON DELETE CASCADE,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    PRIMARY KEY (privilege_code, right_code)
);

RBAC UI and dynamic menu system

Applications use RBAC permissions to render customized navigation trees. When a user logs in, the user interface queries the database to retrieve only the applications, modules, menu groups, and items the user has permission to view.

Menu hierarchy

The menu hierarchy organizes navigation into four distinct levels:

  1. Application. The top-level software product (such as Customer Portal or Admin Console).
  2. ApplicationMenuModule. Major feature areas within the application (such as Billing, User Management, or Analytics).
  3. ApplicationMenuItemGroup. Groupings of navigation links inside a module (such as Invoices & Receipts or System Settings).
  4. ApplicationMenuItem. The individual link or action (such as Create Invoice, Audit Logs, or Export Data).

Menu permissions integration

Each menu item or group links to an ApplicationPrivilege. When a user requests their menu:

  • The system resolves all active privileges granted to the user through their roles.
  • Menu items requiring a privilege the user lacks are filtered out.
  • Empty parent groups or modules that contain no accessible child items are automatically hidden.

Menu system entity relationship diagram

erDiagram
    APPLICATION ||--o{ APPLICATION_MENU_MODULE : "contains"
    APPLICATION_MENU_MODULE ||--o{ APPLICATION_MENU_ITEM_GROUP : "contains"
    APPLICATION_MENU_ITEM_GROUP ||--o{ APPLICATION_MENU_ITEM : "contains"
    APPLICATION_PRIVILEGE ||--o{ APPLICATION_MENU_ITEM : "secures"
    APPLICATION_PRIVILEGE ||--o{ APPLICATION_MENU_MODULE : "secures"

    APPLICATION {
        uuid application_id PK
        string application_code
        string application_name
        string application_description
        uuid parent_application_id FK
        int display_order
        boolean is_active
    }

    APPLICATION_MENU_MODULE {
        uuid application_menu_module_id PK
        uuid application_id FK
        string module_code
        string module_name
        string module_description
        string required_privilege_code FK
        uuid parent_module_id FK
        int display_order
        boolean is_active
    }

    APPLICATION_MENU_ITEM_GROUP {
        uuid menu_item_group_id PK
        uuid application_menu_module_id FK
        string group_link
        string group_text
        string group_description
        string required_privilege_code FK
        uuid parent_group_id FK
        int group_depth
        int display_order
        boolean is_active
    }

    APPLICATION_MENU_ITEM {
        uuid menu_item_id PK
        uuid menu_item_group_id FK
        string item_link
        string item_text
        string item_description
        string item_onclick_event
        string required_privilege_code FK
        uuid parent_item_id FK
        int item_depth
        int display_order
        boolean expanded_by_default
        boolean is_active
    }
Loading

Menu SQL DDL

-- 1. Application Table
CREATE TABLE application (
    application_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    application_code VARCHAR(50) NOT NULL UNIQUE,
    application_name VARCHAR(100) NOT NULL,
    application_description TEXT,
    parent_application_id UUID REFERENCES application(application_id),
    display_order INT DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN NOT NULL DEFAULT TRUE
);

-- 2. Menu Module Table
CREATE TABLE application_menu_module (
    application_menu_module_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    application_id UUID NOT NULL REFERENCES application(application_id) ON DELETE CASCADE,
    module_code VARCHAR(50) NOT NULL,
    module_name VARCHAR(100) NOT NULL,
    module_description TEXT,
    required_privilege_code VARCHAR(50) REFERENCES application_privilege(privilege_code),
    parent_module_id UUID REFERENCES application_menu_module(application_menu_module_id),
    display_order INT DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN NOT NULL DEFAULT TRUE
);

-- 3. Menu Item Group Table
CREATE TABLE application_menu_item_group (
    menu_item_group_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    application_menu_module_id UUID NOT NULL REFERENCES application_menu_module(application_menu_module_id) ON DELETE CASCADE,
    group_link VARCHAR(255),
    group_text VARCHAR(100) NOT NULL,
    group_description TEXT,
    required_privilege_code VARCHAR(50) REFERENCES application_privilege(privilege_code),
    parent_group_id UUID REFERENCES application_menu_item_group(menu_item_group_id),
    group_depth INT DEFAULT 1,
    display_order INT DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN NOT NULL DEFAULT TRUE
);

-- 4. Menu Item Table
CREATE TABLE application_menu_item (
    menu_item_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    menu_item_group_id UUID NOT NULL REFERENCES application_menu_item_group(menu_item_group_id) ON DELETE CASCADE,
    item_link VARCHAR(255) NOT NULL,
    item_text VARCHAR(100) NOT NULL,
    item_description TEXT,
    item_onclick_event VARCHAR(100),
    required_privilege_code VARCHAR(50) REFERENCES application_privilege(privilege_code),
    parent_item_id UUID REFERENCES application_menu_item(menu_item_id),
    item_depth INT DEFAULT 1,
    display_order INT DEFAULT 0,
    expanded_by_default BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN NOT NULL DEFAULT TRUE
);

TypeScript types for the UI navigation system

These TypeScript interfaces model the menu data in frontend applications using string representations of UUIDs:

export interface UserPermissionContext {
  accountId: string;
  emailAlias: string;
  roles: string[];
  privileges: string[];
  isSuperAdmin: boolean;
}

export interface MenuItem {
  menuItemId: string;
  itemText: string;
  itemLink: string;
  itemDescription?: string;
  itemOnClickEvent?: string;
  requiredPrivilegeCode?: string;
  displayOrder: number;
  expandedByDefault: boolean;
  children?: MenuItem[];
}

export interface MenuItemGroup {
  menuItemGroupId: string;
  groupText: string;
  groupLink?: string;
  groupDescription?: string;
  requiredPrivilegeCode?: string;
  displayOrder: number;
  items: MenuItem[];
}

export interface MenuModule {
  menuModuleId: string;
  moduleCode: string;
  moduleName: string;
  moduleDescription?: string;
  requiredPrivilegeCode?: string;
  displayOrder: number;
  groups: MenuItemGroup[];
}

export interface ApplicationMenu {
  applicationId: string;
  applicationCode: string;
  applicationName: string;
  modules: MenuModule[];
}

SQL query: Fetching authorized menu items for a user

This query returns all menu items accessible by a specific user account:

WITH user_privileges AS (
    SELECT DISTINCT p.privilege_code
    FROM account a
    JOIN account_role ar 
        ON a.account_id = ar.account_id 
        AND ar.is_active = TRUE
    JOIN application_role r 
        ON (ar.role_code = r.role_code OR r.all_users_ind = TRUE OR a.all_roles_ind = TRUE)
        AND r.is_active = TRUE
    JOIN role_privilege rp 
        ON r.role_code = rp.role_code 
        AND rp.is_active = TRUE
    JOIN application_privilege p 
        ON (rp.privilege_code = p.privilege_code OR r.all_privileges_ind = TRUE OR p.all_roles_ind = TRUE)
        AND p.is_active = TRUE
    WHERE a.account_id = :target_account_id::uuid
      AND a.is_active = TRUE
)
SELECT 
    app.application_name,
    mod.module_name,
    grp.group_text,
    item.menu_item_id,
    item.item_text,
    item.item_link,
    item.display_order
FROM application app
JOIN application_menu_module mod 
    ON app.application_id = mod.application_id 
    AND mod.is_active = TRUE
JOIN application_menu_item_group grp 
    ON mod.application_menu_module_id = grp.application_menu_module_id 
    AND grp.is_active = TRUE
JOIN application_menu_item item 
    ON grp.menu_item_group_id = item.menu_item_group_id 
    AND item.is_active = TRUE
WHERE app.is_active = TRUE
  -- Filter by required privileges if specified
  AND (mod.required_privilege_code IS NULL OR mod.required_privilege_code IN (SELECT privilege_code FROM user_privileges))
  AND (grp.required_privilege_code IS NULL OR grp.required_privilege_code IN (SELECT privilege_code FROM user_privileges))
  AND (item.required_privilege_code IS NULL OR item.required_privilege_code IN (SELECT privilege_code FROM user_privileges))
ORDER BY 
    app.display_order,
    mod.display_order,
    grp.display_order,
    item.display_order;

React menu component example

This React component illustrates how a client application filters and renders the authorized menu tree:

import React from 'react';
import { ApplicationMenu, UserPermissionContext } from './types';

interface NavigationSidebarProps {
  menu: ApplicationMenu;
  userContext: UserPermissionContext;
}

export const NavigationSidebar: React.FC<NavigationSidebarProps> = ({ menu, userContext }) => {
  const hasAccess = (privilegeCode?: string) => {
    if (!privilegeCode || userContext.isSuperAdmin) {
      return true;
    }
    return userContext.privileges.includes(privilegeCode);
  };

  return (
    <aside className="navigation-sidebar">
      <h2>{menu.applicationName}</h2>
      <nav>
        {menu.modules
          .filter(module => hasAccess(module.requiredPrivilegeCode))
          .map(module => {
            const accessibleGroups = module.groups.filter(group => hasAccess(group.requiredPrivilegeCode));
            if (accessibleGroups.length === 0) return null;

            return (
              <div key={module.menuModuleId} className="menu-module">
                <h3>{module.moduleName}</h3>
                {accessibleGroups.map(group => {
                  const accessibleItems = group.items.filter(item => hasAccess(item.requiredPrivilegeCode));
                  if (accessibleItems.length === 0) return null;

                  return (
                    <div key={group.menuItemGroupId} className="menu-group">
                      <span className="group-heading">{group.groupText}</span>
                      <ul>
                        {accessibleItems.map(item => (
                          <li key={item.menuItemId}>
                            <a href={item.itemLink}>{item.itemText}</a>
                          </li>
                        ))}
                      </ul>
                    </div>
                  );
                })}
              </div>
            );
          })}
      </nav>
    </aside>
  );
};
⚠️ **GitHub.com Fallback** ⚠️