Table of Contents
Basic Permissions Class
A simple class to manage permissions
Basic permissions example with bitwise
Class
/**
* User permission class.
*/
export class Permission {
private permissions: bigint;
public static readonly PERMISSIONS = {
NONE: 0n,
READ: 1n << 0n,
WRITE: 1n << 1n,
ADMIN: 1n << 2n,
OWNER: 1n << 3n
// Add more permissions here by increasing the last number
} as const;
constructor(permission: bigint = 0n) {
this.permissions = permission;
}
private toBitMask(keys: PermissionKey[]): bigint {
return keys.reduce((acc, key) => acc | Permission.PERMISSIONS[key], 0n);
}
getPermissions(): { string: string; array: PermissionKey[] } {
const array = Object.entries(Permission.PERMISSIONS)
.filter(([, value]) => (this.permissions & value) !== 0n)
.map(([key]) => key as PermissionKey);
return { string: this.permissions.toString(), array };
}
setPermissions(keys: PermissionKey[]) {
this.permissions = this.toBitMask(keys);
return this.getPermissions();
}
removePermissions(keys: PermissionKey[]) {
this.permissions &= ~this.toBitMask(keys);
return this.getPermissions();
}
addPermissions(keys: PermissionKey[]) {
this.permissions |= this.toBitMask(keys);
return this.getPermissions();
}
hasAnyPermission(keys: PermissionKey[]): boolean {
return (this.permissions & this.toBitMask(keys)) !== 0n;
}
hasAllPermissions(keys: PermissionKey[]): boolean {
const required = this.toBitMask(keys);
return (this.permissions & required) === required;
}
static getPermissionKeys(): PermissionKey[] {
return Object.keys(Permission.PERMISSIONS) as PermissionKey[];
}
// Helper method to create Permission from database string
static fromString(permissionString: string): Permission {
return new Permission(BigInt(permissionString));
}
}
export type PermissionKey = keyof typeof Permission.PERMISSIONS;
Usage
Create permission instance
const userPerms = new Permission()
console.log(userPerms.getPermissions()) // { string: "0", array: [] }
Modify permissions
// Set permissions (replaces all existing permissions)
userPerms.setPermissions(['ADMIN', 'OWNER'])
// Add more permissions (keeps existing ones)
userPerms.addPermissions(['READ'])
// Remove specific permissions
userPerms.removePermissions(['ADMIN']);
Check permissions
// Check if user has ANY of the specified permissions
userPerms.hasAnyPermission(['READ', 'WRITE', 'ADMIN']) // true (has READ)
// Check if user has ALL specified permissions
userPerms.hasAllPermissions(['READ', 'ADMIN']) // false (missing ADMIN)
Setting and restoring permissions
// Get all available permission keys
Permission.getPermissionKeys() // ["NONE", "READ", "WRITE", "ADMIN", "OWNER"]
// Create instance with initial permissions using bigint
const readOnlyUser = new Permission(1n) // READ permission
// Create instance with multiple initial permissions
const editor = new Permission(3n) // READ (1) + WRITE (2) = 3
Database
const user = new Permission();
user.setPermissions(['READ', 'WRITE']);
// Store in database
user.getPermissions().string // "3"
// Restore from database
const userPerms = Permission.fromString(stringFromDatabase);