feat 超级管理员初始化密码随机生成 && 修改密码强制要求大写小写数字组合
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import base64
|
||||
import re
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
|
||||
@ -59,6 +60,10 @@ def update_user(
|
||||
"""
|
||||
user_info = user_in.dict()
|
||||
if user_info.get("password"):
|
||||
# 正则表达式匹配密码包含大写字母、小写字母、数字
|
||||
pattern = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]+$'
|
||||
if not re.match(pattern, user_info.get("password")):
|
||||
return schemas.Response(success=False, message="密码需包含大写小写数字")
|
||||
user_info["hashed_password"] = get_password_hash(user_info["password"])
|
||||
user_info.pop("password")
|
||||
user = User.get_by_name(db, name=user_info["name"])
|
||||
|
@ -35,8 +35,6 @@ class Settings(BaseSettings):
|
||||
CONFIG_DIR: str = None
|
||||
# 超级管理员
|
||||
SUPERUSER: str = "admin"
|
||||
# 超级管理员初始密码
|
||||
SUPERUSER_PASSWORD: str = "password"
|
||||
# API密钥,需要更换
|
||||
API_TOKEN: str = "moviepilot"
|
||||
# 登录页面电影海报,tmdb/bing
|
||||
|
@ -1,3 +1,6 @@
|
||||
import random
|
||||
import string
|
||||
|
||||
from alembic.command import upgrade
|
||||
from alembic.config import Config
|
||||
|
||||
@ -14,13 +17,24 @@ def init_db():
|
||||
"""
|
||||
# 全量建表
|
||||
Base.metadata.create_all(bind=Engine)
|
||||
|
||||
|
||||
def init_super_user():
|
||||
"""
|
||||
初始化超级管理员
|
||||
"""
|
||||
# 初始化超级管理员
|
||||
with SessionFactory() as db:
|
||||
_user = User.get_by_name(db=db, name=settings.SUPERUSER)
|
||||
if not _user:
|
||||
# 定义包含数字、大小写字母的字符集合
|
||||
characters = string.ascii_letters + string.digits
|
||||
# 生成随机密码
|
||||
random_password = ''.join(random.choice(characters) for _ in range(16))
|
||||
logger.info(f"初始化超级管理员随机密码 {random_password} 请登录系统后在设定中修改。 注:该密码只会显示一次,请注意保存。")
|
||||
_user = User(
|
||||
name=settings.SUPERUSER,
|
||||
hashed_password=get_password_hash(settings.SUPERUSER_PASSWORD),
|
||||
hashed_password=get_password_hash(random_password),
|
||||
is_superuser=True,
|
||||
)
|
||||
_user.create(db)
|
||||
|
@ -19,7 +19,7 @@ if SystemUtils.is_frozen():
|
||||
from app.core.config import settings
|
||||
from app.core.module import ModuleManager
|
||||
from app.core.plugin import PluginManager
|
||||
from app.db.init import init_db, update_db
|
||||
from app.db.init import init_db, update_db, init_super_user
|
||||
from app.helper.thread import ThreadHelper
|
||||
from app.helper.display import DisplayHelper
|
||||
from app.helper.resource import ResourceHelper
|
||||
@ -202,6 +202,8 @@ def start_module():
|
||||
start_frontend()
|
||||
# 检查认证状态
|
||||
check_auth()
|
||||
# 初始化超级管理员
|
||||
init_super_user()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Reference in New Issue
Block a user