Explorar el Código

✨ feat(shared-crud): 实现更新操作的数据权限验证

- 在update方法中添加权限验证逻辑
- 验证用户是否有权更新目标实体
- 确保权限验证与现有更新逻辑集成
- 更新文档,标记更新权限验证相关任务为已完成
yourname hace 1 mes
padre
commit
f89c21644b

+ 8 - 4
docs/stories/006.001.shared-crud-data-permission.story.md

@@ -31,10 +31,10 @@ Draft
   - [x] 在 `create` 方法中添加权限验证,防止用户创建不属于自己的数据
   - [x] 验证用户ID字段与当前用户匹配
   - [x] 提供清晰的错误信息
-- [ ] 实现更新操作的权限验证 (AC: 4)
-  - [ ] 在 `update` 方法中添加权限验证
-  - [ ] 验证用户是否有权更新目标实体
-  - [ ] 确保权限验证与现有更新逻辑集成
+- [x] 实现更新操作的权限验证 (AC: 4)
+  - [x] 在 `update` 方法中添加权限验证
+  - [x] 验证用户是否有权更新目标实体
+  - [x] 确保权限验证与现有更新逻辑集成
 - [ ] 实现删除操作的权限验证 (AC: 5)
   - [ ] 在 `delete` 方法中添加权限验证
   - [ ] 验证用户是否有权删除目标实体
@@ -147,6 +147,10 @@ Draft
 - 已为 create 方法添加权限验证,防止用户创建不属于自己的数据
 - 已验证用户ID字段与当前用户匹配
 - 已提供清晰的错误信息
+- 已实现更新操作的权限验证
+- 已为 update 方法添加权限验证
+- 已验证用户是否有权更新目标实体
+- 已确保权限验证与现有更新逻辑集成
 - 构建验证通过,无类型错误
 
 ### File List

+ 11 - 0
packages/shared-crud/src/services/generic-crud.service.ts

@@ -322,6 +322,17 @@ export abstract class GenericCrudService<T extends ObjectLiteral> {
    * 更新实体
    */
   async update(id: number, data: Partial<T>, userId?: string | number): Promise<T | null> {
+    // 权限验证
+    if (this.dataPermissionOptions?.enabled && userId) {
+      const entity = await this.getById(id);
+      if (!entity) return null;
+
+      const hasPermission = await this.checkPermission(entity, userId);
+      if (!hasPermission) {
+        throw new Error('无权更新该资源');
+      }
+    }
+
     const updateData = { ...data };
     this.setUserFields(updateData, userId, false);