Skip to content

Commit

Permalink
feat(guard): get metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jiawei397 committed Dec 7, 2021
1 parent 79c093c commit 8783a51
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
28 changes: 25 additions & 3 deletions example/user/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Controller,
ControllerName,
Get,
getMetadataForGuard,
Ip,
MethodName,
Post,
Expand All @@ -14,14 +15,16 @@ import {
Request,
Res,
Response,
SetMetadata,
UseGuards,
} from "../../../mod.ts";
import type { Context } from "../../../mod.ts";
import { BadRequestException, delay, mockjs } from "../../deps.ts";

class AuthGuard implements CanActivate {
async canActivate(_context: Context): Promise<boolean> {
async canActivate(context: Context): Promise<boolean> {
console.log("--AuthGuard---");
console.log("roles", getMetadataForGuard("roles", context));
// await delay(100);
// throw new ForbiddenException("this is AuthGuard error message");
return true;
Expand Down Expand Up @@ -51,6 +54,22 @@ class AuthGuard3 implements CanActivate {
}
}

export enum RoleAction {
read = 1,
write = 1 << 1,
delete = 1 << 2,
export = 1 << 3,
}

export const Roles = (...roles: RoleAction[]) => SetMetadata("roles", roles);

export const LogTime = () => {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
console.log("----logTime--");
return descriptor;
};
};

@UseGuards(AuthGuard)
@Controller("/user")
export class UserController {
Expand Down Expand Up @@ -91,8 +110,9 @@ export class UserController {
throw new BadRequestException("bad request");
}

// @UseGuards(AuthGuard2, AuthGuard3)
@UseGuards(AuthGuard2, AuthGuard3)
@Get("list")
@Roles(RoleAction.read)
list(context: Context) {
this.testInnerCall();
context.response.body = mockjs.mock({
Expand All @@ -101,10 +121,12 @@ export class UserController {
}

testInnerCall() {
console.log("---test---");
console.log("---testInnerCall---");
}

@Post("citys")
@LogTime()
@Roles(RoleAction.read, RoleAction.write)
getCitys(ctx: Context, @Body() params: any) {
console.log("----citys---", params);
const result = mockjs.mock({
Expand Down
29 changes: 29 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { transferParam } from "./params.ts";
export const META_METHOD_KEY = Symbol("meta:method");
export const META_PATH_KEY = Symbol("meta:path");
export const META_GUARD_KEY = Symbol("meta:guard");
export const META_FUNCTION_KEY = Symbol("meta:fn");

const classCaches = new Map<Constructor, any>();

Expand All @@ -26,6 +27,33 @@ function transResponseResult(context: Context, result: any) {
}
}

export function SetMetadata<K = string, V = any>(
metadataKey: K,
metadataValue: V,
) {
const decoratorFactory = (
target: any,
_propertyKey: string | symbol,
descriptor: PropertyDescriptor,
) => {
if (descriptor) {
Reflect.defineMetadata(metadataKey, metadataValue, descriptor.value);
return descriptor;
}
Reflect.defineMetadata(metadataKey, metadataValue, target);
return target;
};
decoratorFactory.KEY = metadataKey;
return decoratorFactory;
}

export function getMetadataForGuard(metadataKey: string, context: Context) {
const fn = Reflect.getMetadata(META_FUNCTION_KEY, context);
if (fn) {
return Reflect.getMetadata(metadataKey, fn);
}
}

export function overrideFnByGuard(
guards: CanActivate[],
target: unknown,
Expand All @@ -45,6 +73,7 @@ export function overrideFnByGuard(
classCaches.set(guard, _guard);
}
}
Reflect.defineMetadata(META_FUNCTION_KEY, fn, context); // record the function to context
const result = await _guard.canActivate(context);
if (!result) {
throw new UnauthorizedException(UnauthorizedException.name);
Expand Down

0 comments on commit 8783a51

Please sign in to comment.