本文整理汇总了TypeScript中@nestjs/common.ExecutionContext.switchToHttp方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ExecutionContext.switchToHttp方法的具体用法?TypeScript ExecutionContext.switchToHttp怎么用?TypeScript ExecutionContext.switchToHttp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@nestjs/common.ExecutionContext
的用法示例。
在下文中一共展示了ExecutionContext.switchToHttp方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: canActivate
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest();
const isAdmin = request.user.user.userGroups[0].isAdmin;
return isAdmin;
}
示例2: 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();
}
示例3: 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();
}
示例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
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
const user = request.user;
return user && user.role.role === Roles.Admin;
}