有没有办法在表单数据中检查所需的角色?

问题描述 投票:0回答:0

我正在使用 nest js ..我从正文中检查了 require 角色,但现在我有案例让我需要检查表单数据中的 require 角色,这是我的角色守卫

import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Role } from '../enums/role.enum';
import { ROLES_KEY } from '../decorators/roles.decorator';

@Injectable()
export class RolesGuard implements CanActivate {
  constructor(private reflector: Reflector) {}

  canActivate(context: ExecutionContext): boolean {
    const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
      context.getHandler(),
      context.getClass(),
    ]);
    if (!requiredRoles) {

      return true;
    }
    
    const { body } = context.switchToHttp().getRequest();
   //console.log('==>', context.switchToHttp().getRequest())
   console.log('body should contain data ==>', body)

    console.log('should return true for role==>', requiredRoles.some((role) => body.role?.includes(role)))

    return requiredRoles.some((role) => body.role?.includes(role));
  }
}

在我的请求中,我找到了多部分/表单数据

typescript nestjs multipartform-data roles
© www.soinside.com 2019 - 2024. All rights reserved.