Files
ragflow/internal/dao/tenant.go
chanx a3e6c2e84a Fix: Enhanced user management functionality and cascading data deletion. (#13594)
### What problem does this PR solve?
Fix: Enhanced user management functionality and cascading data deletion.

Added tenant and related data initialization functionality during user
creation, including tenants, user-tenant relationships, LLM
configuration, and root folder.
Added cascading deletion logic for user deletion, ensuring that all
associated data is cleaned up simultaneously when a user is deleted.
Implemented a Werkzeug-compatible password hash algorithm (scrypt) and
verification functionality.
Added multiple DAO methods to support batch data operations and
cascading deletion.
Improved user login processing and added token signing functionality.
### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
2026-03-13 16:53:54 +08:00

111 lines
4.1 KiB
Go

//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package dao
import (
"ragflow/internal/model"
)
// TenantDAO tenant data access object
type TenantDAO struct{}
// NewTenantDAO create tenant DAO
func NewTenantDAO() *TenantDAO {
return &TenantDAO{}
}
// GetJoinedTenantsByUserID get joined tenants by user ID
func (dao *TenantDAO) GetJoinedTenantsByUserID(userID string) ([]*TenantWithRole, error) {
var results []*TenantWithRole
err := DB.Model(&model.Tenant{}).
Select("tenant.id as tenant_id, tenant.name, tenant.llm_id, tenant.embd_id, tenant.asr_id, tenant.img2txt_id, user_tenant.role").
Joins("INNER JOIN user_tenant ON user_tenant.tenant_id = tenant.id").
Where("user_tenant.user_id = ? AND user_tenant.status = ? AND user_tenant.role = ? AND tenant.status = ?", userID, "1", "normal", "1").
Scan(&results).Error
return results, err
}
// TenantWithRole tenant with role information
type TenantWithRole struct {
TenantID string `gorm:"column:tenant_id" json:"tenant_id"`
Name string `gorm:"column:name" json:"name"`
LLMID string `gorm:"column:llm_id" json:"llm_id"`
EmbDID string `gorm:"column:embd_id" json:"embd_id"`
ASRID string `gorm:"column:asr_id" json:"asr_id"`
Img2TxtID string `gorm:"column:img2txt_id" json:"img2txt_id"`
Role string `gorm:"column:role" json:"role"`
}
// TenantInfo tenant information with role (for owner tenant)
type TenantInfo struct {
TenantID string `gorm:"column:tenant_id" json:"tenant_id"`
Name *string `gorm:"column:name" json:"name,omitempty"`
LLMID string `gorm:"column:llm_id" json:"llm_id"`
EmbDID string `gorm:"column:embd_id" json:"embd_id"`
RerankID string `gorm:"column:rerank_id" json:"rerank_id"`
ASRID string `gorm:"column:asr_id" json:"asr_id"`
Img2TxtID string `gorm:"column:img2txt_id" json:"img2txt_id"`
TTSID *string `gorm:"column:tts_id" json:"tts_id,omitempty"`
ParserIDs string `gorm:"column:parser_ids" json:"parser_ids"`
Role string `gorm:"column:role" json:"role"`
}
// GetInfoByUserID get tenant information for the owner tenant of a user
func (dao *TenantDAO) GetInfoByUserID(userID string) ([]*TenantInfo, error) {
var results []*TenantInfo
err := DB.Model(&model.Tenant{}).
Select("tenant.id as tenant_id, tenant.name, tenant.llm_id, tenant.embd_id, tenant.rerank_id, tenant.asr_id, tenant.img2txt_id, tenant.tts_id, tenant.parser_ids, user_tenant.role").
Joins("INNER JOIN user_tenant ON user_tenant.tenant_id = tenant.id").
Where("user_tenant.user_id = ? AND user_tenant.status = ? AND user_tenant.role = ? AND tenant.status = ?", userID, "1", "owner", "1").
Scan(&results).Error
return results, err
}
// GetByID gets tenant by ID
func (dao *TenantDAO) GetByID(id string) (*model.Tenant, error) {
var tenant model.Tenant
err := DB.Where("id = ? AND status = ?", id, "1").First(&tenant).Error
if err != nil {
return nil, err
}
return &tenant, nil
}
// Create creates a new tenant
func (dao *TenantDAO) Create(tenant *model.Tenant) error {
return DB.Create(tenant).Error
}
// Delete deletes a tenant by ID (soft delete)
func (dao *TenantDAO) Delete(id string) error {
return DB.Model(&model.Tenant{}).Where("id = ?", id).Update("status", "0").Error
}
// Update updates a tenant by ID
func (dao *TenantDAO) Update(id string, updates map[string]interface{}) error {
return DB.Model(&model.Tenant{}).Where("id = ?", id).Updates(updates).Error
}
// HardDelete hard deletes a tenant by ID
func (dao *TenantDAO) HardDelete(id string) error {
return DB.Unscoped().Where("id = ?", id).Delete(&model.Tenant{}).Error
}