RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: allow declining pending tenant invitations (#20896)
buua4361c6536ab7574
diff --git a/internal/dao/user_tenant.go b/internal/dao/user_tenant.goindex bed281a123..f60ecaee1f 100644--- a/internal/dao/user_tenant.go+++ b/internal/dao/user_tenant.go@@ -188,6 +188,18 @@ func (dao *UserTenantDAO) DeleteByUserAndTenant(ctx context.Context, db *gorm.DBDelete(&entity.UserTenant{}).Error}+// DeleteByUserAndTenantAndRole deletes an active membership only when its role+// matches, and reports whether a row was removed.+func (dao *UserTenantDAO) DeleteByUserAndTenantAndRole(ctx context.Context, db *gorm.DB, userID, tenantID, role string) (bool, error) {+ result := db.WithContext(ctx).Unscoped().+ Where("user_id = ? AND tenant_id = ? AND role = ? AND status = ?", userID, tenantID, role, "1").+ Delete(&entity.UserTenant{})+ if result.Error != nil {+ return false, result.Error+ }+ return result.RowsAffected > 0, nil+}+// UpdateRoleByUserAndTenant updates the role for a specific user+tenant pair.// Returns an error if no matching row was found.func (dao *UserTenantDAO) UpdateRoleByUserAndTenant(ctx context.Context, db *gorm.DB, userID, tenantID, role string) error {diff --git a/internal/service/tenant.go b/internal/service/tenant.goindex 03c2081aef..f6ff646e86 100644--- a/internal/service/tenant.go+++ b/internal/service/tenant.go@@ -831,8 +831,22 @@ func (s *TenantService) AddMember(ctx context.Context, userID, tenantID string,}// RemoveMember removes a user from the tenant. The owner may remove members,-// and a member may remove themselves. The tenant owner cannot be removed.+// and a member may remove themselves. An invitee may decline their own pending+// invitation. The tenant owner cannot be removed.func (s *TenantService) RemoveMember(ctx context.Context, userID, tenantID, targetUserID string) (common.ErrorCode, error) {+ if s.userTenantDAO == nil {+ return common.CodeServerError, fmt.Errorf("userTenantDAO not initialized")+ }+ if userID != "" && userID == targetUserID {+ removed, err := s.userTenantDAO.DeleteByUserAndTenantAndRole(ctx, dao.DB, userID, tenantID, TenantRoleInvite)+ if err != nil {+ return common.CodeServerError, fmt.Errorf("failed to decline tenant invitation: %w", err)+ }+ if removed {+ return common.CodeSuccess, nil+ }+ }+requirement := permission.TenantOwnerif userID == targetUserID {requirement = permission.TenantMember@@ -841,9 +855,6 @@ func (s *TenantService) RemoveMember(ctx context.Context, userID, tenantID, targcode, permissionErr := permissionresponse.Normalize(err)return code, permissionErr}- if s.userTenantDAO == nil {- return common.CodeServerError, fmt.Errorf("userTenantDAO not initialized")- }targetMembership, err := s.userTenantDAO.FilterByUserIDAndTenantID(ctx, dao.DB, targetUserID, tenantID)if err != nil {if dao.IsNotFoundErr(err) {diff --git a/internal/service/tenant_test.go b/internal/service/tenant_test.goindex 0448936052..7ac2bbe41d 100644--- a/internal/service/tenant_test.go+++ b/internal/service/tenant_test.go@@ -137,6 +137,20 @@ func TestRemoveMemberSelfAllowed(t *testing.T) {}}+func TestDeclineOwnTenantInviteAllowed(t *testing.T) {+ db := setupTenantPermissionTestDB(t)+ seedTenantMembership(t, db, "invite", "invitee-user", "tenant-xyz", TenantRoleInvite)+ s := &TenantService{userTenantDAO: dao.NewUserTenantDAO()}++ code, err := s.RemoveMember(t.Context(), "invitee-user", "tenant-xyz", "invitee-user")+ if err != nil || code != common.CodeSuccess {+ t.Fatalf("RemoveMember(invitation self-decline) = (%v, %v), want success", code, err)+ }+ if _, err := dao.NewUserTenantDAO().FilterByUserIDAndTenantID(t.Context(), db, "invitee-user", "tenant-xyz"); !dao.IsNotFoundErr(err) {+ t.Fatalf("pending invitation remains after decline: %v", err)+ }+}+func TestRemoveMemberCannotRemoveTenantOwner(t *testing.T) {db := setupTenantPermissionTestDB(t)seedTenantMembership(t, db, "owner", "owner-user", "tenant-xyz", "owner")