| | |
| | | from app.models.app_token_model import AppToken |
| | | from app.models.base_model import get_db |
| | | from app.models.postgresql_base_model import get_pdb |
| | | from app.models.token_model import upsert_token |
| | | from app.models.token_model import upsert_token, update_token |
| | | from app.models.user import UserCreate, LoginData |
| | | from app.models.user_model import UserModel, UserAppModel |
| | | from app.service.auth import authenticate_user, create_access_token, is_valid_password, save_register_user, \ |
| | |
| | | from app.service.ragflow import RagflowService |
| | | from sqlalchemy.future import select |
| | | |
| | | from app.utils.password_handle import generate_password |
| | | |
| | | router = APIRouter() |
| | | |
| | |
| | | continue |
| | | try: |
| | | name = login_data.username |
| | | app_password = login_data.password |
| | | user_app = await UserAppDao(db).get_data_by_id(user.id, app["id"]) |
| | | if user_app: |
| | | name = user_app.username |
| | | app_password = user_app.decrypted_password(user_app.password) |
| | | else: |
| | | await update_user_info(db, user.id) |
| | | token = await service.login(name, login_data.password) |
| | | token = await service.login(name, app_password) |
| | | token_dict[app["id"]] = token |
| | | except Exception as e: |
| | | return Response(code=500, msg=f"Failed to login with {app['id']}: {str(e)}") |
| | |
| | | # 创建本地token |
| | | access_token = create_access_token(data={"sub": user.username, "user_id": user.id}) |
| | | |
| | | # await update_token(db, user.id, access_token, token_dict) |
| | | await update_token(db, user.id, access_token, token_dict) |
| | | await update_user_token(db, user.id, token_dict) |
| | | result = await pdb.execute(select(AppToken).where(AppToken.id == user.id)) |
| | | db_app_token = result.scalars().first() |
| | |
| | | app_register = AppRegisterDao(db).get_apps() |
| | | register_dict = {} |
| | | token = "" |
| | | app_password = await generate_password(10) |
| | | for app in app_register: |
| | | if app["id"] == RAGFLOW: |
| | | service = RagflowService(settings.fwr_base_url) |
| | |
| | | continue |
| | | try: |
| | | name = app["id"] + str(int(time.time())) |
| | | register_info = await service.register(name, user.password, token) |
| | | register_info = await service.register(name, app_password, token) |
| | | # print(register_info) |
| | | register_dict[app['id']] = {"id":register_info.get("id"), "name": name, "email": register_info.get("email")} |
| | | except Exception as e: |
| | | return Response(code=500, msg=f"Failed to register with {app['id']}: {str(e)}") |
| | | user_id = await save_register_user(db, user.username, user.password, user.email, register_dict) |
| | | user_id = await save_register_user(db, user.username, user.password, user.email, app_password, register_dict) |
| | | if not user_id: |
| | | return Response(code=500, msg=f"Failed to register with app") |
| | | is_update = await update_user_group(db, user_id) |
| | | return Response(code=200, msg="User registered successfully",data={"username": user.username}) |
| | | return Response(code=200, msg="User registered successfully",data={"userFlag": user_id}) |
| | | |
| | | |
| | | @router.get("/v2/sync", response_model=Response) |
| | | async def sync_user_tenant(userFlag: str, db=Depends(get_db)): |
| | | app_register = AppRegisterDao(db).get_app_by_id(RAGFLOW) |
| | | if app_register: |
| | | is_update = await update_user_group(db, userFlag) |
| | | return Response(code=200, msg="success", data={}) |