diff --git a/README.md b/README.md index b95640d..496411d 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,8 @@ Kubernetes 或密钥管理系统仍可覆盖文件中的值。 配置 `Moodle__TeacherRoleId` 并为服务账户提供 `core_user_get_users_by_field`、`enrol_manual_enrol_users` 和相应用户查询、手动选课及角色分配权限后,管理员可在已关联的教学任务中同步授课教师。教务工号须与 Moodle 用户名一致;系统先检查所有教师账号,再加入课程,不会自动创建账号或移除 Moodle 中已有的教师。课程需启用 Moodle 手动选课方式。 +Moodle 用户名使用工号或学号的小写形式,例如教务工号 `T001` 对应 Moodle 用户名 `t001`;同步时自动转换,教务系统中的原编号不变。如果同一名单的编号转换后重复,系统会拒绝同步并要求核对账号。 + 任课教师也可在“选课名单”中对本人已发布教学班匹配或创建 Moodle 课程、同步授课教师及导入学生。教师复制历史课程时,来源限于本人曾任教且已在教务系统关联的 Moodle 数字 ID 链接;编号链接需要先由管理员改为对应数字 ID 链接才能作为教师复制来源。教师同步不会自动完成全部步骤,课程创建、教师同步和学生导入分别发起。 配置 `Moodle__StudentRoleId` 后,有管理权限的管理员或该教学班的任课教师可将当前教学班名单导入 Moodle,学号须与 Moodle 用户名一致。名单包括行政班关联和正式选课学生,排除学籍异动限制;导入是增量操作,不会自动移除退课学生或创建账号。导入前应确认选课名单,名单变化后可重复导入;需要撤销访问时须在 Moodle 中处理。 diff --git a/src/Jiaowu.Api/Controllers/MoodleCoursesController.cs b/src/Jiaowu.Api/Controllers/MoodleCoursesController.cs index 2d80e97..d451552 100644 --- a/src/Jiaowu.Api/Controllers/MoodleCoursesController.cs +++ b/src/Jiaowu.Api/Controllers/MoodleCoursesController.cs @@ -53,7 +53,7 @@ public sealed class MoodleCoursesController(AppDbContext db, ICurrentUserDataSco return Conflict(new ProblemDetails { Detail = "请先发布教学任务并关联 Moodle 课程。", Status = 409 }); try { - await moodle.SyncTeachersAsync(task.MoodleCourseUrl, task.Teachers.Select(x => x.Teacher!.TeacherNumber).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(), ct); + await moodle.SyncTeachersAsync(task.MoodleCourseUrl, task.Teachers.Select(x => x.Teacher!.TeacherNumber).ToArray(), ct); return NoContent(); } catch (MoodleIntegrationException ex) diff --git a/src/Jiaowu.Api/Infrastructure/Teaching/MoodleCourseClient.cs b/src/Jiaowu.Api/Infrastructure/Teaching/MoodleCourseClient.cs index e7a7126..bb67c70 100644 --- a/src/Jiaowu.Api/Infrastructure/Teaching/MoodleCourseClient.cs +++ b/src/Jiaowu.Api/Infrastructure/Teaching/MoodleCourseClient.cs @@ -41,8 +41,11 @@ public sealed class MoodleCourseClient(IHttpClientFactory clients, IConfiguratio else if (!query.TryGetValue("id", out var idText) || !long.TryParse(idText.ToString(), out courseId) || courseId <= 0) throw new MoodleIntegrationException("Moodle 课程链接缺少有效的课程标识。"); if (teacherNumbers.Count == 0) throw new MoodleIntegrationException("教学任务名单为空。"); + var usernames = teacherNumbers.Select(number => number.ToLowerInvariant()).ToArray(); + if (usernames.Distinct(StringComparer.Ordinal).Count() != usernames.Length) + throw new MoodleIntegrationException("人员编号转换为小写后出现重复,无法安全匹配 Moodle 账号。"); var usersQuery = new Dictionary { ["field"] = "username" }; - for (var i = 0; i < teacherNumbers.Count; i++) usersQuery[$"values[{i}]"] = teacherNumbers[i]; + for (var i = 0; i < usernames.Length; i++) usersQuery[$"values[{i}]"] = usernames[i]; using var response = await CallAsync(baseUri, token, "core_user_get_users_by_field", usersQuery, ct); if (response.RootElement.ValueKind != JsonValueKind.Array) throw new MoodleIntegrationException("Moodle 用户查询结果无效。"); var users = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -54,13 +57,13 @@ public sealed class MoodleCourseClient(IHttpClientFactory clients, IConfiguratio !users.TryAdd(username.GetString()!, userId)) throw new MoodleIntegrationException("Moodle 用户信息无效或存在重复、停用账号。"); } - if (teacherNumbers.Any(number => !users.ContainsKey(number))) + if (usernames.Any(number => !users.ContainsKey(number))) throw new MoodleIntegrationException("部分人员编号未匹配到 Moodle 用户名,请检查账号后重试。"); var enrollments = new Dictionary(); - for (var i = 0; i < teacherNumbers.Count; i++) + for (var i = 0; i < usernames.Length; i++) { enrollments[$"enrolments[{i}][roleid]"] = roleId.ToString(); - enrollments[$"enrolments[{i}][userid]"] = users[teacherNumbers[i]].ToString(); + enrollments[$"enrolments[{i}][userid]"] = users[usernames[i]].ToString(); enrollments[$"enrolments[{i}][courseid]"] = courseId.ToString(); } using var enrolled = await CallAsync(baseUri, token, "enrol_manual_enrol_users", enrollments, ct); diff --git a/tests/Jiaowu.Api.Tests/MoodleCourseClientTests.cs b/tests/Jiaowu.Api.Tests/MoodleCourseClientTests.cs index 3a0dce7..f7e2680 100644 --- a/tests/Jiaowu.Api.Tests/MoodleCourseClientTests.cs +++ b/tests/Jiaowu.Api.Tests/MoodleCourseClientTests.cs @@ -90,11 +90,25 @@ public sealed class MoodleCourseClientTests await Assert.ThrowsAsync(() => client.SyncTeachersAsync("https://moodle.example.edu/sub/course/view.php?id=42", ["T01", "MISSING"], CancellationToken.None)); Assert.Single(handler.Requests); await client.SyncTeachersAsync("https://moodle.example.edu/sub/course/view.php?id=42", ["T01"], CancellationToken.None); + Assert.Contains("values%5B0%5D=t01", handler.Requests[1]); Assert.Contains("enrol_manual_enrol_users", handler.Requests.Last()); await Assert.ThrowsAsync(() => client.SyncTeachersAsync("https://other.example.edu/sub/course/view.php?id=42", ["T01"], CancellationToken.None)); Assert.Equal(3, handler.Requests.Count); } + [Fact] + public async Task TeacherSync_RejectsNumbersThatCollideAfterLowercasing() + { + var handler = new TeacherHandler(); + var config = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary + { + ["Moodle:BaseUrl"] = "https://moodle.example.edu/sub", ["Moodle:Token"] = "token", ["Moodle:CategoryId"] = "4", ["Moodle:TeacherRoleId"] = "3" + }).Build(); + var client = new MoodleCourseClient(new Factory(new HttpClient(handler)), config); + await Assert.ThrowsAsync(() => client.SyncTeachersAsync("https://moodle.example.edu/sub/course/view.php?id=42", ["T01", "t01"], CancellationToken.None)); + Assert.Empty(handler.Requests); + } + private sealed class TeacherHandler : HttpMessageHandler { public List Requests { get; } = []; @@ -102,7 +116,7 @@ public sealed class MoodleCourseClientTests { var body = await request.Content!.ReadAsStringAsync(cancellationToken); Requests.Add(body); - return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(body.Contains("core_user_get_users_by_field") ? "[{\"id\":7,\"username\":\"T01\",\"suspended\":0}]" : "null") }; + return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(body.Contains("core_user_get_users_by_field") ? "[{\"id\":7,\"username\":\"t01\",\"suspended\":0}]" : "null") }; } }