forked from wangziqi/gongxue-base
chore: check off Task 3 (PermissionGuard + decorators)
This commit is contained in:
@@ -557,7 +557,7 @@ git commit -m "feat(rbac): add RbacModule and RbacService with seed data and rol
|
||||
- Consumes: `ExecutionContex`, `Reflector` (NestJS Core)
|
||||
- Produces: `@Public()` (SetMetadata IS_PUBLIC_KEY true), `@RequirePermission(...codes: string[])` (SetMetadata PERMISSION_KEY), `PermissionGuard implements CanActivate`
|
||||
|
||||
- [ ] **Step 1: 创建 @Public 装饰器**
|
||||
- [x] **Step 1: 创建 @Public 装饰器**
|
||||
|
||||
```typescript
|
||||
// backend/src/auth/decorators/public.decorator.ts
|
||||
@@ -567,7 +567,7 @@ export const IS_PUBLIC_KEY = 'isPublic';
|
||||
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
|
||||
```
|
||||
|
||||
- [ ] **Step 2: 创建 @RequirePermission 装饰器**
|
||||
- [x] **Step 2: 创建 @RequirePermission 装饰器**
|
||||
|
||||
```typescript
|
||||
// backend/src/auth/decorators/permission.decorator.ts
|
||||
@@ -584,7 +584,7 @@ export const RequirePermission = (...permissions: string[]) =>
|
||||
SetMetadata(PERMISSION_KEY, permissions);
|
||||
```
|
||||
|
||||
- [ ] **Step 3: 创建 PermissionGuard**
|
||||
- [x] **Step 3: 创建 PermissionGuard**
|
||||
|
||||
```typescript
|
||||
// backend/src/auth/guards/permission.guard.ts
|
||||
@@ -690,7 +690,7 @@ NestJS 中,**同一个 key** 多次 `SetMetadata` 时,后面的会覆盖前
|
||||
|
||||
实际代码实现如下。现在继续写 plan。
|
||||
|
||||
- [ ] **Step 3 (修正版): 创建 PermissionGuard**
|
||||
- [x] **Step 3 (修正版): 创建 PermissionGuard**
|
||||
|
||||
```typescript
|
||||
// backend/src/auth/guards/permission.guard.ts
|
||||
@@ -730,7 +730,7 @@ export class PermissionGuard implements CanActivate {
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 4: 编译验证**
|
||||
- [x] **Step 4: 编译验证**
|
||||
|
||||
```bash
|
||||
cd backend && npx tsc --noEmit
|
||||
@@ -738,7 +738,7 @@ cd backend && npx tsc --noEmit
|
||||
|
||||
预期:无类型错误。
|
||||
|
||||
- [ ] **Step 5: Commit**
|
||||
- [x] **Step 5: Commit**
|
||||
|
||||
```bash
|
||||
git add backend/src/auth/decorators/ backend/src/auth/guards/permission.guard.ts
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
|
||||
## 3. 权限守卫实现
|
||||
|
||||
- [ ] 3.1 创建 @RequirePermission 装饰器(支持 AND/OR 语义,通过 SetMetadata 存储权限列表和逻辑模式)
|
||||
- [ ] 3.2 创建 PermissionGuard:从 JWT payload 中读取 permissions 数组,与装饰器标注的权限做匹配
|
||||
- [x] 3.1 创建 @RequirePermission 装饰器(支持 AND/OR 语义,通过 SetMetadata 存储权限列表和逻辑模式)
|
||||
- [x] 3.2 创建 PermissionGuard:从 JWT payload 中读取 permissions 数组,与装饰器标注的权限做匹配
|
||||
- [ ] 3.3 修改 JwtStrategy 和 AuthService.login:登录时调用 getUserPermissions,将 permissions 写入 JWT payload
|
||||
- [ ] 3.4 修改 AuthService.validateUser/profile:返回用户信息时携带角色和权限列表
|
||||
- [ ] 3.5 全局注册 PermissionGuard(或在各模块按需注册)
|
||||
|
||||
Reference in New Issue
Block a user