本文整理匯總了TypeScript中@nestjs/common.ExecutionContext類的典型用法代碼示例。如果您正苦於以下問題:TypeScript ExecutionContext類的具體用法?TypeScript ExecutionContext怎麽用?TypeScript ExecutionContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ExecutionContext類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: canActivate
canActivate(context: ExecutionContext): boolean {
const roles = this.reflector.get<string[]>('role', context.getHandler());
if (!roles) {
return true;
}
const request = context.switchToHttp().getRequest();
const user = request.user;
const hasRole = () => roles.includes(user.role);
return user && user.role && hasRole();
}
示例2: canActivate
canActivate(context: ExecutionContext): boolean {
const roles = this.reflector.get<string[]>('roles', context.getHandler());
if (!roles) {
return true;
}
const request = context.switchToHttp().getRequest();
const user = request.user;
const hasRole = user.roles.some((role) => !!roles.find((item) => item === role));
return user && user.roles && hasRole();
}
示例3: canActivate
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest();
const isAdmin = request.user.user.userGroups[0].isAdmin;
return isAdmin;
}
示例4: trackBy
trackBy(context: ExecutionContext): string | undefined {
const request = context.switchToHttp().getRequest();
const isGetRequest = this.httpServer.getRequestMethod(request) === 'GET';
const excludePaths = [];
if (
!isGetRequest ||
(isGetRequest && excludePaths.includes(this.httpServer.getRequestUrl))
) {
return undefined;
}
return this.httpServer.getRequestUrl(request);
}
示例5: canActivate
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
const {user} = request;
return user && user.isAdmin;
}
示例6: canActivate
// https://github.com/nestjs/graphql/issues/48#issuecomment-420693225
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
const ctx = GqlExecutionContext.create(context);
const { req } = ctx.getContext() ? ctx.getContext() : { req: context.getArgs().find(c => c instanceof IncomingMessage) };
return super.canActivate(new ExecutionContextHost([req]));
}
示例7: canActivate
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
const user = request.user;
return user && user.role.role === Roles.Admin;
}