Small 0 dependencies TypeScript library for managing and checking user permissions
npm install ts-can
import { Permissions, TestRules, checkPermissions, canAllow } from 'permissions-library';
// Define user permissions
const userPermissions: Permissions = {
'moduleA': {
abilities: { read: true, write: false },
checks: { isAdmin: (target) => target.isAdmin }
},
'moduleB': {
abilities: { read: true, write: true },
checks: { hasAccess: (target) => target.isValidUser }
},
// ... other modules
};
// Define test rules
const testRules: TestRules = {
'moduleA': { abilities: ['read'] },
'moduleB': { checks: ['hasAccess'], target: { isValidUser: true } },
// ... other test rules
};
// Check if user has required permissions for all test rules
const result = canAllow(userPermissions, testRules);
console.log(result); // Output: true
Please refer to the official documentation for detailed information about the API and usage examples.
Generated using TypeDoc