辅导员权限优化

This commit is contained in:
biss committed 2026-10-20 18:21:34 +08:00
1 parent 37c5d6eb93
commit 16cbd188fb
12 files changed
+940 -98

No files matched your search

@@ -43,6 +43,9 @@ public sealed class AttendanceController(
{
page = Math.Max(1, page);
pageSize = Math.Clamp(pageSize, 10, 50);
var scope = currentUserDataScope.Current;
var counselorOnly = scope.Scope == DataScope.Class &&
scope.IsInRole(SystemRoles.Counselor);
var tasks = AccessibleTasks().AsNoTracking()
.Where(x => x.Status == TeachingTaskStatus.Published ||
x.Status == TeachingTaskStatus.Closed);
@@ -82,14 +85,16 @@ public sealed class AttendanceController(
CourseCode = x.Course!.Code,
CourseName = x.Course!.Name,
StudentCount = db.Students.Count(student =>
(student.Status == StudentStatus.Active &&
(!counselorOnly ||
student.AdministrativeClass!.CounselorUserId == scope.UserId) &&
((student.Status == StudentStatus.Active &&
x.Classes.Any(assignment =>
assignment.AdministrativeClassId ==
student.AdministrativeClassId)) ||
db.CourseEnrollments.Any(enrollment =>
enrollment.StudentId == student.Id &&
enrollment.Status == CourseEnrollmentStatus.Enrolled &&
enrollment.CourseSelectionOffering!.TeachingTaskId == x.Id)),
enrollment.CourseSelectionOffering!.TeachingTaskId == x.Id))),
SheetCount = db.AttendanceSheets.Count(sheet =>
sheet.TeachingTaskId == x.Id)
})
@@ -110,6 +115,9 @@ public sealed class AttendanceController(
{
page = Math.Max(1, page);
pageSize = Math.Clamp(pageSize, 10, 50);
var scope = currentUserDataScope.Current;
var counselorOnly = scope.Scope == DataScope.Class &&
scope.IsInRole(SystemRoles.Counselor);
var task = await AccessibleTasks().AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == teachingTaskId, cancellationToken);
if (task is null) return NotFound();
@@ -143,13 +151,26 @@ public sealed class AttendanceController(
x.CheckInEndsAt,
x.Notes,
x.SubmittedAt,
CheckedInCount = x.Records.Count(r => r.CheckInAt != null),
PresentCount = x.Records.Count(r => r.Status == AttendanceStatus.Present),
AbsentCount = x.Records.Count(r => r.Status == AttendanceStatus.Absent),
LateCount = x.Records.Count(r => r.Status == AttendanceStatus.Late),
LeaveCount = x.Records.Count(r => r.Status == AttendanceStatus.Leave),
ExcusedCount = x.Records.Count(r => r.Status == AttendanceStatus.Excused),
TotalCount = x.Records.Count
CheckedInCount = x.Records.Count(r =>
(!counselorOnly || r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId) &&
r.CheckInAt != null),
PresentCount = x.Records.Count(r =>
(!counselorOnly || r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId) &&
r.Status == AttendanceStatus.Present),
AbsentCount = x.Records.Count(r =>
(!counselorOnly || r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId) &&
r.Status == AttendanceStatus.Absent),
LateCount = x.Records.Count(r =>
(!counselorOnly || r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId) &&
r.Status == AttendanceStatus.Late),
LeaveCount = x.Records.Count(r =>
(!counselorOnly || r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId) &&
r.Status == AttendanceStatus.Leave),
ExcusedCount = x.Records.Count(r =>
(!counselorOnly || r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId) &&
r.Status == AttendanceStatus.Excused),
TotalCount = x.Records.Count(r =>
!counselorOnly || r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId)
})
.ToListAsync(cancellationToken);
return Ok(new PagedResult<object>(sheets, total, page, pageSize));
@@ -164,6 +185,8 @@ public sealed class AttendanceController(
var task = await AccessibleTasks().AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == request.TeachingTaskId, cancellationToken);
if (task is null) return NotFound();
if (!await CanManageTaskAsync(request.TeachingTaskId, cancellationToken))
return Forbid();
if (!IsTaskOperable(task))
return ConflictProblem("教学任务已归档,不能新建考勤表。");
@@ -245,6 +268,9 @@ public sealed class AttendanceController(
[Authorize(Roles = AttendanceRoles)]
public async Task<ActionResult> GetSheet(Guid id, CancellationToken cancellationToken)
{
var scope = currentUserDataScope.Current;
var counselorOnly = scope.Scope == DataScope.Class &&
scope.IsInRole(SystemRoles.Counselor);
var sheet = await db.AttendanceSheets.AsNoTracking()
.Where(x => x.Id == id)
.Select(x => new
@@ -268,6 +294,8 @@ public sealed class AttendanceController(
CourseCode = x.TeachingTask.Course!.Code,
CourseName = x.TeachingTask.Course.Name,
Records = x.Records
.Where(r => !counselorOnly ||
r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId)
.OrderBy(r => r.Student!.StudentNumber)
.Select(r => new
{
@@ -529,6 +557,9 @@ public sealed class AttendanceController(
[Authorize(Roles = AttendanceRoles)]
public async Task<ActionResult> Export(Guid id, CancellationToken cancellationToken)
{
var scope = currentUserDataScope.Current;
var counselorOnly = scope.Scope == DataScope.Class &&
scope.IsInRole(SystemRoles.Counselor);
var sheetData = await db.AttendanceSheets.AsNoTracking()
.Where(x => x.Id == id)
.Select(x => new
@@ -539,6 +570,8 @@ public sealed class AttendanceController(
TaskNumber = x.TeachingTask!.TaskNumber,
CourseName = x.TeachingTask.Course!.Name,
Records = x.Records
.Where(r => !counselorOnly ||
r.Student!.AdministrativeClass!.CounselorUserId == scope.UserId)
.OrderBy(r => r.Student!.StudentNumber)
.Select(r => new
{
@@ -1203,11 +1236,13 @@ public sealed class AttendanceController(
return source.Where(x => x.Course!.CollegeId == scope.RestrictedCollegeId);
if (scope.IsInRole(SystemRoles.Counselor))
{
var collegeIds = db.AdministrativeClasses
.Where(c => c.CounselorUserId == scope.UserId)
.Select(c => c.Major!.CollegeId)
.Distinct();
return source.Where(x => collegeIds.Contains(x.Course!.CollegeId));
return source.Where(task =>
task.Classes.Any(item =>
item.AdministrativeClass!.CounselorUserId == scope.UserId) ||
db.CourseEnrollments.Any(enrollment =>
enrollment.CourseSelectionOffering!.TeachingTaskId == task.Id &&
enrollment.Status == CourseEnrollmentStatus.Enrolled &&
enrollment.Student!.AdministrativeClass!.CounselorUserId == scope.UserId));
}
if (scope.IsInRole(SystemRoles.Teacher))
return source.Where(x =>
@@ -1399,9 +1434,14 @@ public sealed class AttendanceController(
.FirstOrDefaultAsync(cancellationToken);
if (course is null) return null;
var enrolledStudents = await TeachingTaskRosterQuery
.ForTask(db, teachingTaskId)
.AsNoTracking()
var scope = currentUserDataScope.Current;
var counselorOnly = scope.Scope == DataScope.Class &&
scope.IsInRole(SystemRoles.Counselor);
var roster = TeachingTaskRosterQuery.ForTask(db, teachingTaskId).AsNoTracking();
if (counselorOnly)
roster = roster.Where(x =>
x.AdministrativeClass!.CounselorUserId == scope.UserId);
var enrolledStudents = await roster
.Select(x => new AttendanceStatisticsStudentIdentity(
x.Id,
x.StudentNumber,
@@ -1424,7 +1464,9 @@ public sealed class AttendanceController(
var records = await db.AttendanceRecords.AsNoTracking()
.Where(x =>
x.AttendanceSheet!.TeachingTaskId == teachingTaskId &&
x.AttendanceSheet.Status == AttendanceSheetStatus.Submitted)
x.AttendanceSheet.Status == AttendanceSheetStatus.Submitted &&
(!counselorOnly ||
x.Student!.AdministrativeClass!.CounselorUserId == scope.UserId))
.Select(x => new AttendanceStatisticsRecord(
x.AttendanceSheetId,
x.StudentId,
@@ -272,7 +272,7 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache,
.Take(pageSize.Value)
.Select(x => new AdministrativeClassListItem(
x.Id, x.Code, x.Name, x.MajorId, x.Major!.Name,
x.Major.College!.Name, x.Grade, x.CounselorUserId,
x.Major.CollegeId, x.Major.College!.Name, x.Grade, x.CounselorUserId,
x.CounselorName, x.IsEnabled, x.SortOrder))
.ToListAsync(cancellationToken);
return Ok(new PagedResult<AdministrativeClassListItem>(
@@ -289,6 +289,7 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache,
x.Name,
x.MajorId,
x.Major!.Name,
x.Major.CollegeId,
x.Major.College!.Name,
x.Grade,
x.CounselorUserId,
@@ -336,6 +337,9 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache,
cancellationToken);
if (request.CounselorUserId.HasValue && counselorName is null)
return ValidationProblem("所选辅导员账号不存在、已停用或没有辅导员角色。");
if (!await CounselorBelongsToMajorCollegeAsync(
request.CounselorUserId, request.MajorId, cancellationToken))
return ValidationProblem("辅导员所属学院与行政班所属学院不一致。");
var entity = new AdministrativeClass
{
@@ -365,6 +369,9 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache,
cancellationToken);
if (request.CounselorUserId.HasValue && counselorName is null)
return ValidationProblem("所选辅导员账号不存在、已停用或没有辅导员角色。");
if (!await CounselorBelongsToMajorCollegeAsync(
request.CounselorUserId, request.MajorId, cancellationToken))
return ValidationProblem("辅导员所属学院与行政班所属学院不一致。");
ApplyCatalog(entity, request);
entity.MajorId = request.MajorId;
entity.Grade = request.Grade;
@@ -771,6 +778,22 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache,
.FirstOrDefaultAsync(cancellationToken);
}
private async Task<bool> CounselorBelongsToMajorCollegeAsync(
Guid? userId,
Guid majorId,
CancellationToken cancellationToken)
{
if (!userId.HasValue) return true;
var counselorCollegeId = await db.Users.AsNoTracking()
.Where(x => x.Id == userId.Value)
.Select(x => x.CollegeId)
.FirstOrDefaultAsync(cancellationToken);
if (!counselorCollegeId.HasValue) return true;
return await db.Majors.AsNoTracking().AnyAsync(
x => x.Id == majorId && x.CollegeId == counselorCollegeId.Value,
cancellationToken);
}
private static void ApplyCatalog(CatalogEntity entity, CatalogRequest request)
{
entity.Code = request.Code.Trim();
@@ -866,6 +889,7 @@ public sealed record AdministrativeClassListItem(
string Name,
Guid MajorId,
string MajorName,
Guid CollegeId,
string CollegeName,
int Grade,
Guid? CounselorUserId,
@@ -337,6 +337,12 @@ public sealed class BaseDataExcelController(AppDbContext db, IAppCache cache) :
if (counselorNumber is not null &&
!counselors.TryGetValue(counselorNumber, out counselor))
errors.Add($"第 {row.RowNumber} 行:辅导员工号“{counselorNumber}”不存在或已停用。");
if (counselor?.CollegeId is { } counselorCollegeId &&
counselorCollegeId != major.CollegeId)
{
errors.Add($"第 {row.RowNumber} 行:辅导员所属学院与行政班所属学院不一致。");
continue;
}
var entity = existing.GetValueOrDefault(code);
if (entity is null)
{
Loaded 3 of 12 files, more files were not shown because too many files have changed in this diff. Show more