feat: add unique super administrator and role management
This commit is contained in:
1 parent
9e731e47b9
commit
386d15cde2
16 files changed
+455
-24
No files matched your search
@@ -45,6 +45,8 @@ public class AuthController {
|
||||
|
||||
public record InvitationRequest(String label, Integer maxUses, OffsetDateTime expiresAt) {}
|
||||
|
||||
public record RoleRequest(String role) {}
|
||||
|
||||
@PostMapping("/auth/login")
|
||||
public Map<String, Object> login(
|
||||
@RequestBody LoginRequest input,
|
||||
@@ -86,6 +88,15 @@ public class AuthController {
|
||||
return Map.of("users", authService.listUsers(sessionToken(request)));
|
||||
}
|
||||
|
||||
@PostMapping("/admin/users/{id}/role")
|
||||
public Map<String, Object> changeRole(
|
||||
@PathVariable UUID id,
|
||||
@RequestBody RoleRequest input,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
return Map.of("user", authService.changeRole(sessionToken(request), id, input.role(), meta(request)));
|
||||
}
|
||||
|
||||
@GetMapping("/admin/invitations")
|
||||
public Map<String, Object> invitations(HttpServletRequest request) {
|
||||
return Map.of("invitations", authService.listInvitations(sessionToken(request)));
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -73,17 +74,31 @@ public class AuthService {
|
||||
@Transactional
|
||||
public void ensureAdmin(String accountInput, String password, String displayName) {
|
||||
String account = normalizeAccount(accountInput);
|
||||
if (account.isBlank() || password.isBlank()) {
|
||||
if (account.isBlank() || password == null || password.isBlank()) {
|
||||
throw new ApiException("INVALID_ADMIN_CONFIG", "管理员初始配置无效", 500);
|
||||
}
|
||||
if (findUserByAccount(account) != null) {
|
||||
// Serialize bootstrap across replicas, including the first creation.
|
||||
jdbc.execute("LOCK TABLE users IN SHARE ROW EXCLUSIVE MODE");
|
||||
List<String> owners = jdbc.queryForList("SELECT account FROM users WHERE role = 'super_admin'", String.class);
|
||||
if (!owners.isEmpty()) {
|
||||
if (!owners.getFirst().equals(account)) {
|
||||
throw new IllegalStateException("ADMIN_ACCOUNT differs from the existing super administrator; refusing to create another owner.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
UserRow existing = findUserByAccount(account);
|
||||
if (existing != null) {
|
||||
if (!"admin".equals(existing.role()) || !"active".equals(existing.status())) {
|
||||
throw new IllegalStateException("Bootstrap account must be an active administrator; refusing to promote an ordinary or disabled user.");
|
||||
}
|
||||
jdbc.update("UPDATE users SET role = 'super_admin', updated_at = ? WHERE id = ?", now(), existing.id());
|
||||
return;
|
||||
}
|
||||
OffsetDateTime now = now();
|
||||
jdbc.update(
|
||||
"INSERT INTO users (id, account, account_kind, password_hash, display_name, role, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'admin', 'active', ?, ?)",
|
||||
"INSERT INTO users (id, account, account_kind, password_hash, display_name, role, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'super_admin', 'active', ?, ?)",
|
||||
UUID.randomUUID(), account, accountKind(account), passwordHasher.hash(password),
|
||||
displayName == null || displayName.isBlank() ? "平台管理员" : displayName.trim(), now, now
|
||||
displayName == null || displayName.isBlank() ? "超级管理员" : displayName.trim(), now, now
|
||||
);
|
||||
}
|
||||
|
||||
@@ -155,12 +170,49 @@ public class AuthService {
|
||||
|
||||
public UserView requireAdmin(String token) {
|
||||
UserRow user = requireUserRow(token);
|
||||
if (!"admin".equals(user.role())) {
|
||||
if (!isAdminRole(user.role())) {
|
||||
throw new ApiException("FORBIDDEN", "需要管理员权限", 403);
|
||||
}
|
||||
return toView(user, null);
|
||||
}
|
||||
|
||||
public UserView requireSuperAdmin(String token) {
|
||||
UserRow user = requireUserRow(token);
|
||||
if (!"super_admin".equals(user.role())) {
|
||||
throw new ApiException("FORBIDDEN", "需要超级管理员权限", 403);
|
||||
}
|
||||
return toView(user, null);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserView changeRole(String token, UUID userId, String requestedRole, RequestMeta meta) {
|
||||
UserView actor = requireSuperAdmin(token);
|
||||
String role = requestedRole == null ? "" : requestedRole.trim().toLowerCase(Locale.ROOT);
|
||||
if (!role.equals("user") && !role.equals("admin")) {
|
||||
throw new ApiException("INVALID_ROLE", "只能授予普通用户或管理员角色", 400);
|
||||
}
|
||||
if (actor.id().equals(userId)) {
|
||||
throw new ApiException("SELF_ROLE_CHANGE", "超级管理员不能修改自己的角色", 400);
|
||||
}
|
||||
List<UserRow> targets = jdbc.query("SELECT * FROM users WHERE id = ? FOR UPDATE", this::mapUser, userId);
|
||||
if (targets.isEmpty()) {
|
||||
throw new ApiException("USER_NOT_FOUND", "用户不存在", 404);
|
||||
}
|
||||
UserRow target = targets.getFirst();
|
||||
if ("super_admin".equals(target.role())) {
|
||||
throw new ApiException("SUPER_ADMIN_PROTECTED", "超级管理员账号不可通过此接口降级", 403);
|
||||
}
|
||||
if (!role.equals(target.role())) {
|
||||
jdbc.update("UPDATE users SET role = ?, updated_at = ? WHERE id = ?", role, now(), userId);
|
||||
audit("user.role_changed", actor.id(), meta, Map.of("targetUserId", userId, "fromRole", target.role(), "role", role));
|
||||
}
|
||||
return toView(findUserById(userId), null);
|
||||
}
|
||||
|
||||
private boolean isAdminRole(String role) {
|
||||
return "admin".equals(role) || "super_admin".equals(role);
|
||||
}
|
||||
|
||||
public void logout(String token, RequestMeta meta) {
|
||||
if (token == null || token.isBlank()) {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user