进入界面的调整
This commit is contained in:
@ -30,14 +30,12 @@ class AuthService:
|
||||
'department': 'System'
|
||||
}
|
||||
else:
|
||||
# [修改] 使用 ValueError 表示认证失败
|
||||
raise ValueError("密码错误")
|
||||
|
||||
# 2. 如果不是 IRIS,检查数据库用户
|
||||
else:
|
||||
user = SysUser.query.filter_by(username=username).first()
|
||||
|
||||
# [修改] 分开判断,逻辑更清晰,且使用 ValueError
|
||||
if not user:
|
||||
raise ValueError("用户不存在")
|
||||
|
||||
@ -67,21 +65,17 @@ class AuthService:
|
||||
"""
|
||||
创建新用户 (仅限管理员使用)
|
||||
"""
|
||||
# 简单权限控制:只有超级管理员或主管可以创建用户
|
||||
if operator_role not in [UserRole.SUPER_ADMIN, UserRole.SUPERVISOR]:
|
||||
raise Exception("权限不足:只有超级管理员或主管可以创建新用户")
|
||||
|
||||
# 检查重名
|
||||
if SysUser.query.filter_by(username=data.get('username')).first():
|
||||
raise Exception("用户名已存在")
|
||||
|
||||
# 默认角色处理
|
||||
role = data.get('role')
|
||||
valid_roles = [v for k, v in UserRole.__dict__.items() if not k.startswith('__')]
|
||||
if role not in valid_roles:
|
||||
raise Exception(f"角色无效,可选角色: {valid_roles}")
|
||||
|
||||
# 处理 Email 为空的情况
|
||||
email = data.get('email', '')
|
||||
if email and SysUser.query.filter_by(email=email).first():
|
||||
raise Exception("邮箱已被使用")
|
||||
@ -100,6 +94,47 @@ class AuthService:
|
||||
|
||||
return new_user.to_dict()
|
||||
|
||||
@staticmethod
|
||||
def update_user(user_id, data, operator_role):
|
||||
"""
|
||||
[新增] 更新用户信息
|
||||
"""
|
||||
if operator_role not in [UserRole.SUPER_ADMIN, UserRole.SUPERVISOR]:
|
||||
raise Exception("权限不足:只有超级管理员或主管可以修改用户信息")
|
||||
|
||||
user = SysUser.query.get(user_id)
|
||||
if not user:
|
||||
raise Exception("用户不存在")
|
||||
|
||||
# 1. 更新基本信息
|
||||
if 'role' in data:
|
||||
valid_roles = [v for k, v in UserRole.__dict__.items() if not k.startswith('__')]
|
||||
if data['role'] not in valid_roles:
|
||||
raise Exception(f"角色无效")
|
||||
user.role = data['role']
|
||||
|
||||
if 'department' in data:
|
||||
user.department = data['department']
|
||||
|
||||
if 'email' in data:
|
||||
# 如果修改了邮箱,且新邮箱已被其他人使用
|
||||
email = data['email']
|
||||
if email and email != user.email:
|
||||
existing = SysUser.query.filter_by(email=email).first()
|
||||
if existing:
|
||||
raise Exception("该邮箱已被其他用户使用")
|
||||
user.email = email
|
||||
|
||||
# 2. 如果提供了密码,则重置密码;否则保持原密码
|
||||
new_password = data.get('password')
|
||||
if new_password and str(new_password).strip():
|
||||
if len(new_password) < 6:
|
||||
raise Exception("密码长度至少6位")
|
||||
user.set_password(new_password)
|
||||
|
||||
db.session.commit()
|
||||
return user.to_dict()
|
||||
|
||||
@staticmethod
|
||||
def get_all_users():
|
||||
"""获取所有系统用户"""
|
||||
|
||||
Reference in New Issue
Block a user