From 25bf0a51088fa0d0acdc39c6096cc45d699f63a1 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期四, 13 二月 2025 10:53:23 +0800 Subject: [PATCH] merge master --- app/service/v2/initialize_data.py | 80 +++++++++++++++++++++++++++++++++++++-- 1 files changed, 75 insertions(+), 5 deletions(-) diff --git a/app/service/v2/initialize_data.py b/app/service/v2/initialize_data.py index 5d2043a..e315b4a 100644 --- a/app/service/v2/initialize_data.py +++ b/app/service/v2/initialize_data.py @@ -1,10 +1,16 @@ 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 + cipher_suite, UserTokenModel from app.service.auth import UserAppDao from app.service.bisheng import BishengService from app.service.difyService import DifyService @@ -13,6 +19,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): @@ -132,14 +140,15 @@ dialog.name = agent["name"] dialog.description = agent["description"] dialog.icon = agent["icon"] + dialog.mode = agent["mode"] + dialog.parameters = json.dumps(agent["parameters"]) db.commit() except Exception as e: logger.error(e) else: try: - dialog = DialogModel(id=agent["id"], name=agent["name"], description=agent["description"], - icon=agent["icon"], tenant_id=user.id if user else "", dialog_type="3", - agent_id=agent["id"]) + dialog = DialogModel(id=agent["id"], name=agent["name"], description=agent["description"], + icon=agent["icon"], tenant_id=user.id if user else "", dialog_type=agent["dialogType"], mode=agent["mode"],parameters = json.dumps(agent["parameters"])) db.add(dialog) db.commit() db.refresh(dialog) @@ -220,3 +229,64 @@ if not user_id: raise Exception("Failed to register with app") print({"msg": "User registered successfully", "userFlag": user_id}) + + +async def admin_account_sync(db): + try: + config = {} + now_account =[] + with open(os.path.join(ENV_CONF_PATH, "account.yaml"), 'r', encoding='utf-8') as file: + # 鍔犺浇JSON鏁版嵁 + config = yaml.safe_load(file) + account_list = db.query(UserTokenModel).all() + for account in account_list: + if account.id in config: + + if account.account != config[account.id]["account"] or account.password != config[account.id]["password"]: + db.query(UserTokenModel).filter_by(id=account.id).update({"account": config[account.id]["account"], + "password": config[account.id]["password"], + "access_token": "" + }) + now_account.append(account.id) + else: + db.query(UserTokenModel).filter_by(id=account.id).delete() + for k, v in config.items(): + if k not in now_account: + new_account = UserTokenModel(id=k, account=v["account"], password=v["password"]) + db.add(new_account) + db.commit() + except Exception as e: + print(e) + db.rollback() +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() \ No newline at end of file -- Gitblit v1.8.0