1. 登录时自动升级旧密码哈希改为只更新 password_hash,不再误清 need_change_password 标记 2. need_change_password 语义明确为"密码待修改":创建/重置置 1,用户改密成功清 0,登录自动升级哈希不影响 3. 首次登录弹窗与文档文案由"首次登录"改为"密码待修改(首次使用/被重置后)"
163 lines
6.5 KiB
Python
163 lines
6.5 KiB
Python
# ===========================================
|
|
# 班级操行分管理系统 - 后端服务
|
|
#
|
|
# 开发者: Canglan
|
|
# 联系方式: admin@sea-studio.top
|
|
# 版权归属: Sea Network Technology Studio
|
|
# 许可证: MIT License
|
|
#
|
|
# 版权所有 © Sea Network Technology Studio
|
|
# ===========================================
|
|
|
|
from typing import Optional
|
|
from utils.database import execute_one, execute_insert, execute_update
|
|
from utils.security import security
|
|
from utils.logger import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
class UserModel:
|
|
"""用户数据模型"""
|
|
|
|
@staticmethod
|
|
async def get_by_username(username: str) -> dict:
|
|
"""根据用户名获取用户(登录主链路,不查询邮箱)"""
|
|
sql = """
|
|
SELECT user_id, username, password_hash, real_name, user_type,
|
|
student_id, status, need_change_password, last_login_time, last_login_ip
|
|
FROM users
|
|
WHERE username = %s AND status = 1
|
|
"""
|
|
return await execute_one(sql, (username,))
|
|
|
|
@staticmethod
|
|
async def get_by_user_id(user_id: int) -> dict:
|
|
"""根据用户ID获取用户(不查询邮箱)"""
|
|
sql = """
|
|
SELECT user_id, username, password_hash, real_name, user_type, student_id,
|
|
need_change_password, status
|
|
FROM users
|
|
WHERE user_id = %s
|
|
"""
|
|
return await execute_one(sql, (user_id,))
|
|
|
|
@staticmethod
|
|
async def get_email_by_user_id(user_id: int) -> Optional[str]:
|
|
"""获取用户绑定邮箱(仅找回密码/绑定邮箱功能使用)"""
|
|
result = await execute_one("SELECT email FROM users WHERE user_id = %s", (user_id,))
|
|
return result.get("email") if result else None
|
|
|
|
@staticmethod
|
|
async def create_student(username: str, password: str, real_name: str, student_id: int) -> int:
|
|
"""创建学生账号"""
|
|
password_hash = security.bcrypt_password(password)
|
|
sql = """
|
|
INSERT INTO users (username, password_hash, real_name, user_type, student_id, need_change_password)
|
|
VALUES (%s, %s, %s, 'student', %s, 1)
|
|
"""
|
|
return await execute_insert(sql, (username, password_hash, real_name, student_id))
|
|
|
|
@staticmethod
|
|
async def create_parent(username: str, password: str, real_name: str, student_id: int) -> int:
|
|
"""创建家长账号"""
|
|
password_hash = security.bcrypt_password(password)
|
|
sql = """
|
|
INSERT INTO users (username, password_hash, real_name, user_type, student_id, need_change_password)
|
|
VALUES (%s, %s, %s, 'parent', %s, 0)
|
|
"""
|
|
return await execute_insert(sql, (username, password_hash, real_name, student_id))
|
|
|
|
@staticmethod
|
|
async def create_admin(username: str, password: str, real_name: str, email: str = None) -> int:
|
|
"""创建管理员账号(不传邮箱时不写 email 列)"""
|
|
password_hash = security.bcrypt_password(password)
|
|
if email:
|
|
sql = """
|
|
INSERT INTO users (username, password_hash, real_name, email, user_type, need_change_password)
|
|
VALUES (%s, %s, %s, %s, 'admin', 1)
|
|
"""
|
|
return await execute_insert(sql, (username, password_hash, real_name, email))
|
|
sql = """
|
|
INSERT INTO users (username, password_hash, real_name, user_type, need_change_password)
|
|
VALUES (%s, %s, %s, 'admin', 1)
|
|
"""
|
|
return await execute_insert(sql, (username, password_hash, real_name))
|
|
|
|
@staticmethod
|
|
async def update_password(user_id: int, new_password: str) -> bool:
|
|
"""更新密码(加密失败会抛出异常;rowcount 为 0 时再确认用户是否存在)"""
|
|
try:
|
|
password_hash = security.bcrypt_password(new_password)
|
|
except Exception as e:
|
|
logger.error(f"密码加密失败: {e}")
|
|
raise
|
|
sql = """
|
|
UPDATE users
|
|
SET password_hash = %s, need_change_password = 0
|
|
WHERE user_id = %s
|
|
"""
|
|
result = await execute_update(sql, (password_hash, user_id))
|
|
if result > 0:
|
|
return True
|
|
exists = await execute_one("SELECT 1 FROM users WHERE user_id = %s", (user_id,))
|
|
return exists is not None
|
|
|
|
@staticmethod
|
|
async def upgrade_password_hash(user_id: int, password: str) -> bool:
|
|
"""仅升级密码哈希格式(bcrypt),不改变 need_change_password 标记
|
|
|
|
登录时自动升级旧哈希不等于用户主动改密,不能清除"密码待修改"标记。
|
|
"""
|
|
try:
|
|
password_hash = security.bcrypt_password(password)
|
|
except Exception as e:
|
|
logger.error(f"密码加密失败: {e}")
|
|
raise
|
|
result = await execute_update(
|
|
"UPDATE users SET password_hash = %s WHERE user_id = %s",
|
|
(password_hash, user_id)
|
|
)
|
|
return result > 0
|
|
|
|
@staticmethod
|
|
async def update_last_login(user_id: int, ip: str) -> None:
|
|
"""更新最后登录信息"""
|
|
sql = """
|
|
UPDATE users
|
|
SET last_login_time = NOW(), last_login_ip = %s
|
|
WHERE user_id = %s
|
|
"""
|
|
await execute_update(sql, (ip, user_id))
|
|
|
|
@staticmethod
|
|
async def get_by_username_any(username: str) -> dict:
|
|
"""根据用户名获取用户(含已禁用)"""
|
|
sql = """
|
|
SELECT user_id, username, password_hash, real_name, user_type,
|
|
student_id, status, need_change_password, last_login_time, last_login_ip
|
|
FROM users
|
|
WHERE username = %s
|
|
"""
|
|
return await execute_one(sql, (username,))
|
|
|
|
@staticmethod
|
|
async def update_status(user_id: int, status: int) -> bool:
|
|
"""更新用户状态(0=禁用,1=启用)"""
|
|
sql = "UPDATE users SET status = %s WHERE user_id = %s"
|
|
result = await execute_update(sql, (status, user_id))
|
|
return result > 0
|
|
|
|
@staticmethod
|
|
async def update_real_name(user_id: int, real_name: str) -> bool:
|
|
"""更新用户真实姓名"""
|
|
sql = "UPDATE users SET real_name = %s WHERE user_id = %s"
|
|
result = await execute_update(sql, (real_name, user_id))
|
|
return result > 0
|
|
|
|
@staticmethod
|
|
async def update_email(user_id: int, email: str) -> bool:
|
|
"""更新用户绑定邮箱(传空字符串解除绑定)"""
|
|
sql = "UPDATE users SET email = %s WHERE user_id = %s"
|
|
result = await execute_update(sql, (email or None, user_id))
|
|
return result > 0 |