上回分析了 node-casbin 模块, Casbin 主要是提供了多种的权限控制策略, 支持 ACL, RBAC, ABAC, RESTful 以及复杂的拒绝覆盖, 权限优先等级等。 不得不说 Casbin 功能强大,不过对于我的项目需求,直接用起来,看似又比较复杂了。 因为我这个项目主要是用RESTful API, 所以借鉴了 accesscontrol 和 casbin 的 RESTful 的控制模式简化成我要的版本,具体如下:
1. @RolesGuard 与 @Roles 的关系
上回我们看官网的方式是,使用 @RolesGuard 和 @Roles 组合的方式, 使用 @RolesGuard 去守护, @Roles 则去标记哪个角色可以通过。
我们回顾下代码 @RolesGuard 搭档 @Roles
@Controller('cats')
@UseGuards(RolesGuard)
@UseInterceptors(LoggingInterceptor, TransformInterceptor)
export class CatsController {
constructor(private readonly catsService: CatsService) {}
&#