| | |
| | | from app.service.difyService import DifyService |
| | | from app.service.ragflow import RagflowService |
| | | from app.service.service_token import get_ragflow_token, get_bisheng_token, get_new_token, get_dify_token |
| | | from app.utils.password_handle import generate_password |
| | | |
| | | |
| | | async def get_user_list(db, page_index: int, page_size: int, keyword: str, role_key: str, user_id): |
| | |
| | | # query.filter(UserModel.creator==user_id) |
| | | if keyword: |
| | | query = query.filter(UserModel.username.like('%{}%'.format(keyword))) |
| | | total = query.count() |
| | | users = query.order_by(UserModel.id.desc()).limit(page_size).offset( |
| | | (page_index - 1) * page_size).all() |
| | | return {"total": query.count(), "rows": [user.to_json() for user in users]} |
| | | return {"total": total, "rows": [user.to_json() for user in users]} |
| | | |
| | | |
| | | async def edit_user_status(db, status: str, user_id: int): |
| | |
| | | |
| | | async def create_user(db, user_name, email, phone, login_name, password, roles, groups, user_id): |
| | | try: |
| | | # bisheng_service = BishengService(settings.sgb_base_url) |
| | | # ragflow_service = RagflowService(settings.fwr_base_url) |
| | | # |
| | | # # 注册到毕昇 |
| | | # try: |
| | | # bisheng_info = await bisheng_service.register(user_name, password) |
| | | # except Exception as e: |
| | | # logger.error(f"Failed to register with Bisheng: {str(e)}") |
| | | # return False |
| | | # |
| | | # # 注册到ragflow |
| | | # try: |
| | | # ragflow_info = await ragflow_service.register(user_name, password) |
| | | # except Exception as e: |
| | | # logger.error(f"Failed to register with Ragflow: {str(e)}") |
| | | # return False |
| | | app_register = AppRegisterDao(db).get_apps() |
| | | register_dict = {} |
| | | token = "" |
| | | app_password = generate_password() |
| | | for app in app_register: |
| | | if app["id"] == RAGFLOW: |
| | | service = RagflowService(settings.fwr_base_url) |
| | | elif app["id"] == BISHENG: |
| | | service = BishengService(settings.sgb_base_url) |
| | | elif app["id"] == DIFY: |
| | | token = await get_dify_token() |
| | | token = await get_dify_token(db, user_id) |
| | | service = DifyService(settings.dify_base_url) |
| | | else: |
| | | logger.error("未知注册应用---") |
| | | continue |
| | | try: |
| | | name = app["id"] + str(int(time.time())) |
| | | register_info = await service.register(name, 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")} |
| | |
| | | user_model = UserModel(username=user_name, hashed_password=hashed_password, email=email, |
| | | ## ragflow_id=ragflow_info.get("id"),bisheng_id=bisheng_info.get("user_id"), |
| | | phone=phone, login_name=login_name) |
| | | pwd = user_model.encrypted_password(password) |
| | | pwd = user_model.encrypted_password(app_password) |
| | | user_model.roles = [db.get(RoleModel, roleId) for roleId in roles] |
| | | user_model.password = pwd |
| | | if groups: |
| | |
| | | user.email = email |
| | | user.updated_at = datetime.now() |
| | | user.roles = [db.get(RoleModel, roleId) for roleId in roles] |
| | | user.groups = [db.get(GroupModel, groupId) for groupId in groups] |
| | | # user.groups = [db.get(GroupModel, groupId) for groupId in groups] |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | |
| | | async def edit_user_pwd(db, user_id, current_user_id, new_password="basic123456"): |
| | | try: |
| | | user = db.query(UserModel).filter(UserModel.id == user_id).first() |
| | | pwd = user.decrypted_password() |
| | | for app in AppRegisterDao(db).get_apps(): |
| | | if app.get("id") == RAGFLOW: |
| | | token = await get_new_token(db, user_id, app.get("id")) |
| | | ragflow_service = RagflowService(settings.fwr_base_url) |
| | | await ragflow_service.set_user_password(token, pwd, new_password) |
| | | elif app.get("id") == BISHENG: |
| | | token = await get_bisheng_token(db, current_user_id) |
| | | bisheng_service = BishengService(settings.sgb_base_url) |
| | | await bisheng_service.change_password_public(token, user.username, pwd, new_password) |
| | | else: |
| | | logger.error("注册未知应用:{}".format(app.get("id"))) |
| | | # pwd = user.decrypted_password() |
| | | # for app in AppRegisterDao(db).get_apps(): |
| | | # if app.get("id") == RAGFLOW: |
| | | # token = await get_new_token(db, user_id, app.get("id")) |
| | | # ragflow_service = RagflowService(settings.fwr_base_url) |
| | | # await ragflow_service.set_user_password(token, pwd, new_password) |
| | | # elif app.get("id") == BISHENG: |
| | | # token = await get_bisheng_token(db, current_user_id) |
| | | # bisheng_service = BishengService(settings.sgb_base_url) |
| | | # await bisheng_service.change_password_public(token, user.username, pwd, new_password) |
| | | # else: |
| | | # logger.error("注册未知应用:{}".format(app.get("id"))) |
| | | user.hashed_password = pwd_context.hash(new_password) |
| | | user.password = user.encrypted_password(new_password) |
| | | # user.password = user.encrypted_password(new_password) |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | | db.rollback() |
| | | return False |
| | | return True |
| | | |
| | | async def change_user_pwd(db, user_id, new_password): |
| | | try: |
| | | user = db.query(UserModel).filter(UserModel.id == user_id).first() |
| | | user.hashed_password = pwd_context.hash(new_password) |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | | db.rollback() |
| | | return False |
| | | return True |
| | | |
| | | |
| | | |
| | | async def get_user_info(db, user_id): |
| | |
| | | if parent_ogt.id not in dept_set: |
| | | await role_resource(role_set, permissions, parent_ogt.roles) |
| | | dept_set.add(parent_ogt.id) |
| | | parent_ogt = parent_ogt.parent |
| | | parent_ogt = parent_ogt.parent |
| | | else: |
| | | break |
| | | tmp_dit = {} |
| | | for permission in permissions.values(): |
| | | tmp_dit[permission["parentId"]] = tmp_dit.get(permission["parentId"], []) + [permission] |
| | |
| | | |
| | | async def get_user_menus(db, user_id): |
| | | dialog_list = [] |
| | | agent_list = [] |
| | | menu_dict = {} |
| | | res = [] |
| | | user = db.query(UserModel).filter_by(id=user_id).first() |
| | | for group in user.groups: |
| | | for dialog in group.dialogs: |
| | | dialog_list.append(dialog.id) |
| | | for agent in group.agents: |
| | | agent_list.append(agent.id) |
| | | menu_list = db.query(WebMenuModel.id, WebMenuModel.title, WebMenuModel.describe, WebMenuModel.icon, WebMenuModel.desc, |
| | | WebMenuModel.img, MenuCapacityModel.capacity_id, MenuCapacityModel.capacity_type, DialogModel.agent_id.label("agentId")).outerjoin( |
| | | menu_list = db.query(WebMenuModel.id, WebMenuModel.title, WebMenuModel.describe, WebMenuModel.icon, WebMenuModel.desc,WebMenuModel.rank, |
| | | WebMenuModel.img, MenuCapacityModel.capacity_id, MenuCapacityModel.capacity_type, MenuCapacityModel.chat_id.label("agentId")).outerjoin( |
| | | MenuCapacityModel, WebMenuModel.id == MenuCapacityModel.menu_id).outerjoin( |
| | | DialogModel, MenuCapacityModel.capacity_id == DialogModel.id).filter(DialogModel.status=="1").all() |
| | | |
| | |
| | | continue |
| | | elif not m.capacity_type or m.capacity_type == 1 and m.capacity_id not in dialog_list: |
| | | break |
| | | elif not m.capacity_type or m.capacity_type == 2 and m.capacity_id not in agent_list: |
| | | break |
| | | else: |
| | | res.append({ |
| | | 'id': menus[0].id, |
| | |
| | | 'img': menus[0].img, |
| | | 'desc': menus[0].desc, |
| | | 'dialog': menus[0].describe, |
| | | 'agentId': menus[0].agentId |
| | | 'agentId': menus[0].agentId, |
| | | 'rank': menus[0].rank |
| | | }) |
| | | return res |
| | | return sorted(res, key=lambda x: x['rank'], reverse=True) |
| | | |
| | | |
| | | async def get_user_permission(db, user_id): |
| | |
| | | knowledge_dict = {} |
| | | user = db.query(UserModel).filter_by(id=user_id).first() |
| | | parent_id = "" |
| | | print(111111111111111) |
| | | # print(111111111111111) |
| | | async def role_resource(role_set, permissions, roles): |
| | | nonlocal parent_id |
| | | for role in roles: |
| | |
| | | await role_resource(role_set, permissions, parent_ogt.roles) |
| | | dept_set.add(parent_ogt.id) |
| | | |
| | | parent_ogt = parent_ogt.parent |
| | | parent_ogt = parent_ogt.parent |
| | | else: |
| | | break |
| | | |
| | | tmp_dit = {} |
| | | for permission in permissions.values(): |
| | |
| | | res = {} |
| | | user = db.query(UserModel).filter_by(id=user_id).first() |
| | | res["rows"] = [i.to_dict() for i in user.organizations] |
| | | return res |
| | | return res |
| | | |
| | | |
| | | async def user_data_service(db, user_id): |
| | | user = db.query(UserModel).filter_by(id=user_id).first() |
| | | |
| | | |
| | | return {"roles": [i.to_dict() for i in user.roles], "user": user.to_dict()} |