Commit 40d07faa by 江名洋

feat(auth): 补齐缺失默认账号并新增操作员

1 parent 41e8f404
......@@ -34,9 +34,10 @@ LOCK_AFTER_FAIL_COUNT = 5 # 失败 5 次后锁定
LOCK_DURATION_MINUTES = 10 # 锁定 10 分钟
CAPTCHA_EXPIRE_MINUTES = 5 # 验证码有效期 5 分钟
# 默认账号(应用启动时若用户表为空则自动创建)
# 默认账号(启动时自动补齐缺失的账号)
DEFAULT_USERS = [
{"username": "admin001", "password": "Admin@123456", "is_superuser": True},
{"username": "oper001", "password": "Oper@060710", "is_superuser": False},
]
......@@ -47,25 +48,26 @@ class AuthService:
@staticmethod
def init_default_users(db: Session) -> None:
"""若用户表为空,则初始化服务端固定账号"""
count = db.query(User).count()
if count > 0:
return
"""补齐缺失的默认账号(已存在的不重复创建)"""
existing_usernames = {u[0] for u in db.query(User.username).all()}
for item in DEFAULT_USERS:
if not validate_username(item["username"]):
logger.warning(f"默认账号 {item['username']} 不符合规则,跳过")
username = item["username"]
if username in existing_usernames:
continue
if not validate_username(username):
logger.warning(f"默认账号 {username} 不符合规则,跳过")
continue
if not validate_password(item["password"]):
logger.warning(f"默认账号 {item['username']} 密码不符合规则,跳过")
logger.warning(f"默认账号 {username} 密码不符合规则,跳过")
continue
user = User(
username=item["username"],
username=username,
hashed_password=get_password_hash(item["password"]),
is_active=True,
is_superuser=item.get("is_superuser", False),
)
db.add(user)
logger.info(f"创建默认账号: {item['username']}")
logger.info(f"创建默认账号: {username}")
db.commit()
# ========== 验证码 ==========
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!