mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-04 00:56:01 +00:00
enhance Guard and permission handling with new test cases
- Updated the `Guard` class to improve context validation and permission checks, ensuring clearer error messages for unmet conditions. - Refactored the `Policy` and `RolePermission` classes to support default effects and better handle conditions and filters. - Enhanced tests in `authorize.spec.ts` and `permissions.spec.ts` to cover new permission scenarios, including guest and member role behaviors. - Added new tests for context validation in permission middleware, ensuring robust error handling for invalid contexts. - Improved utility functions for better integration with the updated permission structure.
This commit is contained in:
@@ -5,6 +5,7 @@ import type { RouterRoute } from "hono/types";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import type { ServerEnv } from "modules/Controller";
|
||||
import type { MaybePromise } from "core/types";
|
||||
import { GuardPermissionsException } from "auth/authorize/Guard";
|
||||
|
||||
function getPath(reqOrCtx: Request | Context) {
|
||||
const req = reqOrCtx instanceof Request ? reqOrCtx : reqOrCtx.req.raw;
|
||||
@@ -54,7 +55,7 @@ export function permission<P extends Permission<any, any, any, any>>(
|
||||
|
||||
if (options?.onGranted || options?.onDenied) {
|
||||
let returned: undefined | void | Response;
|
||||
if (threw(() => guard.granted(permission, c, context))) {
|
||||
if (threw(() => guard.granted(permission, c, context), GuardPermissionsException)) {
|
||||
returned = await options?.onDenied?.(c);
|
||||
} else {
|
||||
returned = await options?.onGranted?.(c);
|
||||
|
||||
Reference in New Issue
Block a user