Dify智能體平臺源碼二次開發筆記(5) - 多租戶的SAAS版實現(2)

目錄

前言

用戶的查詢

controller層

添加路由

service層

用戶的添加

controller層

添加路由

service層-添加用戶

service層-添加用戶和租戶關系

驗證結果

結果


前言


完成租戶添加功能后,下一步需要實現租戶下的用戶管理。基礎功能包括:查詢租戶用戶列表接口,添加用戶接口

用戶的查詢

controller層
class AccountListApi(Resource):"""Resource for getting account list."""@validate_tokendef get(self):"""Get account list."""parser = reqparse.RequestParser()parser.add_argument("tenant_id", type=str, required=True, location="json")parser.add_argument("page", type=int, required=False, default=1, location="args")parser.add_argument("limit", type=int, required=False, default=20, location="args")parser.add_argument("search", type=str, required=False, location="args")args = parser.parse_args()accounts = AccountService.get_accounts_by_tenant(tenant_id=args["tenant_id"],page=args["page"],limit=args["limit"],search=args["search"])return {"result": "success","data": accounts}
添加路由
api.add_resource(AccountListApi, "/accounts")
service層
@staticmethoddef get_accounts_by_tenant(tenant_id: str, page: int = 1, limit: int = 20, search: str = None, status: str = None) -> dict:query = (db.session.query(Account, TenantAccountJoin.role).select_from(Account).join(TenantAccountJoin, Account.id == TenantAccountJoin.account_id).filter(TenantAccountJoin.tenant_id == tenant_id))if search:search = f"%{search}%"query = query.filter(db.or_(Account.name.ilike(search),Account.email.ilike(search)))if status:query = query.filter(Account.status == status)query = query.order_by(Account.name, Account.email)total = query.count()query = query.offset((page - 1) * limit).limit(limit)results = query.all()account_list = [{"id": str(account[0].id),"name": account[0].name,"email": account[0].email,"created_at": account[0].created_at.isoformat(),"role": account[1]} for account in results]return {'items': account_list,'total': total,'page': page,'limit': limit}

用戶的添加

controller層
class AccountCreateApi(Resource):"""Resource for creating a new account."""@validate_tokendef post(self):"""Create a new account."""parser = reqparse.RequestParser()parser.add_argument("name", type=str, required=True, location="json")parser.add_argument("email", type=str, required=True, location="json")parser.add_argument("password", type=str, required=True, location="json")parser.add_argument("tenant_id", type=str, required=True, location="json")args = parser.parse_args()account = current_usertenant = TenantService.get_tenant_by_id(args["tenant_id"])if not tenant:return {"result": "fail", "message": "Tenant not found"}, 404try:new_account = AccountService.create_account(email=args["email"],name=args["name"],interface_language="zh-Hans",password=args["password"])TenantService.create_tenant_member(tenant, new_account, role="owner")return {"result": "success","data": {"id": str(new_account.id),"name": new_account.name,"email": new_account.email,"created_at": new_account.created_at.isoformat()}}except Exception as e:return {"result": "error", "message": str(e)}, 400

傳入租戶id和用戶信息,我這里就直接默認語言是中文。

添加路由
api.add_resource(AccountCreateApi, "/accounts/create")
service層-添加用戶
@staticmethoddef create_account(email: str,name: str,interface_language: str,password: Optional[str] = None,interface_theme: str = "light",is_setup: Optional[bool] = False,) -> Account:"""create account"""# if not FeatureService.get_system_features().is_allow_register and not is_setup:#     from controllers.console.error import AccountNotFound##     raise AccountNotFound()if dify_config.BILLING_ENABLED and BillingService.is_email_in_freeze(email):raise AccountRegisterError(description=("This email account has been deleted within the past ""30 days and is temporarily unavailable for new account registration"))account = Account()account.email = emailaccount.name = nameif password:# generate password saltsalt = secrets.token_bytes(16)base64_salt = base64.b64encode(salt).decode()# encrypt password with saltpassword_hashed = hash_password(password, salt)base64_password_hashed = base64.b64encode(password_hashed).decode()account.password = base64_password_hashedaccount.password_salt = base64_saltaccount.interface_language = interface_languageaccount.interface_theme = interface_theme# Set timezone based on languageaccount.timezone = language_timezone_mapping.get(interface_language, "UTC")db.session.add(account)db.session.commit()return account
service層-添加用戶和租戶關系
@staticmethoddef create_tenant_member(tenant: Tenant, account: Account, role: str = "normal") -> TenantAccountJoin:"""Create tenant member"""if role == TenantAccountRole.OWNER.value:if TenantService.has_roles(tenant, [TenantAccountRole.OWNER]):logging.error(f"Tenant {tenant.id} has already an owner.")raise Exception("Tenant already has an owner.")ta = db.session.query(TenantAccountJoin).filter_by(tenant_id=tenant.id, account_id=account.id).first()if ta:ta.role = roleelse:ta = TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, role=role)db.session.add(ta)db.session.commit()return ta

這里直接調用已有方法

TenantAccountJoin
驗證結果

用戶表

用戶租戶關聯表

結果

用創建的用戶和密碼從前端登錄進去后,智能體、知識庫、插件、模型等都完全隔離了。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/75772.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/75772.shtml
英文地址,請注明出處:http://en.pswp.cn/web/75772.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于若依的ruoyi-vue-plus的nbmade-boot在線表單的設計(一)架構方面的設計

希望大家一起能參與我的新開源項目nbmade-boot: 寧波智能制造低代碼實訓平臺 主要目標是類似設計jeecgboot那樣的online表單功能,因為online本身沒有開源這部分代碼,而我設計這個是完全開源的,所以希望大家支持支持,開源不容易。 1、數據庫方面設計考慮 是在原來gen_table和…

WebFlux應用中獲取x-www-form-urlencoded數據的六種方法

🧑 博主簡介:CSDN博客專家,歷代文學網(PC端可以訪問:https://literature.sinhy.com/#/?__c1000,移動端可微信小程序搜索“歷代文學”)總架構師,15年工作經驗,精通Java編…

[Python基礎速成]1-Python規范與核心語法

本系列旨在快速掌握Python,實現能夠快速閱讀和理解 Python 代碼,并在可查閱語法的情況下進行 AI 學習。 本篇先了解一下Python基礎知識。 本篇內容較菜鳥教程有所刪減、方便快速構建大綱,且加入了PEP 8規范說明等有助于理解和編寫代碼的說明。…

農民劇團的春天與改變之路

楊天義,男,1966年9月生,中共黨員,江西省吉安市吉水縣水南農民劇團團長。 楊天義從廢品收購起家,憑借自身的努力和奮斗,自籌資金100余萬元建設了水南鎮的第一座影劇院,組建了江西省吉安市吉水縣…

python asyncio 的基本使用

1、引言 asyncio 是 Python 標準庫中的一個庫,提供了對異步 I/O 、事件循環、協程和任務等異步編程模型的支持。 asyncio 文檔 2、進程、線程、協程 線程 線程是操作系統調度的基本單位,同一個進程中的多個線程共享相同的內存空間。線程之間的切換由操…

Leedcode刷題 | Day30_貪心算法04

一、學習任務 452. 用最少數量的箭引爆氣球代碼隨想錄435. 無重疊區間763. 劃分字母區間 二、具體題目 1.452用最少數量的箭引爆氣球452. 用最少數量的箭引爆氣球 - 力扣(LeetCode) 在二維空間中有許多球形的氣球。對于每個氣球,提供的輸…

Ant Design Vue 表格復雜數據合并單元格

Ant Design Vue 表格復雜數據合并單元格 官方合并效果 官方示例 表頭只支持列合并&#xff0c;使用 column 里的 colSpan 進行設置。 表格支持行/列合并&#xff0c;使用 render 里的單元格屬性 colSpan 或者 rowSpan 設值為 0 時&#xff0c;設置的表格不會渲染。 <temp…

C++ 標準庫中的 <algorithm> 頭文件算法總結

C 常用 <algorithm> 算法概覽 C 標準庫中的 <algorithm> 頭文件提供了大量有用的算法&#xff0c;主要用于操作容器&#xff08;如 vector, list, array 等&#xff09;。這些算法通常通過迭代器來操作容器元素。 1. 非修改序列操作 std::all_of, std::any_of, s…

程序化廣告行業(84/89):4A廣告代理公司與行業資質解讀

程序化廣告行業&#xff08;84/89&#xff09;&#xff1a;4A廣告代理公司與行業資質解讀 大家好&#xff01;在探索程序化廣告行業的道路上&#xff0c;每一次知識的分享都是我們共同進步的階梯。一直以來&#xff0c;我都希望能和大家攜手前行&#xff0c;深入了解這個充滿機…

deepin使用autokey添加微信快捷鍵一鍵顯隱ctrl+alt+w

打開deepin商店&#xff0c;搜索快捷鍵&#xff0c;找到autokey 快捷鍵管理&#xff0c;點擊安裝 點擊右鍵新建文件夾 點擊右鍵新建腳本 打開腳本并添加以下內容 import subprocess import time# ------------------ 配置項 ------------------ WM_CLASS "wechat…

文件內容課堂總結

Spark SQL是Spark用于結構化數據處理的模塊&#xff0c;前身是Shark。Shark基于Hive開發&#xff0c;雖提升了SQL-on-Hadoop效率&#xff0c;但對Hive依賴過多。2014年6月1日Shark項目停止開發&#xff0c;團隊將資源投入Spark SQL項目。Spark SQL具有諸多優點&#xff0c;如擺…

Zotero PDF Translate 翻譯插件使用OpenAI API配置教程

PDF Translate&#xff1a;提升 Zotero 內置 PDF 閱讀器的翻譯功能 “PDF Translate” 是一款為 Zotero 設計的插件&#xff0c;旨在方便用戶在 Zotero 內置的 PDF 閱讀器中進行劃詞或段落翻譯&#xff0c;輔助閱讀外文文獻。 一、 安裝插件 下載插件&#xff1a; 訪問 PDF T…

火山引擎旗下的產品

用戶問的是火山引擎旗下的產品&#xff0c;我需要詳細列出各個類別下的產品。首先&#xff0c;我得確認火山引擎有哪些主要業務領域&#xff0c;比如云計算、大數據、人工智能這些。然后&#xff0c;每個領域下具體有哪些產品呢&#xff1f;比如云計算方面可能有云服務器、容器…

C/C++程序中實現Python綁定多種技術路線

在C/C程序中實現Python綁定有多種技術路線&#xff0c;選擇合適的方法取決于項目需求、性能要求和開發效率。以下是常見的幾種方案&#xff0c;按易用性排序&#xff1a; 1. PyBind11&#xff08;推薦首選&#xff09; 特點&#xff1a;現代C庫&#xff0c;語法簡潔&#xff0…

【位運算】消失的兩個數字

文章目錄 面試題 17.19. 消失的兩個數字解題思路 面試題 17.19. 消失的兩個數字 面試題 17.19. 消失的兩個數字 ? 給定一個數組&#xff0c;包含從 1 到 N 所有的整數&#xff0c;但其中缺了兩個數字。你能在 O(N) 時間內只用 O(1) 的空間找到它們嗎&#xff1f; ? 以任意…

自然語言處理Hugging Face Transformers

Hugging Face Transformers 是一個基于 PyTorch 和 TensorFlow 的開源庫&#xff0c;專注于 最先進的自然語言處理&#xff08;NLP&#xff09;模型&#xff0c;如 BERT、GPT、RoBERTa、T5 等。它提供了 預訓練模型、微調工具和推理 API&#xff0c;廣泛應用于文本分類、機器翻…

vue開發基礎流程 (后20)

創建項目命令&#xff1b; 或者 vue create my - vue - router - project這個是創建帶路由的項目 22.組件組成 比如說一個頁面吧&#xff0c;他三個組件&#xff0c;template就是用來放所有的標簽&#xff0c;script用來放業務邏輯&#xff0c;style用來放樣式&#xff0c;c…

高性能內存kv數據庫Redis

引言 在當今數據驅動的時代&#xff0c;高效的數據存儲和檢索對于各類應用程序至關重要。Redis&#xff08;Remote Dictionary Server&#xff09;作為一款開源的內存鍵值數據庫&#xff0c;憑借其出色的性能、豐富的數據結構和靈活的特性&#xff0c;在眾多場景中得到了廣泛應…

自動化測試概念篇

文章目錄 目錄1. 自動化1.1 自動化概念1.1.1 回歸測試 1.2 自動化分類1.3 自動化測試金字塔 2. web自動化測試2.1 驅動2.1.1 安裝驅動管理2.1.2 selenium庫 3. Selenium3.1 一個簡單的web自動化示例3.2 selenium驅動瀏覽器的工作原理 目錄 自動化web自動化測試Selenium 1. 自…

《AI大模型應知應會100篇》第17篇:大模型的偏見與公平性問題

第17篇&#xff1a;大模型的偏見與公平性問題 摘要 在人工智能迅速發展的今天&#xff0c;大型語言模型&#xff08;LLM&#xff09;已經深入到我們的日常生活和工作中。然而&#xff0c;這些模型并非完美無缺&#xff0c;它們可能攜帶并放大數據中的偏見&#xff0c;導致不公…