Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

19.5 KB
Raw Blame
//
//  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 handler

import (
	"crypto/tls"
	"errors"
	"fmt"
	"net/http"
	"net/mail"
	"net/smtp"

	"ragflow/internal/permission"
	"ragflow/internal/server"
	"strings"
	"time"

	"github.com/gin-gonic/gin"

	"ragflow/internal/common"
	"ragflow/internal/service"
	dataset "ragflow/internal/service/dataset"

	"go.uber.org/zap"
	"ragflow/internal/dao"
)

// TenantHandler tenant handler
type TenantHandler struct {
	tenantService     *service.TenantService
	userTenantService *service.UserTenantService
	datasetService    *dataset.DatasetService
}

// NewTenantHandler create tenant handler
func NewTenantHandler(tenantService *service.TenantService, userTenantService *service.UserTenantService, datasetService *dataset.DatasetService) *TenantHandler {
	return &TenantHandler{
		tenantService:     tenantService,
		userTenantService: userTenantService,
		datasetService:    datasetService,
	}
}

func (h *TenantHandler) SetModels(c *gin.Context) {
	h.setDefaultModels(c, false)
}

func (h *TenantHandler) SetDefaultModels(c *gin.Context) {
	h.setDefaultModels(c, true)
}

type SetModelRequest struct {
	ModelProvider string `json:"model_provider"`
	ModelInstance string `json:"model_instance"`
	ModelName     string `json:"model_name"`
	ModelID       string `json:"model_id"`
	ModelType     string `json:"model_type" binding:"required"`
}

func (h *TenantHandler) setDefaultModels(c *gin.Context, wrapModels bool) {
	// Parse request body (same as Python get_request_json())
	var req SetModelRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Respond(c, http.StatusBadRequest, common.CodeBadRequest, nil, "Invalid request body: "+err.Error())
		return
	}

	ctx := c.Request.Context()
	code, err := h.userTenantService.SetModelSettings(ctx, &service.SetModelSettingsRequest{
		DefaultModel: &service.DefaultModelSelection{
			ModelProvider: req.ModelProvider,
			ModelInstance: req.ModelInstance,
			ModelName:     req.ModelName,
			ModelType:     req.ModelType,
			ModelID:       req.ModelID,
		},
	})
	if err != nil {
		common.RespondError(c, code, err.Error())
		return
	}

	if wrapModels {
		common.RespondSuccess(c, map[string]interface{}{"models": []service.ModelItem{}}, "success")
		return
	}

	common.RespondSuccess(c, nil, "success")
}

// GetDefaultModels returns the user's model selections for the current tenant. The
// response wraps the model list under `data.models` to mirror the
// Python `list_tenant_default_models` contract (api/apps/restful_apis/
// models_api.py:84). The frontend hook `useFetchDefaultModels`
// (web/src/hooks/use-llm-request.tsx:423) reads `data.data.models`.
func (h *TenantHandler) GetDefaultModels(c *gin.Context) {
	ctx := c.Request.Context()

	defaultModels, err := h.userTenantService.ListDefaultModels(ctx)
	if err != nil {
		common.RespondError(c, common.CodeExceptionError, err.Error())
		return
	}

	// Empty selection is a normal state for a freshly created tenant, not a
	// data error. Match Python's `list_tenant_default_models` (which returns
	// get_result(data=[])) and the frontend's expectation that `data.data.models`
	// is always an array.
	if defaultModels == nil {
		defaultModels = []service.ModelItem{}
	}
	common.RespondSuccess(c, map[string]interface{}{"models": defaultModels}, "success")
}

// TenantInfo get tenant information
// @Summary Get Tenant Information
// @Description Get current user's tenant information (owner tenant)
// @Tags tenants
// @Security ApiKeyAuth
// @Success 200 {object} map[string]interface{}
// @Router /v1/user/tenant_info [get]
func (h *TenantHandler) TenantInfo(c *gin.Context) {
	ctx := c.Request.Context()

	tenantInfo, err := h.userTenantService.GetCurrentTenantInfo(ctx)
	if err != nil {
		common.RespondError(c, common.CodeExceptionError, err.Error())
		return
	}

	if tenantInfo == nil {
		common.RespondError(c, common.CodeDataError, "Tenant not found!")
		return
	}

	common.RespondSuccess(c, tenantInfo, "success")
}

// TenantList get tenant list for current user
// @Summary Get Tenant List
// @Description Get all tenants that the current user belongs to
// @Tags tenants
// @Security ApiKeyAuth
// @Success 200 {object} map[string]interface{}
// @Router /api/v1/tenants [get]
func (h *TenantHandler) TenantList(c *gin.Context) {
	ctx := c.Request.Context()

	tenantList, err := h.tenantService.GetTenantList(ctx)
	if err != nil {
		common.RespondError(c, common.CodeExceptionError, err.Error())
		return
	}

	common.RespondSuccessList(c, tenantList, "success")
}

// CreateMetadataStore handles the create metadata store request
// @Summary Create Metadata Store
// @Description Create the metadata store for a tenant
// @Tags tenants
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} map[string]interface{}
// @Router /v1/tenant/metadata-store [post]
func (h *TenantHandler) CreateMetadataStore(c *gin.Context) {
	user, errorCode, errorMessage := GetUser(c)
	if errorCode != common.CodeSuccess {
		common.RespondError(c, errorCode, errorMessage)
		return
	}

	// Use user.ID as tenant ID (user IS the tenant in user mode)
	tenantID := user.ID
	ctx := c.Request.Context()
	code, err := h.tenantService.CreateMetadataStore(ctx, tenantID)
	if err != nil {
		common.RespondError(c, code, err.Error())
		return
	}

	common.RespondSuccess(c, nil, "success")
}

// DeleteMetadataStore handles the delete metadata store request
// @Summary Delete Metadata Store
// @Description Delete the metadata store for a tenant
// @Tags tenants
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} map[string]interface{}
// @Router /v1/tenant/metadata-store [delete]
func (h *TenantHandler) DeleteMetadataStore(c *gin.Context) {
	user, errorCode, errorMessage := GetUser(c)
	if errorCode != common.CodeSuccess {
		common.RespondError(c, errorCode, errorMessage)
		return
	}

	// Use user.ID as tenant ID (user IS the tenant in user mode)
	tenantID := user.ID

	ctx := c.Request.Context()
	code, err := h.tenantService.DeleteMetadataStore(ctx, tenantID)
	if err != nil {
		common.RespondError(c, code, err.Error())
		return
	}

	common.RespondSuccess(c, nil, "success")
}

// CreateChunkTableRequest represents the request for creating a chunk table
type CreateChunkTableRequest struct {
	KBID       string `json:"kb_id" binding:"required"`
	VectorSize int    `json:"vector_size" binding:"required"`
}

// CreateChunkStore handles the create chunk store request
// @Summary Create Chunk Store
// @Description Create the chunk store for a knowledge base
// @Tags tenants
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param request body CreateChunkTableRequest true "create chunk store request"
// @Success 200 {object} map[string]interface{}
// @Router /v1/tenant/chunk-store [post]
func (h *TenantHandler) CreateChunkStore(c *gin.Context) {

	var req CreateChunkTableRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.RespondError(c, common.CodeDataError, err.Error())
		return
	}

	ctx := c.Request.Context()
	// Check authorization - user must have access to this kb
	if err := permission.NewDatabaseChecker(dao.DB).CheckResource(ctx, permission.ResourceRef{Kind: permission.ResourceKindDataset, ID: req.KBID}, permission.OperationUpdate); err != nil {
		respondPermissionError(c, err, false)
		return
	}

	serviceReq := &service.CreateDatasetTableRequest{
		KBID:       req.KBID,
		VectorSize: req.VectorSize,
	}
	result, code, err := h.tenantService.CreateChunkStore(ctx, serviceReq)
	if err != nil {
		common.RespondError(c, code, err.Error())
		return
	}

	common.RespondSuccess(c, result, "success")
}

// DeleteChunkTableRequest represents the request for deleting a chunk table
type DeleteChunkTableRequest struct {
	KBID string `json:"kb_id" binding:"required"`
}

// DeleteChunkStore handles the delete chunk store request
// @Summary Delete Chunk Store
// @Description Delete the chunk store for a knowledge base
// @Tags tenants
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param request body DeleteChunkTableRequest true "delete chunk store request"
// @Success 200 {object} map[string]interface{}
// @Router /v1/tenant/chunk-store [delete]
func (h *TenantHandler) DeleteChunkStore(c *gin.Context) {

	ctx := c.Request.Context()
	var req DeleteChunkTableRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.RespondError(c, common.CodeDataError, err.Error())
		return
	}

	// Check authorization
	if err := permission.NewDatabaseChecker(dao.DB).CheckResource(ctx, permission.ResourceRef{Kind: permission.ResourceKindDataset, ID: req.KBID}, permission.OperationUpdate); err != nil {
		respondPermissionError(c, err, false)
		return
	}

	code, err := h.tenantService.DeleteChunkStore(ctx, req.KBID)
	if err != nil {
		common.RespondError(c, code, err.Error())
		return
	}

	common.RespondSuccess(c, nil, "success")
}

// ListTenantMembers lists all non-owner members of a tenant.
// @Summary List tenant members
// @Tags tenants
// @Param tenant_id path string true "Tenant ID"
// @Router /api/v1/tenants/{tenant_id}/users [get]
func (h *TenantHandler) ListTenantMembers(c *gin.Context) {
	tenantID := c.Param("tenant_id")
	if tenantID == "" {
		common.Respond(c, http.StatusBadRequest, common.CodeBadRequest, nil, "tenant_id is required")
		return
	}
	if !matchesActiveTenant(c, tenantID) {
		common.RespondError(c, common.CodeForbidden, "Tenant does not match the active token context")
		return
	}

	ctx := c.Request.Context()
	members, code, err := h.tenantService.ListMembers(ctx, tenantID)
	if err != nil {
		common.RespondError(c, code, err.Error())
		return
	}
	common.RespondSuccessList(c, members, "success")
}

// tenantSMTPConfig returns the SMTP settings together with a flag telling
// whether they are usable. SMTP is optional: when it is not configured the
// caller must skip sending instead of failing the request.
func tenantSMTPConfig() (common.SMTPConfig, bool) {
	config := server.GetConfig()
	if config == nil {
		return common.SMTPConfig{}, false
	}

	smtpCfg := config.GetSMTPConfig()
	if smtpCfg.MailServer == "" || smtpCfg.MailPort == 0 || smtpCfg.MailFromAddress == "" {
		return common.SMTPConfig{}, false
	}
	return smtpCfg, true
}

func sendTenantInviteEmail(smtpCfg common.SMTPConfig, toEmail, recipientEmail, tenantID, inviter string) error {
	from := mail.Address{
		Name:    smtpCfg.MailFromName,
		Address: smtpCfg.MailFromAddress,
	}
	to := mail.Address{Address: toEmail}
	subject := "RAGFlow Invitation"
	body := fmt.Sprintf(
		"Hi %s,\n%s has invited you to join their team (ID: %s).\nClick the link below to complete your registration:\n%s\nIf you did not request this, please ignore this email.\n",
		recipientEmail,
		inviter,
		tenantID,
		smtpCfg.MailFrontendURL,
	)
	message := strings.Join([]string{
		fmt.Sprintf("From: %s", from.String()),
		fmt.Sprintf("To: %s", to.String()),
		fmt.Sprintf("Subject: %s", subject),
		"MIME-Version: 1.0",
		"Content-Type: text/plain; charset=UTF-8",
		"",
		body,
	}, "\r\n")

	address := fmt.Sprintf("%s:%d", smtpCfg.MailServer, smtpCfg.MailPort)
	var auth smtp.Auth
	if smtpCfg.MailUsername != "" || smtpCfg.MailPassword != "" {
		auth = smtp.PlainAuth("", smtpCfg.MailUsername, smtpCfg.MailPassword, smtpCfg.MailServer)
	}

	if smtpCfg.MailUseSSL {
		return sendMailWithTLS(address, smtpCfg.MailServer, auth, from.Address, []string{to.Address}, []byte(message))
	}

	client, err := smtp.Dial(address)
	if err != nil {
		return err
	}
	defer client.Close()

	if smtpCfg.MailUseTLS {
		tlsConfig := &tls.Config{ServerName: smtpCfg.MailServer}
		if err = client.StartTLS(tlsConfig); err != nil {
			return err
		}
	}
	if auth != nil {
		if err = client.Auth(auth); err != nil {
			return err
		}
	}
	if err = client.Mail(from.Address); err != nil {
		return err
	}
	if err = client.Rcpt(to.Address); err != nil {
		return err
	}
	writer, err := client.Data()
	if err != nil {
		return err
	}
	if _, err = writer.Write([]byte(message)); err != nil {
		_ = writer.Close()
		return err
	}
	if err = writer.Close(); err != nil {
		return err
	}
	return client.Quit()
}

func sendMailWithTLS(addr, host string, auth smtp.Auth, from string, to []string, msg []byte) error {
	conn, err := tls.Dial("tcp", addr, &tls.Config{ServerName: host})
	if err != nil {
		return err
	}
	defer conn.Close()

	client, err := smtp.NewClient(conn, host)
	if err != nil {
		return err
	}
	defer client.Close()

	if auth != nil {
		if err = client.Auth(auth); err != nil {
			return err
		}
	}
	if err = client.Mail(from); err != nil {
		return err
	}
	for _, recipient := range to {
		if err = client.Rcpt(recipient); err != nil {
			return err
		}
	}
	writer, err := client.Data()
	if err != nil {
		return err
	}
	if _, err = writer.Write(msg); err != nil {
		_ = writer.Close()
		return err
	}
	if err = writer.Close(); err != nil {
		return err
	}
	return client.Quit()
}

// AddTenantMember invites a user (by email) to the tenant.
// @Summary Invite a user to a tenant
// @Tags tenants
// @Accept json
// @Produce json
// @Param tenant_id path string true "Tenant ID"
// @Param request body service.AddMemberRequest true "Invite request"
// @Router /api/v1/tenants/{tenant_id}/users [post]
func (h *TenantHandler) AddTenantMember(c *gin.Context) {
	startAt := time.Now()
	operationLog := &common.OperationLog{
		EventTime:  startAt,
		Operation:  "set_tenant_info",
		APIPath:    c.FullPath(),
		HTTPMethod: c.Request.Method,
		IPAddress:  c.ClientIP(),
	}
	defer func() {
		operationLog.DurationMS = time.Since(startAt).Milliseconds()
		common.Audit(operationLog)
	}()

	user, errorCode, errorMessage := GetUser(c)
	if errorCode != common.CodeSuccess {
		common.RespondError(c, errorCode, errorMessage)
		operationLog.Code = int(errorCode)
		operationLog.Error = errors.New(errorMessage)
		return
	}
	operationLog.UserID = user.ID

	tenantID := c.Param("tenant_id")
	if tenantID == "" {
		common.Respond(c, http.StatusBadRequest, common.CodeBadRequest, nil, "tenant_id is required")
		operationLog.Code = int(common.CodeBadRequest)
		operationLog.Error = errors.New("tenant_id is required")
		return
	}
	if !matchesActiveTenant(c, tenantID) {
		common.RespondError(c, common.CodeForbidden, "Tenant does not match the active token context")
		operationLog.Code = int(common.CodeForbidden)
		operationLog.Error = errors.New("tenant does not match the active token context")
		return
	}

	var req service.AddMemberRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Respond(c, http.StatusBadRequest, common.CodeBadRequest, nil, "invalid request body: "+err.Error())
		operationLog.Code = int(common.CodeBadRequest)
		operationLog.Error = errors.New("invalid request body: " + err.Error())
		return
	}

	ctx := c.Request.Context()
	resp, code, err := h.tenantService.AddMember(ctx, tenantID, &req)
	if err != nil {
		common.RespondError(c, code, err.Error())
		operationLog.Code = int(code)
		operationLog.Error = err
		return
	}

	inviter := user.Nickname
	if inviter == "" {
		inviter = user.Email
	}
	// The member is already invited at this point; a missing or broken SMTP
	// setup must not turn the whole invitation into a failure.
	if smtpCfg, ok := tenantSMTPConfig(); ok {
		if err = sendTenantInviteEmail(smtpCfg, req.Email, req.Email, tenantID, inviter); err != nil {
			common.Warn("failed to send tenant invite email",
				zap.String("tenant_id", tenantID),
				zap.String("email", req.Email),
				zap.Error(err))
			operationLog.Code = int(common.CodeServerError)
			operationLog.Error = err
		}
	}

	common.RespondSuccess(c, resp, "success")
}

// RemoveTenantMember removes a user from the tenant.
// @Summary Remove a user from a tenant
// @Tags tenants
// @Param tenant_id path string true "Tenant ID"
// @Param request body object true "Remove member request" SchemaExample({"user_id":"string"})
// @Router /api/v1/tenants/{tenant_id}/users [delete]
func (h *TenantHandler) RemoveTenantMember(c *gin.Context) {
	startAt := time.Now()
	operationLog := &common.OperationLog{
		EventTime:  startAt,
		Operation:  "set_tenant_info",
		APIPath:    c.FullPath(),
		HTTPMethod: c.Request.Method,
		IPAddress:  c.ClientIP(),
	}
	defer func() {
		operationLog.DurationMS = time.Since(startAt).Milliseconds()
		common.Audit(operationLog)
	}()
	user, errorCode, errorMessage := GetUser(c)
	if errorCode != common.CodeSuccess {
		common.RespondError(c, errorCode, errorMessage)
		operationLog.Code = int(errorCode)
		operationLog.Error = errors.New(errorMessage)
		return
	}
	operationLog.UserID = user.ID

	tenantID := c.Param("tenant_id")
	if tenantID == "" {
		common.Respond(c, http.StatusBadRequest, common.CodeBadRequest, nil, "tenant_id is required")
		operationLog.Code = int(common.CodeBadRequest)
		operationLog.Error = errors.New("tenant_id is required")
		return
	}
	var body struct {
		UserID string `json:"user_id"`
	}
	if err := c.ShouldBindJSON(&body); err != nil || body.UserID == "" {
		common.Respond(c, http.StatusBadRequest, common.CodeBadRequest, nil, "user_id is required")
		operationLog.Code = int(common.CodeBadRequest)
		operationLog.Error = errors.New("user_id is required")
		return
	}
	if body.UserID != user.ID && !matchesActiveTenant(c, tenantID) {
		common.RespondError(c, common.CodeForbidden, "Tenant does not match the active token context")
		operationLog.Code = int(common.CodeForbidden)
		operationLog.Error = errors.New("tenant does not match the active token context")
		return
	}

	ctx := c.Request.Context()
	code, err := h.tenantService.RemoveMember(ctx, tenantID, body.UserID)
	if err != nil {
		common.RespondError(c, code, err.Error())
		operationLog.Code = int(code)
		operationLog.Error = err
		return
	}
	common.RespondSuccess(c, nil, "success")
}

// AcceptTenantInvite accepts a pending invitation, transitioning membership status to joined.
// @Summary Accept tenant invitation
// @Tags tenants
// @Produce json
// @Param tenant_id path string true "Tenant ID"
// @Router /api/v1/tenants/{tenant_id} [patch]
func (h *TenantHandler) AcceptTenantInvite(c *gin.Context) {
	startAt := time.Now()
	operationLog := &common.OperationLog{
		EventTime:  startAt,
		Operation:  "set_tenant_info",
		APIPath:    c.FullPath(),
		HTTPMethod: c.Request.Method,
		IPAddress:  c.ClientIP(),
	}
	defer func() {
		operationLog.DurationMS = time.Since(startAt).Milliseconds()
		common.Audit(operationLog)
	}()

	user, errorCode, errorMessage := GetUser(c)
	if errorCode != common.CodeSuccess {
		common.RespondError(c, errorCode, errorMessage)
		operationLog.Code = int(errorCode)
		operationLog.Error = errors.New(errorMessage)
		return
	}
	operationLog.UserID = user.ID

	tenantID := c.Param("tenant_id")
	if tenantID == "" {
		common.Respond(c, http.StatusBadRequest, common.CodeBadRequest, nil, "tenant_id is required")
		operationLog.Code = int(common.CodeArgumentError)
		operationLog.Error = errors.New("tenant_id is required")
		return
	}
	ctx := c.Request.Context()
	code, err := h.tenantService.AcceptInvite(ctx, tenantID)
	if err != nil {
		common.RespondError(c, code, err.Error())
		operationLog.Code = int(code)
		operationLog.Error = err
		return
	}
	common.RespondSuccess(c, nil, "success")
}

func matchesActiveTenant(c *gin.Context, tenantID string) bool {
	activeTenantID := strings.TrimSpace(c.GetString("tenant_id"))
	return activeTenantID == "" || activeTenantID == tenantID
}