mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 12:37:20 +00:00
refactor: enhance permission handling and introduce new Permission and Policy classes
- Updated the `Guard` class to improve permission checking by utilizing the new `Permission` class. - Refactored tests in `authorize.spec.ts` to use `Permission` instances instead of strings for better type safety. - Introduced a new `permissions.spec.ts` file to test the functionality of the `Permission` and `Policy` classes. - Enhanced the `recursivelyReplacePlaceholders` utility function to support various object structures and types. - Updated middleware and controller files to align with the new permission handling structure.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Exception } from "core/errors";
|
||||
import { $console, objectTransform } from "bknd/utils";
|
||||
import { $console, objectTransform, type s } from "bknd/utils";
|
||||
import { Permission } from "core/security/Permission";
|
||||
import type { Context } from "hono";
|
||||
import type { ServerEnv } from "modules/Controller";
|
||||
@@ -12,6 +12,7 @@ export type GuardUserContext = {
|
||||
|
||||
export type GuardConfig = {
|
||||
enabled?: boolean;
|
||||
context?: string;
|
||||
};
|
||||
export type GuardContext = Context<ServerEnv> | GuardUserContext;
|
||||
|
||||
@@ -26,6 +27,9 @@ export class Guard {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
static create(
|
||||
permissionNames: string[],
|
||||
roles?: Record<
|
||||
@@ -156,12 +160,25 @@ export class Guard {
|
||||
return !!rolePermission;
|
||||
}
|
||||
|
||||
granted(permission: Permission | string, c?: GuardContext): boolean {
|
||||
granted<P extends Permission>(
|
||||
permission: P,
|
||||
c?: GuardContext,
|
||||
context: s.Static<P["context"]> = {} as s.Static<P["context"]>,
|
||||
): boolean {
|
||||
const user = c && "get" in c ? c.get("auth")?.user : c;
|
||||
return this.hasPermission(permission as any, user);
|
||||
const ctx = {
|
||||
...context,
|
||||
user,
|
||||
context: this.config?.context,
|
||||
};
|
||||
return this.hasPermission(permission, user);
|
||||
}
|
||||
|
||||
throwUnlessGranted(permission: Permission | string, c: GuardContext) {
|
||||
throwUnlessGranted<P extends Permission>(
|
||||
permission: P,
|
||||
c: GuardContext,
|
||||
context: s.Static<P["context"]>,
|
||||
) {
|
||||
if (!this.granted(permission, c)) {
|
||||
throw new Exception(
|
||||
`Permission "${typeof permission === "string" ? permission : permission.name}" not granted`,
|
||||
|
||||
Reference in New Issue
Block a user