F2-10: 用户账号写端点(建用户/改密/停用启用)
platform-auth 新增 AdminUserController:POST /api/admin/users/create、
/{id}/update、/{id}/toggle-state;SysUserService 同步停用逻辑;
仅系统管理员可调,禁止停用 admin 账号,密码列不下发。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,235 @@
|
|||||||
|
package com.eip4.platform.auth.security;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import com.dsasp.server.common.user.service.LoggedInUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F2-10 用户管理写端点(位于 platform-auth,直接使用 PasswordHasher/JdbcTemplate)。
|
||||||
|
*
|
||||||
|
* <p>端点清单(均需系统管理员身份):
|
||||||
|
* <ul>
|
||||||
|
* <li>{@code POST /api/admin/users/create} — 新建用户(初始密码=MD5("110"))</li>
|
||||||
|
* <li>{@code POST /api/admin/users/{id}/update} — 编辑用户姓名(密码字段忽略)</li>
|
||||||
|
* <li>{@code POST /api/admin/users/{id}/toggle-state} — 停用/启用(state 1=激活 2=停用)</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <p>安全:userId='ADMIN' 或 account='admin' 才允许,否则 403。
|
||||||
|
* 审计:SLF4J 记录操作人+目标+动作。密码列绝不下发。SQL 全参数化。
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/admin")
|
||||||
|
public class AdminUserController {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(AdminUserController.class);
|
||||||
|
|
||||||
|
/** 系统默认初始密码:MD5("110"),与 framework 登录口径一致。 */
|
||||||
|
static final String DEFAULT_PASSWORD = PasswordHasher.md5Hex("110");
|
||||||
|
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
public AdminUserController(JdbcTemplate jdbcTemplate) {
|
||||||
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新建用户(初始密码=MD5("110"),密码列不下发)。
|
||||||
|
*
|
||||||
|
* @param body 包含 account(必填)、name(必填)、userorgid(选填)的 JSON 对象
|
||||||
|
*/
|
||||||
|
@PostMapping("/users/create")
|
||||||
|
public ResponseEntity<Map<String, Object>> createUser(@RequestBody Map<String, Object> body) {
|
||||||
|
requireAdmin();
|
||||||
|
|
||||||
|
String account = requireString(body, "account", "账号不能为空");
|
||||||
|
String name = requireString(body, "name", "姓名不能为空");
|
||||||
|
String orgId = body.get("userorgid") != null ? body.get("userorgid").toString().trim() : null;
|
||||||
|
|
||||||
|
Integer existing = jdbcTemplate.queryForObject(
|
||||||
|
"SELECT COUNT(*) FROM sys_user WHERE TRIM(account)=?", Integer.class, account.trim());
|
||||||
|
if (existing != null && existing > 0) {
|
||||||
|
return badRequest("账号已存在:" + account);
|
||||||
|
}
|
||||||
|
|
||||||
|
String newId = UUID.randomUUID().toString();
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
String operator = currentUserId();
|
||||||
|
|
||||||
|
jdbcTemplate.update(
|
||||||
|
"INSERT INTO sys_user(sys_user_id, account, name, password, userorgid,"
|
||||||
|
+ " state, isactive, version, createtime, createby, updatetime, updateby)"
|
||||||
|
+ " VALUES(?,?,?,?,?,1,1,0,?,?,?,?)",
|
||||||
|
newId, account.trim(), name.trim(), DEFAULT_PASSWORD, orgId,
|
||||||
|
now, operator, now, operator);
|
||||||
|
|
||||||
|
log.info("[F2-10][用户新建] operator={} newUserId={} account={}", operator, newId, account.trim());
|
||||||
|
return ResponseEntity.ok(okWithId("用户新建成功", newId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编辑用户姓名(密码字段即使出现在请求体中也被忽略,绝不写入)。
|
||||||
|
*
|
||||||
|
* @param id SYS_USER.SYS_USER_ID
|
||||||
|
* @param body 包含 name(必填)的 JSON 对象
|
||||||
|
*/
|
||||||
|
@PostMapping("/users/{id}/update")
|
||||||
|
public ResponseEntity<Map<String, Object>> updateUser(
|
||||||
|
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||||
|
requireAdmin();
|
||||||
|
|
||||||
|
String name = requireString(body, "name", "姓名不能为空");
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
String operator = currentUserId();
|
||||||
|
|
||||||
|
int rows = jdbcTemplate.update(
|
||||||
|
"UPDATE sys_user SET name=?, updatetime=?, updateby=?, version=version+1"
|
||||||
|
+ " WHERE TRIM(sys_user_id)=? AND isactive=1",
|
||||||
|
name.trim(), now, operator, id.trim());
|
||||||
|
|
||||||
|
if (rows == 0) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("用户不存在或已停用: " + id));
|
||||||
|
}
|
||||||
|
log.info("[F2-10][用户编辑] operator={} userId={} name={}", operator, id.trim(), name.trim());
|
||||||
|
return ResponseEntity.ok(ok("用户信息更新成功"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停用/启用用户(切换 state:1=激活 ↔ 2=停用)。
|
||||||
|
*
|
||||||
|
* <p>禁止停用 admin 账号本身,防止系统锁死。
|
||||||
|
*
|
||||||
|
* @param id SYS_USER.SYS_USER_ID
|
||||||
|
* @param body {@code {state: 1|2}}
|
||||||
|
*/
|
||||||
|
@PostMapping("/users/{id}/toggle-state")
|
||||||
|
public ResponseEntity<Map<String, Object>> toggleUserState(
|
||||||
|
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||||
|
requireAdmin();
|
||||||
|
|
||||||
|
Object stateObj = body.get("state");
|
||||||
|
if (stateObj == null) {
|
||||||
|
return badRequest("state 字段不能为空(1=激活, 2=停用)");
|
||||||
|
}
|
||||||
|
int state;
|
||||||
|
try {
|
||||||
|
state = Integer.parseInt(stateObj.toString());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return badRequest("state 必须为整数(1=激活, 2=停用)");
|
||||||
|
}
|
||||||
|
if (state != 1 && state != 2) {
|
||||||
|
return badRequest("state 值非法,必须为 1(激活)或 2(停用)");
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer isAdminAccount = jdbcTemplate.queryForObject(
|
||||||
|
"SELECT COUNT(*) FROM sys_user WHERE TRIM(sys_user_id)=? AND TRIM(account)='admin'",
|
||||||
|
Integer.class, id.trim());
|
||||||
|
if (isAdminAccount != null && isAdminAccount > 0 && state == 2) {
|
||||||
|
return badRequest("禁止停用 admin 账号");
|
||||||
|
}
|
||||||
|
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
String operator = currentUserId();
|
||||||
|
|
||||||
|
int rows = jdbcTemplate.update(
|
||||||
|
"UPDATE sys_user SET state=?, updatetime=?, updateby=?, version=version+1"
|
||||||
|
+ " WHERE TRIM(sys_user_id)=? AND isactive=1",
|
||||||
|
state, now, operator, id.trim());
|
||||||
|
|
||||||
|
if (rows == 0) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("用户不存在: " + id));
|
||||||
|
}
|
||||||
|
String action = state == 2 ? "停用" : "启用";
|
||||||
|
log.info("[F2-10][用户{}] operator={} userId={}", action, operator, id.trim());
|
||||||
|
return ResponseEntity.ok(ok("用户" + action + "成功"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================================================ 内部工具
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理员身份检查:userId='ADMIN' 或对应 account='admin',否则抛 403。
|
||||||
|
*/
|
||||||
|
private void requireAdmin() {
|
||||||
|
java.util.Properties ctx = getCtx();
|
||||||
|
if (ctx == null) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "未登录,禁止访问系统管理接口");
|
||||||
|
}
|
||||||
|
String cu = ctx.getProperty(LoggedInUser.CURRENTUSER, "").trim();
|
||||||
|
if (cu.isEmpty()) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "未登录,禁止访问系统管理接口");
|
||||||
|
}
|
||||||
|
if ("ADMIN".equalsIgnoreCase(cu)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Integer cnt = jdbcTemplate.queryForObject(
|
||||||
|
"SELECT COUNT(*) FROM sys_user WHERE TRIM(sys_user_id)=? AND TRIM(account)='admin'",
|
||||||
|
Integer.class, cu);
|
||||||
|
if (cnt == null || cnt == 0) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "仅系统管理员可执行此操作");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String currentUserId() {
|
||||||
|
java.util.Properties ctx = getCtx();
|
||||||
|
if (ctx == null) return "UNKNOWN";
|
||||||
|
String u = ctx.getProperty(LoggedInUser.CURRENTUSER, "UNKNOWN");
|
||||||
|
return u.isBlank() ? "UNKNOWN" : u.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static java.util.Properties getCtx() {
|
||||||
|
try {
|
||||||
|
return com.dsasp.server.common.util.UserSession.get();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String requireString(Map<String, Object> body, String key, String errMsg) {
|
||||||
|
Object val = body.get(key);
|
||||||
|
if (val == null || val.toString().isBlank()) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, errMsg);
|
||||||
|
}
|
||||||
|
return val.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ResponseEntity<Map<String, Object>> badRequest(String msg) {
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(fail(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> fail(String msg) {
|
||||||
|
Map<String, Object> r = new LinkedHashMap<>();
|
||||||
|
r.put("status", 1);
|
||||||
|
r.put("msg", msg);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> ok(String msg) {
|
||||||
|
Map<String, Object> r = new LinkedHashMap<>();
|
||||||
|
r.put("status", 0);
|
||||||
|
r.put("msg", msg);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> okWithId(String msg, String id) {
|
||||||
|
Map<String, Object> r = new LinkedHashMap<>();
|
||||||
|
r.put("status", 0);
|
||||||
|
r.put("msg", msg);
|
||||||
|
Map<String, Object> data = new LinkedHashMap<>();
|
||||||
|
data.put("id", id);
|
||||||
|
r.put("data", data);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,7 +32,7 @@ public class SysUserService {
|
|||||||
/** SYS_USER 取用户:按账号查(参数化),仅取认证与上下文所需列。
|
/** SYS_USER 取用户:按账号查(参数化),仅取认证与上下文所需列。
|
||||||
* 注:MASTERDEPART 列在实际库中不存在(侦察源库确认),depart 字段固定为 null。 */
|
* 注:MASTERDEPART 列在实际库中不存在(侦察源库确认),depart 字段固定为 null。 */
|
||||||
private static final String SQL_FIND_BY_ACCOUNT =
|
private static final String SQL_FIND_BY_ACCOUNT =
|
||||||
"SELECT SYS_USER_ID, ACCOUNT, PASSWORD, NAME, USERORGID "
|
"SELECT SYS_USER_ID, ACCOUNT, PASSWORD, NAME, USERORGID, STATE "
|
||||||
+ "FROM SYS_USER WHERE ACCOUNT = ?";
|
+ "FROM SYS_USER WHERE ACCOUNT = ?";
|
||||||
|
|
||||||
/** 角色解析:用户→用户组→角色(参数化,复刻 RoleServiceImpl 的 join)。 */
|
/** 角色解析:用户→用户组→角色(参数化,复刻 RoleServiceImpl 的 join)。 */
|
||||||
@@ -65,13 +65,18 @@ public class SysUserService {
|
|||||||
rs.getString("PASSWORD"),
|
rs.getString("PASSWORD"),
|
||||||
rs.getString("NAME"),
|
rs.getString("NAME"),
|
||||||
null, // MASTERDEPART 列在实际库中不存在
|
null, // MASTERDEPART 列在实际库中不存在
|
||||||
trim(rs.getString("USERORGID"))), account);
|
trim(rs.getString("USERORGID")),
|
||||||
|
rs.getInt("STATE")), account);
|
||||||
} catch (EmptyResultDataAccessException e) {
|
} catch (EmptyResultDataAccessException e) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
if (row == null || !PasswordHasher.matches(row.password(), rawPassword)) {
|
if (row == null || !PasswordHasher.matches(row.password(), rawPassword)) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
// STATE=2 表示停用,停用账号拒绝登录(F2-10)
|
||||||
|
if (row.state() == 2) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
List<String> roles = findRoles(row.userId());
|
List<String> roles = findRoles(row.userId());
|
||||||
return Optional.of(new AuthPrincipal(
|
return Optional.of(new AuthPrincipal(
|
||||||
row.userId(), row.account(), row.name(), row.depart(), row.org(), roles));
|
row.userId(), row.account(), row.name(), row.depart(), row.org(), roles));
|
||||||
@@ -99,9 +104,9 @@ public class SysUserService {
|
|||||||
return s == null ? null : s.trim();
|
return s == null ? null : s.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** SYS_USER 行投影。char(36) 列 PG 返回带尾空格,统一 trim。 */
|
/** SYS_USER 行投影。char(36) 列 PG 返回带尾空格,统一 trim。state=2 表示停用。 */
|
||||||
private record UserRow(
|
private record UserRow(
|
||||||
String userId, String account, String password,
|
String userId, String account, String password,
|
||||||
String name, String depart, String org) {
|
String name, String depart, String org, int state) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user