zhaoqingang
2025-02-11 0078ac6ffa486c481c26b7c0b033ff9664096a7f
app/service/v2/initialize_data.py
@@ -1,8 +1,13 @@
import json
import time
import os
import yaml
from passlib.context import CryptContext
from Log import logger
from app.config.const import DIFY, ENV_CONF_PATH
# from app.api import pwd_context
from app.config.const import DIFY, ENV_CONF_PATH, RAGFLOW
from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \
    cipher_suite
from app.service.auth import UserAppDao
@@ -13,6 +18,8 @@
from app.service.v2.app_register import AppRegisterDao
from app.config.config import settings
from app.utils.password_handle import generate_password
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
async def dialog_menu_sync(db):
@@ -219,3 +226,37 @@
    if not user_id:
        raise Exception("Failed to register with app")
    print({"msg": "User registered successfully", "userFlag": user_id})
async def admin_user_sync(db):
    try:
        config = {}
        with open(os.path.join(ENV_CONF_PATH, "admin.yaml"), 'r', encoding='utf-8') as file:
            # 加载JSON数据
            config = yaml.safe_load(file)
        # print(config)
        db_user = db.query(UserModel).filter(UserModel.username == config["smart_server"]["account"]).first()
        if db_user:
            print("admin_user_sync: 用户已经存在!")
            return
        register_dict = {}
        for app in [RAGFLOW, DIFY]:
            register_dict[app] = {"id": config[app].get("id", "123"), "name": config[app]["account"],
                                        "pwd":config[app]["password"],
                                        "email": config[app]["account"]}
        # 存储用户信息
        hashed_password = pwd_context.hash(config["smart_server"]["password"])
        user_model = UserModel(username=config["smart_server"]["account"], hashed_password=hashed_password, email="",
                               phone="", login_name="", sync_flag="", creator=0, permission="admin")
        db.add(user_model)
        db.commit()
        db.refresh(user_model)
        u_id = user_model.id
        user_app_dao = UserAppDao(db)
        for k, v in register_dict.items():
            await user_app_dao.update_and_insert_data(v.get("name"), user_model.encrypted_password(v.get("pwd")), v.get("email"), u_id, str(v.get("id")), k)
    except Exception as e:
        print(e)
        db.rollback()