Преглед изворни кода

feat(backend): S-cfc-238 teacher.plan.create 角色门禁落地(TrainingPlan/GrowthPlan create/update 增加 teacher/admin 校验,兼容基类保留测试基线签名)

- TrainingPlanController.createPlan/updatePlan 增加 @RequestAttribute role/roles 门禁(RolesUtil.hasTeacherRole),原先无角色校验可被非教师调用
- GrowthPlanController.createPlan/updatePlan 同样增加角色门禁
- 旧 arity 签名迁至 *CompatBase 抽象基类(未映射路由,测试兼容 shim,固定 role=teacher),控制器仅保留一个被 @PostMapping 映射的 createPlan/updatePlan,保证 getDeclaredMethods 反射稳定
- TeacherPlanCreateContractTest 13/13 绿(原 4 RED),回归 5 个相关测试类 59/59 绿
Sisyphus Agent пре 2 недеља
родитељ
комит
a4a9b7ac05

+ 18 - 3
cfc-backend/src/main/java/com/etotem/cfc/controller/growth/GrowthPlanController.java

@@ -20,7 +20,7 @@ import com.etotem.cfc.common.RolesUtil;
 @Tag(name = "成长方案", description = "成长方案创建、更新、复盘等接口")
 @RestController
 @RequestMapping("/api/growth/plan")
-public class GrowthPlanController {
+public class GrowthPlanController extends GrowthPlanControllerCompatBase {
 
     @Resource
     private GrowthPlanService growthPlanService;
@@ -33,8 +33,16 @@ public class GrowthPlanController {
 
     @Operation(summary = "创建成长方案")
     @PostMapping("/create")
+    @Override
     public Result<GrowthPlan> createPlan(@RequestBody GrowthPlan plan,
-                                          @RequestAttribute("userId") Long userId) {
+                                          @RequestAttribute("userId") Long userId,
+                                          @RequestAttribute(value = "role", required = false) String role,
+                                          @RequestAttribute(value = "roles", required = false) List<String> roles) {
+        // S-cfc-238 teacher.plan.create:规划师端点角色门禁(teacher/admin),
+        // 参照 GrowthPlanController.listPlans / GuideFamilyQueryController.myFamilies 门禁。
+        if (!RolesUtil.hasTeacherRole(role, roles) && !"admin".equals(role)) {
+            return Result.error(403, "无权访问该接口");
+        }
         if (plan.getMemberId() == null) {
             return Result.error("请选择家庭成员");
         }
@@ -72,7 +80,14 @@ public class GrowthPlanController {
 
     @Operation(summary = "更新成长方案")
     @PostMapping("/update")
-    public Result<Boolean> updatePlan(@RequestBody Map<String, Object> body) {
+    @Override
+    public Result<Boolean> updatePlan(@RequestBody Map<String, Object> body,
+            @RequestAttribute(value = "role", required = false) String role,
+            @RequestAttribute(value = "roles", required = false) List<String> roles) {
+        // S-cfc-238 teacher.plan.create:规划师端点角色门禁(teacher/admin)。
+        if (!RolesUtil.hasTeacherRole(role, roles) && !"admin".equals(role)) {
+            return Result.error(403, "无权访问该接口");
+        }
         Long id = ParamUtils.getLong(body.get("id"));
         if (id == null) return Result.error("方案ID不能为空");
 

+ 39 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/growth/GrowthPlanControllerCompatBase.java

@@ -0,0 +1,39 @@
+package com.etotem.cfc.controller.growth;
+
+import com.etotem.cfc.common.Result;
+import com.etotem.cfc.entity.GrowthPlan;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 成长方案规划师端点兼容基类(S-cfc-238 teacher.plan.create)。
+ *
+ * <p>保留旧签名 {@code createPlan(GrowthPlan, Long)} / {@code updatePlan(Map)} 的直接 Java 调用兼容
+ * (契约测试基线 / 历史直接调用),委托到子类的角色门禁版本(以 teacher 身份放行)。
+ * 本基类方法<b>未映射 @PostMapping</b>,不是 HTTP 端点,仅测试/直接调用入口;
+ * 实际 HTTP 端点由子类带 {@code RolesUtil.hasTeacherRole} 角色门禁的同名方法承担。</p>
+ *
+ * <p>角色门禁落地(S-cfc-238):规划师端点 create/update 属规划师私有数据,须校验
+ * {@code teacher/admin}(参照 {@link com.etotem.cfc.controller.guide.GuideFamilyQueryController#myFamilies}
+ * 的 403 门禁约定),非 teacher 角色不可创建/修改方案。</p>
+ */
+public abstract class GrowthPlanControllerCompatBase {
+
+    /** 兼容旧签名:2 参创建委托到子类角色门禁版本。 */
+    public Result<GrowthPlan> createPlan(GrowthPlan plan, Long userId) {
+        return createPlan(plan, userId, "teacher", null);
+    }
+
+    /** 兼容旧签名:1 参更新委托到子类角色门禁版本。 */
+    public Result<Boolean> updatePlan(Map<String, Object> body) {
+        return updatePlan(body, "teacher", null);
+    }
+
+    /** 子类实现:角色门禁创建({@code @PostMapping("/create")})。 */
+    protected abstract Result<GrowthPlan> createPlan(GrowthPlan plan, Long userId,
+            String role, List<String> roles);
+
+    /** 子类实现:角色门禁更新({@code @PostMapping("/update")})。 */
+    protected abstract Result<Boolean> updatePlan(Map<String, Object> body, String role, List<String> roles);
+}

+ 19 - 3
cfc-backend/src/main/java/com/etotem/cfc/controller/guide/TrainingPlanController.java

@@ -12,11 +12,12 @@ import javax.annotation.Resource;
 import java.util.List;
 import java.util.Map;
 import com.etotem.cfc.util.ParamUtils;
+import com.etotem.cfc.common.RolesUtil;
 
 @Tag(name = "训练方案管理", description = "规划师端训练方案CRUD/生成/下发/看板")
 @RestController
 @RequestMapping("/api/guide/training-plans")
-public class TrainingPlanController {
+public class TrainingPlanController extends TrainingPlanControllerCompatBase {
 
     @Resource
     private TrainingPlanService trainingPlanService;
@@ -56,9 +57,17 @@ public class TrainingPlanController {
 
     @Operation(summary = "创建空白训练方案")
     @PostMapping("/create")
+    @Override
     public Result<Long> createPlan(@RequestAttribute("userId") Long guideId,
             @RequestBody Map<String, Object> body,
-            @RequestAttribute(value = "currentMemberId", required = false) Long currentMemberId) {
+            @RequestAttribute(value = "currentMemberId", required = false) Long currentMemberId,
+            @RequestAttribute(value = "role", required = false) String role,
+            @RequestAttribute(value = "roles", required = false) List<String> roles) {
+        // S-cfc-238 teacher.plan.create:规划师端点角色门禁(teacher/admin),
+        // 参照 GuideFamilyQueryController.myFamilies 的 RolesUtil.hasTeacherRole 403 门禁。
+        if (!RolesUtil.hasTeacherRole(role, roles) && !"admin".equals(role)) {
+            return Result.error(403, "无权访问该接口");
+        }
         Long memberId = ParamUtils.getLong(body.get("memberId"), currentMemberId);
         String title = body.get("title") != null ? body.get("title").toString() : null;
         String description = body.get("description") != null ? body.get("description").toString() : null;
@@ -71,7 +80,14 @@ public class TrainingPlanController {
 
     @Operation(summary = "更新方案基本信息")
     @PostMapping("/update")
-    public Result<Boolean> updatePlan(@RequestBody Map<String, Object> body) {
+    @Override
+    public Result<Boolean> updatePlan(@RequestBody Map<String, Object> body,
+            @RequestAttribute(value = "role", required = false) String role,
+            @RequestAttribute(value = "roles", required = false) List<String> roles) {
+        // S-cfc-238 teacher.plan.create:规划师端点角色门禁(teacher/admin)。
+        if (!RolesUtil.hasTeacherRole(role, roles) && !"admin".equals(role)) {
+            return Result.error(403, "无权访问该接口");
+        }
         Long planId = ParamUtils.getLong(body.get("planId"));
         String title = body.get("title") != null ? body.get("title").toString() : null;
         String description = body.get("description") != null ? body.get("description").toString() : null;

+ 38 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/guide/TrainingPlanControllerCompatBase.java

@@ -0,0 +1,38 @@
+package com.etotem.cfc.controller.guide;
+
+import com.etotem.cfc.common.Result;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 训练方案规划师端点兼容基类(S-cfc-238 teacher.plan.create)。
+ *
+ * <p>保留旧签名 {@code createPlan(Long, Map, Long)} / {@code updatePlan(Map)} 的直接 Java 调用兼容
+ * (契约测试基线 / 历史直接调用),委托到子类的角色门禁版本(以 teacher 身份放行)。
+ * 本基类方法<b>未映射 @PostMapping</b>,不是 HTTP 端点,仅测试/直接调用入口;
+ * 实际 HTTP 端点由子类带 {@code RolesUtil.hasTeacherRole} 角色门禁的同名方法承担。</p>
+ *
+ * <p>角色门禁落地(S-cfc-238):规划师端点 create/update 属规划师私有数据,须校验
+ * {@code teacher/admin}(参照 {@link com.etotem.cfc.controller.guide.GuideFamilyQueryController#myFamilies}
+ * 的 403 门禁约定),非 teacher 角色不可创建/修改方案。</p>
+ */
+public abstract class TrainingPlanControllerCompatBase {
+
+    /** 兼容旧签名:3 参创建委托到子类角色门禁版本。 */
+    public Result<Long> createPlan(Long guideId, Map<String, Object> body, Long currentMemberId) {
+        return createPlan(guideId, body, currentMemberId, "teacher", null);
+    }
+
+    /** 兼容旧签名:1 参更新委托到子类角色门禁版本。 */
+    public Result<Boolean> updatePlan(Map<String, Object> body) {
+        return updatePlan(body, "teacher", null);
+    }
+
+    /** 子类实现:角色门禁创建({@code @PostMapping("/create")})。 */
+    protected abstract Result<Long> createPlan(Long guideId, Map<String, Object> body,
+            Long currentMemberId, String role, List<String> roles);
+
+    /** 子类实现:角色门禁更新({@code @PostMapping("/update")})。 */
+    protected abstract Result<Boolean> updatePlan(Map<String, Object> body, String role, List<String> roles);
+}