Files
XH-XunKe/backend/src/main/java/com/xh/xunke/user/UserAdminService.java
T
xhgitandClaude Code ed55053b10 直播观看与录像链路真实验证通过,前端 UI 全面重做,分角色控制台
- 修复: LiveKit 回环候选生效(宿主机浏览器可直连推流)
- 修复: 回放预签名 URL 403(公开端点直接签名,SignedHeaders=host)
- 修复: 录像探针补 fileSize/durationSeconds
- 修复: 家长录像列表被权限过滤误伤为 0 条;教师/家长列表按角色精确隔离
- 修复: 巡课静音失效(LiveKit attach 强制 unmute,静音时不挂载音轨)
- 修复: 教室页自动请求摄像头/麦克风权限,分辨率失败自动降级
- 新增: 浏览器端到端验证脚本 scripts/e2e-browser.mjs(9 项断言全过:
  教师推流/家长观看含音频电平实测/巡课默认静音/Egress 录像双轨齐全/
  DPlayer 回放/录像权限隔离)
- 新增: /parent/today 家长今日课程表、网站 Logo 上传与公开读取
- UI 重做: 虎牙风直播播放器 + DPlayer 回放播放器(视觉统一)
- 分角色控制台: 管理端 /admin(16 模块)、教师端 /teacher、家长端 /parent
- 直播/录像列表返回班级、课程、教师名称与在线人数
- 18088 本地预览改为 Vite 热更新,README/实现状态文档同步更新

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-22 15:08:36 +08:00

124 lines
5.3 KiB
Java

package com.xh.xunke.user;
import com.xh.xunke.common.BizException;
import com.xh.xunke.common.ErrorCode;
import com.xh.xunke.common.PagePayload;
import com.xh.xunke.common.Pages;
import com.xh.xunke.security.AccessGuard;
import com.xh.xunke.security.PasswordPolicy;
import com.xh.xunke.security.UserPrincipal;
import jakarta.validation.constraints.NotBlank;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class UserAdminService {
private final UserRepository users;
private final UserRoleRepository roles;
private final PasswordEncoder encoder;
private final PasswordPolicy passwords;
private final AccessGuard guard;
public UserAdminService(UserRepository users, UserRoleRepository roles, PasswordEncoder encoder,
PasswordPolicy passwords, AccessGuard guard) {
this.users = users;
this.roles = roles;
this.encoder = encoder;
this.passwords = passwords;
this.guard = guard;
}
public record CreateUser(@NotBlank String username, @NotBlank String password, @NotBlank String displayName,
String email, String phone, @NotBlank String role, Long schoolId) {
}
public record ReviewUser(boolean approve, String note) {
}
public PagePayload<AuthService.UserView> list(UserPrincipal me, String role, String status, String q, int page, int size) {
UserStatus parsed = status == null || status.isBlank() ? null : UserStatus.valueOf(status);
RoleName parsedRole = role == null || role.isBlank() ? null : RoleName.valueOf(role);
Long schoolId = me.superAdmin() ? null : me.schoolId();
var result = users.search(schoolId, parsed, q == null || q.isBlank() ? null : q, parsedRole, Pages.of(page, size));
var views = result.getContent().stream().map(this::view).toList();
return new PagePayload<>(views, result.getTotalElements(), result.getNumber(), result.getSize());
}
@Transactional
public AuthService.UserView create(UserPrincipal me, CreateUser request) {
RoleName role = RoleName.valueOf(request.role());
if (role == RoleName.SUPER_ADMIN) {
throw new BizException(ErrorCode.FORBIDDEN, "不能创建超级管理员");
}
if (!me.superAdmin() && role == RoleName.SCHOOL_ADMIN) {
throw new BizException(ErrorCode.FORBIDDEN, "只有超级管理员可以创建学校管理员");
}
Long schoolId = guard.requiredSchool(me, request.schoolId());
if (users.existsByUsername(request.username())) {
throw new BizException(ErrorCode.CONFLICT, "用户名已存在");
}
passwords.check(request.password());
UserAccount user = new UserAccount();
user.setUsername(request.username().trim());
user.setDisplayName(request.displayName().trim());
user.setEmail(blank(request.email()));
user.setPhone(blank(request.phone()));
user.setPasswordHash(encoder.encode(request.password()));
user.setStatus(UserStatus.ACTIVE);
user.setSchoolId(schoolId);
user.setEmailVerified(user.getEmail() != null);
user.setPhoneVerified(user.getPhone() != null);
users.save(user);
roles.save(new UserRole(user.getId(), role));
return view(user);
}
@Transactional
public AuthService.UserView review(UserPrincipal me, Long id, ReviewUser request) {
UserAccount user = require(me, id);
if (user.getStatus() != UserStatus.PENDING) {
throw new BizException(ErrorCode.STATE, "账号不在待审核状态");
}
user.setStatus(request.approve() ? UserStatus.ACTIVE : UserStatus.REJECTED);
users.save(user);
return view(user);
}
@Transactional
public AuthService.UserView status(UserPrincipal me, Long id, String status) {
UserAccount user = require(me, id);
UserStatus next = UserStatus.valueOf(status);
if (next != UserStatus.ACTIVE && next != UserStatus.DISABLED) {
throw new BizException(ErrorCode.VALIDATION, "只能启用或禁用");
}
user.setStatus(next);
user.setTokenVersion(user.getTokenVersion() + 1);
users.save(user);
return view(user);
}
private UserAccount require(UserPrincipal me, Long id) {
UserAccount user = users.findById(id).orElseThrow(() -> new BizException(ErrorCode.NOT_FOUND, "用户不存在"));
if (user.getSchoolId() != null) {
guard.checkSchool(me, user.getSchoolId());
} else if (!me.superAdmin()) {
throw new BizException(ErrorCode.FORBIDDEN, "不能管理该账号");
}
return user;
}
private AuthService.UserView view(UserAccount user) {
Set<String> roleNames = roles.findByIdUserId(user.getId()).stream().map(role -> role.role().name()).collect(Collectors.toSet());
return new AuthService.UserView(user.getId(), user.getUsername(), user.getDisplayName(), user.getSchoolId(),
user.getStatus().name(), roleNames);
}
private String blank(String value) {
return value == null || value.isBlank() ? null : value.trim();
}
}