npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-rbac

v1.2.9

Published

Prisma middleware that handles Role-Based Access Control based on Express server context

Downloads

57

Readme

npm npm HitCount NPM Version codecov

Prisma-RBAC

Prisma-RBAC is a role-based access control (RBAC) library designed to integrate with Prisma, providing a tool to manage user permissions. This library enforces access control policies at a granular level for each model and action.

It also allows defining permissions on a per-user basis. This makes it suitable for building applications that require detailed security. With customizable configurations and support for model synonyms, it ensures that your application's data access policies are consistently enforced.

Features

  • Easy Integration: You can add this library to your Prisma client without much hassle, making it easy to start using role-based access control in your application.
  • Fine-Grained Control: This feature lets you set up detailed permissions for different actions on different models. For example, you can allow some users to read data but not update it, while others can do both.
  • Mismatch Handler: If the permissions in your application don't match the current settings (maybe because of changes in production), this feature helps identify and fix those issues, making maintenance easier.
  • Support for Synonyms: In Prisma schemas, relationships between models can have different names. For example, you might have a model note that is referred to as notes in another model's relationship. This feature manages permissions for these synonyms.
  • I18n: Pass translation function to get human readable error messages.

Installation

To install Prisma-RBAC, use your preferred package manager:

npm install prisma-rbac
# or
yarn add prisma-rbac

Usage

  1. Authenticate User: When a user token is authenticated, retrieve the user from the database along with their permissions.
  2. Extend Prisma Client: Enhance the Prisma client with role-based access control for the current request.
import { PrismaClient } from '@prisma/client';
import { applyRBAC } from 'prisma-rbac';

// Define your role-based permissions
const permissions = {
  user: {
    create: true,
    read: true,
    update: false,
    delete: false,
  },
  note: {
    create: true,
    read: true,
    update: true,
    delete: false,
  },
};

// Mock function to simulate fetching a user with permissions from the database
async function getUserWithPermissions(userId: number) {
  // Simulate a user with specific permissions
  return {
    id: userId,
    email: '[email protected]',
    permissions,
  };
}

// Initialize Prisma Client
const prisma = new PrismaClient();

// Middleware to enhance Prisma with RBAC
async function addRBACToRequest(req, res, next) {
  const user = await getUserWithPermissions(req.userId);
  req.prisma = applyRBAC({
    prisma,
    permissions: user.permissions,
    restrictedModels: ['user', 'note'],
    allowedActions: ['note:read'],
    synonyms: { note: ['notes'] },
    mismatchHandler: (mismatch) => console.warn('Mismatched permissions:', mismatch),
  });
  next();
}

// Example usage in an Express route
app.use(addRBACToRequest);

app.get('/notes', async (req, res) => {
  try {
    const notes = await req.prisma.note.findMany();
    res.json(notes);
  } catch (error) {
    res.status(403).json({ error: error.message });
  }
});

app.post('/notes', async (req, res) => {
  try {
    const note = await req.prisma.note.create({
      data: {
        title: 'New Note',
        body: 'Note content',
        userId: req.userId,
      },
    });
    res.json(note);
  } catch (error) {
    res.status(403).json({ error: error.message });
  }
});

Configuration Options

| Option | Type | Description | |--------------------|-------------------------------|--------------------------------------------------------------------------------------------------| | permissions | ResourcePermissions \| null \| undefined | Defines the role-based permissions for models and actions. | | restrictedModels | Array<string> | Specifies the models that require RBAC checks. | | allowedActions | Array<string> | Lists the operations that are publicly accessible without RBAC checks. | | synonyms | Record<string, string[]> | Maps model synonyms to their actual model names, useful for handling schema relation mismatches. | | mismatchHandler | (mismatch: string[]) => void| Handles cases where users don't have permissions or permission models have changed, simplifying maintenance and fixing discrepancies. | | translate | (key: TranslationKeys, options?: Record<string, unknown>) => string | Optional i18next function to return access errors in human language. |

i18n

The i18n should have structure similar to this to work:

{
  "errors": {
    "noPermission": "คุณไม่มีสิทธิ์ในการดำเนินการ {{operation}} บน {{model}}."
  },
  "operations": {
    "read": "อ่าน",
    "create": "สร้าง",
    "update": "อัปเดต",
    "delete": "ลบ"
  },
  "models": {
    "User": "ผู้ใช้",
    "Post": "โพสต์",
    "Comment": "ความคิดเห็น"
  }
}

Performance

Prisma-RBAC is optimized for performance, ensuring minimal overhead on your application's data access operations. Internal functions have been benchmarked to execute quickly (~0.003 ms on local test), even under heavy load.

Contributing

If you have suggestions, bug reports, or feature requests, please open an issue or submit a pull request on our GitHub repository.

License

Prisma-RBAC is licensed under the MIT License. See the LICENSE file for more information.

Keywords

Prisma, RBAC, role-based access control, permissions, access control, security, Node.js, TypeScript