feat 超级管理员初始化密码随机生成 && 修改密码强制要求大写小写数字组合

This commit is contained in:
thsrite 2024-01-18 11:08:16 +08:00
parent 64233c89d7
commit a299d786fe
6 changed files with 25 additions and 9 deletions

View File

@ -98,8 +98,7 @@ MoviePilot需要配套下载器和媒体服务器配合使用。
下载 [app.env 模板](https://github.com/jxxghp/MoviePilot/raw/main/config/app.env)修改后放配置文件目录下app.env 的所有配置项也可以通过环境变量进行配置。
- **❗SUPERUSER** 超级管理员用户名,默认`admin`,安装后使用该用户登录后台管理界面,**注意:启动一次后再次修改该值不会生效,除非删除数据库文件!**
- **❗SUPERUSER_PASSWORD** 超级管理员初始密码,默认`password`,建议修改为复杂密码,**注意:启动一次后再次修改该值不会生效,除非删除数据库文件!**
- **❗SUPERUSER** 超级管理员用户名,默认`admin`,安装后使用该用户登录后台管理界面。`初始化超级管理员密码仅会生成一次,请在日志中查看并自行登录系统修改`。**注意:启动一次后再次修改该值不会生效,除非删除数据库文件!**
- **❗API_TOKEN** API密钥默认`moviepilot`在媒体服务器Webhook、微信回调等地址配置中需要加上`?token=`该值,建议修改为复杂字符串
- **BIG_MEMORY_MODE** 大内存模式,默认为`false`,开启后会增加缓存数量,占用更多的内存,但响应速度会更快
- **GITHUB_TOKEN** Github token提高自动更新、插件安装等请求Github Api的限流阈值格式ghp_****

View File

@ -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"])

View File

@ -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

View File

@ -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)

View File

@ -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__':

View File

@ -11,10 +11,8 @@ HOST=0.0.0.0
DEBUG=false
# 是否开发模式,打开后后台服务将不会启动
DEV=false
# 【*】超级管理员,设置后一但重启将固化到数据库中,修改将无效
# 【*】超级管理员,设置后一但重启将固化到数据库中,修改将无效(初始化超级管理员密码仅会生成一次,请在日志中查看并自行登录系统修改)
SUPERUSER=admin
# 【*】超级管理员初始密码,设置后一但重启将固化到数据库中,修改将无效
SUPERUSER_PASSWORD=password
# 大内存模式,开启后会增加缓存数量,但会占用更多内存
BIG_MEMORY_MODE=false
# 自动检查和更新站点资源包(索引、认证等)