本文整理汇总了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;
}