feat: fix table alignment in product view and implement self-service password update with role masking

This commit is contained in:
DXC
2026-03-23 11:41:09 +08:00
parent f701ed7fc8
commit ec5331ffb3
4 changed files with 393 additions and 44 deletions

View File

@ -249,3 +249,109 @@ def get_my_permissions():
except Exception as e:
current_app.logger.error(f"Get Permissions Failed: {str(e)}")
return jsonify({'msg': '获取权限失败'}), 500
@auth_bp.route('/me/password', methods=['PUT'])
@jwt_required()
def change_my_password():
"""
【新增】自我修改密码接口
- 无需管理员权限,只需验证 JWT Token 和旧密码是否正确
- 字段脱敏:不暴露系统角色
"""
try:
from app.models.system import SysUser
claims = get_jwt()
user_id = claims.get('sub')
data = request.get_json()
if not data:
return jsonify({'msg': '无效的请求数据'}), 400
old_password = data.get('old_password')
new_password = data.get('new_password')
confirm_password = data.get('confirm_password')
if not old_password or not new_password or not confirm_password:
return jsonify({'msg': '旧密码、新密码、确认新密码均不能为空'}), 400
if new_password != confirm_password:
return jsonify({'msg': '新密码与确认密码不一致'}), 400
if len(new_password) < 6:
return jsonify({'msg': '新密码长度不能少于6位'}), 400
# 超级管理员user_id=0使用硬编码密码
if user_id == 0:
if old_password != AuthService.SUPER_ADMIN_PASS:
return jsonify({'msg': '旧密码错误'}), 401
# 超级管理员密码不存入数据库直接返回成功IRIS 使用固定密码)
# 注:如果需要支持 IRIS 修改密码,可在此添加特殊逻辑
return jsonify({'msg': '超级管理员密码由系统管理员管理,当前会话无需修改'}), 200
# 普通用户:从数据库验证旧密码
user = SysUser.query.get(user_id)
if not user:
return jsonify({'msg': '用户不存在'}), 404
if not user.check_password(old_password):
return jsonify({'msg': '旧密码错误'}), 401
user.set_password(new_password)
db.session.commit()
return jsonify({'msg': '密码修改成功,请使用新密码重新登录'}), 200
except Exception as e:
current_app.logger.error(f"Change Password Failed: {str(e)}")
return jsonify({'msg': f'密码修改失败: {str(e)}'}), 500
@auth_bp.route('/me', methods=['GET'])
@jwt_required()
def get_my_profile():
"""
【新增】获取当前登录用户的个人资料(自我查看)
- 只返回姓名/账号和所属部门
- 严格脱敏:不暴露系统角色字段
"""
try:
from app.models.system import SysUser
claims = get_jwt()
user_id = claims.get('sub')
display_name = claims.get('display_name', '')
account_id = claims.get('username', '')
# 超级管理员user_id=0
if user_id == 0:
return jsonify({
'msg': '获取成功',
'data': {
'id': 0,
'username': 'IRIS',
'display_name': '超级管理员(IRIS)',
'department': 'System',
# 【关键】严格脱敏:不暴露 role 字段
}
}), 200
user = SysUser.query.get(user_id)
if not user:
return jsonify({'msg': '用户不存在'}), 404
return jsonify({
'msg': '获取成功',
'data': {
'id': user.id,
'username': account_id,
'display_name': user.username.split('/')[0] if user.username else display_name,
'department': user.department or '-',
# 【关键】严格脱敏:不暴露 role 字段
}
}), 200
except Exception as e:
current_app.logger.error(f"Get Profile Failed: {str(e)}")
return jsonify({'msg': f'获取个人资料失败: {str(e)}'}), 500