Skip to main content

Permissions System

The permissions package in @repo/crud provides a comprehensive access control system for CRUD operations. It allows you to define who can perform which operations on which resources.

Features

  • Fine-Grained Control: Define permissions at the resource and operation level
  • Dynamic Permissions: Permissions can be based on the user, resource, and data
  • Role-Based Access Control: Support for role-based permissions
  • Attribute-Based Access Control: Support for attribute-based permissions
  • Flexible API: Easy to integrate with your authentication system
  • Caching: Efficient permission checking with caching
  • TypeScript Support: Full TypeScript support with generics

Basic Usage

Setting Up Permissions

Setting the Current User

Before performing CRUD operations, you need to set the current user:

Integration with Authentication

You can integrate the permissions system with your authentication system:

Permission Types

Boolean Permissions

The simplest form of permissions is a boolean value:

Function Permissions

For more complex permissions, you can use functions:

Data-Dependent Permissions

You can also define permissions that depend on the data being created or updated:

Permission Providers

Creating a Permissions Provider

You can create a permissions provider to fetch permissions dynamically:

Caching Permissions

The permissions provider supports caching to improve performance:

Role-Based Access Control (RBAC)

Defining Role-Based Permissions

You can define permissions based on roles:

Multiple Roles

You can support users with multiple roles:

Attribute-Based Access Control (ABAC)

Defining Attribute-Based Permissions

You can define permissions based on attributes of the user, resource, and environment:

Environment-Based Permissions

You can also include environment factors in your permissions:

UI Integration

Conditional Rendering

You can use the permissions system to conditionally render UI elements:

usePermissions Hook

The permissions package provides a hook for checking permissions in components:

Advanced Usage

Custom Permission Checks

You can perform custom permission checks:

Permission Middleware

You can create middleware for your API routes to check permissions:

Permission Inheritance

You can implement permission inheritance for resources:

API Reference

For a complete API reference, please refer to the TypeScript definitions in the package.