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 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 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(); } }