feat: add organization management features
- Introduced a new API for organization management, including creation, updating, and deletion of organizations. - Implemented organization sharing functionality for knowledge bases, allowing users to share knowledge bases with specific organizations and set permissions. - Added UI components for organization selection and sharing dialogs. - Enhanced existing components to support organization-related features, including shared knowledge base handling and organization role management. This update enhances collaboration capabilities within the application by allowing users to manage and share knowledge bases across different organizations.
This commit is contained in:
@@ -14,6 +14,8 @@ const (
|
||||
LoggerContextKey ContextKey = "Logger"
|
||||
// UserContextKey is the context key for user information
|
||||
UserContextKey ContextKey = "User"
|
||||
// UserIDContextKey is the context key for user ID
|
||||
UserIDContextKey ContextKey = "UserID"
|
||||
)
|
||||
|
||||
// String returns the string representation of the context key
|
||||
|
||||
@@ -12,10 +12,14 @@ type ChunkRepository interface {
|
||||
CreateChunks(ctx context.Context, chunks []*types.Chunk) error
|
||||
// GetChunkByID gets a chunk by id
|
||||
GetChunkByID(ctx context.Context, tenantID uint64, id string) (*types.Chunk, error)
|
||||
// GetChunkByIDOnly gets a chunk by id without tenant filter (for permission resolution)
|
||||
GetChunkByIDOnly(ctx context.Context, id string) (*types.Chunk, error)
|
||||
// GetChunkBySeqID gets a chunk by seq_id
|
||||
GetChunkBySeqID(ctx context.Context, tenantID uint64, seqID int64) (*types.Chunk, error)
|
||||
// ListChunksByID lists chunks by ids
|
||||
ListChunksByID(ctx context.Context, tenantID uint64, ids []string) ([]*types.Chunk, error)
|
||||
// ListChunksByIDOnly lists chunks by ids without tenant filter (for shared KB resolution).
|
||||
ListChunksByIDOnly(ctx context.Context, ids []string) ([]*types.Chunk, error)
|
||||
// ListChunksBySeqID lists chunks by seq_ids
|
||||
ListChunksBySeqID(ctx context.Context, tenantID uint64, seqIDs []int64) ([]*types.Chunk, error)
|
||||
// ListChunksByKnowledgeID lists chunks by knowledge id
|
||||
@@ -84,8 +88,10 @@ type ChunkRepository interface {
|
||||
type ChunkService interface {
|
||||
// CreateChunks creates chunks
|
||||
CreateChunks(ctx context.Context, chunks []*types.Chunk) error
|
||||
// GetChunkByID gets a chunk by id
|
||||
// GetChunkByID gets a chunk by id (uses tenant from context)
|
||||
GetChunkByID(ctx context.Context, id string) (*types.Chunk, error)
|
||||
// GetChunkByIDOnly gets a chunk by id without tenant filter (for permission resolution)
|
||||
GetChunkByIDOnly(ctx context.Context, id string) (*types.Chunk, error)
|
||||
// ListChunksByKnowledgeID lists chunks by knowledge id
|
||||
ListChunksByKnowledgeID(ctx context.Context, knowledgeID string) ([]*types.Chunk, error)
|
||||
// ListPagedChunksByKnowledgeID lists paged chunks by knowledge id
|
||||
|
||||
@@ -42,10 +42,14 @@ type KnowledgeService interface {
|
||||
kbID string,
|
||||
payload *types.ManualKnowledgePayload,
|
||||
) (*types.Knowledge, error)
|
||||
// GetKnowledgeByID retrieves knowledge by ID.
|
||||
// GetKnowledgeByID retrieves knowledge by ID (uses tenant from context).
|
||||
GetKnowledgeByID(ctx context.Context, id string) (*types.Knowledge, error)
|
||||
// GetKnowledgeByIDOnly retrieves knowledge by ID without tenant filter (for permission resolution).
|
||||
GetKnowledgeByIDOnly(ctx context.Context, id string) (*types.Knowledge, error)
|
||||
// GetKnowledgeBatch retrieves a batch of knowledge by IDs.
|
||||
GetKnowledgeBatch(ctx context.Context, tenantID uint64, ids []string) ([]*types.Knowledge, error)
|
||||
// GetKnowledgeBatchWithSharedAccess retrieves knowledge by IDs including items from shared KBs the user has access to.
|
||||
GetKnowledgeBatchWithSharedAccess(ctx context.Context, tenantID uint64, ids []string) ([]*types.Knowledge, error)
|
||||
// ListKnowledgeByKnowledgeBaseID lists all knowledge under a knowledge base.
|
||||
ListKnowledgeByKnowledgeBaseID(ctx context.Context, kbID string) ([]*types.Knowledge, error)
|
||||
// ListPagedKnowledgeByKnowledgeBaseID lists all knowledge under a knowledge base with pagination.
|
||||
@@ -148,6 +152,8 @@ type KnowledgeService interface {
|
||||
type KnowledgeRepository interface {
|
||||
CreateKnowledge(ctx context.Context, knowledge *types.Knowledge) error
|
||||
GetKnowledgeByID(ctx context.Context, tenantID uint64, id string) (*types.Knowledge, error)
|
||||
// GetKnowledgeByIDOnly returns knowledge by ID without tenant filter (for permission resolution).
|
||||
GetKnowledgeByIDOnly(ctx context.Context, id string) (*types.Knowledge, error)
|
||||
ListKnowledgeByKnowledgeBaseID(ctx context.Context, tenantID uint64, kbID string) ([]*types.Knowledge, error)
|
||||
// ListPagedKnowledgeByKnowledgeBaseID lists all knowledge in a knowledge base with pagination.
|
||||
// When tagID is non-empty, results are filtered by tag_id.
|
||||
@@ -182,6 +188,8 @@ type KnowledgeRepository interface {
|
||||
// SearchKnowledge searches knowledge items by keyword across the tenant.
|
||||
// fileTypes: optional list of file extensions to filter by (e.g., ["csv", "xlsx"])
|
||||
SearchKnowledge(ctx context.Context, tenantID uint64, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
|
||||
// SearchKnowledgeInScopes searches knowledge items by keyword within the given (tenant_id, kb_id) scopes (own + shared).
|
||||
SearchKnowledgeInScopes(ctx context.Context, scopes []types.KnowledgeSearchScope, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
|
||||
// ListIDsByTagID returns all knowledge IDs that have the specified tag ID.
|
||||
ListIDsByTagID(ctx context.Context, tenantID uint64, kbID, tagID string) ([]string, error)
|
||||
}
|
||||
|
||||
@@ -32,6 +32,16 @@ type KnowledgeBaseService interface {
|
||||
// - Possible errors such as not existing, insufficient permissions, etc.
|
||||
GetKnowledgeBaseByID(ctx context.Context, id string) (*types.KnowledgeBase, error)
|
||||
|
||||
// GetKnowledgeBaseByIDOnly retrieves knowledge base by ID without tenant filter
|
||||
// Used for cross-tenant shared KB access where permission is checked elsewhere
|
||||
// Parameters:
|
||||
// - ctx: Context information
|
||||
// - id: Unique identifier of the knowledge base
|
||||
// Returns:
|
||||
// - Knowledge base object, if found
|
||||
// - Possible errors such as not existing, etc.
|
||||
GetKnowledgeBaseByIDOnly(ctx context.Context, id string) (*types.KnowledgeBase, error)
|
||||
|
||||
// ListKnowledgeBases lists all knowledge bases under the current tenant
|
||||
// Parameters:
|
||||
// - ctx: Context information, containing tenant information
|
||||
|
||||
@@ -23,6 +23,8 @@ type ModelService interface {
|
||||
DeleteModel(ctx context.Context, id string) error
|
||||
// GetEmbeddingModel gets an embedding model
|
||||
GetEmbeddingModel(ctx context.Context, modelId string) (embedding.Embedder, error)
|
||||
// GetEmbeddingModelForTenant gets an embedding model for a specific tenant (for cross-tenant sharing)
|
||||
GetEmbeddingModelForTenant(ctx context.Context, modelId string, tenantID uint64) (embedding.Embedder, error)
|
||||
// GetRerankModel gets a rerank model
|
||||
GetRerankModel(ctx context.Context, modelId string) (rerank.Reranker, error)
|
||||
// GetChatModel gets a chat model
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// OrganizationService defines the organization service interface
|
||||
type OrganizationService interface {
|
||||
// Organization CRUD
|
||||
CreateOrganization(ctx context.Context, userID string, tenantID uint64, req *types.CreateOrganizationRequest) (*types.Organization, error)
|
||||
GetOrganization(ctx context.Context, id string) (*types.Organization, error)
|
||||
GetOrganizationByInviteCode(ctx context.Context, inviteCode string) (*types.Organization, error)
|
||||
ListUserOrganizations(ctx context.Context, userID string) ([]*types.Organization, error)
|
||||
UpdateOrganization(ctx context.Context, id string, userID string, req *types.UpdateOrganizationRequest) (*types.Organization, error)
|
||||
DeleteOrganization(ctx context.Context, id string, userID string) error
|
||||
|
||||
// Member Management
|
||||
AddMember(ctx context.Context, orgID string, userID string, tenantID uint64, role types.OrgMemberRole) error
|
||||
RemoveMember(ctx context.Context, orgID string, memberUserID string, operatorUserID string) error
|
||||
UpdateMemberRole(ctx context.Context, orgID string, memberUserID string, role types.OrgMemberRole, operatorUserID string) error
|
||||
ListMembers(ctx context.Context, orgID string) ([]*types.OrganizationMember, error)
|
||||
GetMember(ctx context.Context, orgID string, userID string) (*types.OrganizationMember, error)
|
||||
|
||||
// Invite Code
|
||||
GenerateInviteCode(ctx context.Context, orgID string, userID string) (string, error)
|
||||
JoinByInviteCode(ctx context.Context, inviteCode string, userID string, tenantID uint64) (*types.Organization, error)
|
||||
|
||||
// Join Requests (for organizations that require approval)
|
||||
SubmitJoinRequest(ctx context.Context, orgID string, userID string, tenantID uint64, message string, requestedRole types.OrgMemberRole) (*types.OrganizationJoinRequest, error)
|
||||
ListJoinRequests(ctx context.Context, orgID string) ([]*types.OrganizationJoinRequest, error)
|
||||
CountPendingJoinRequests(ctx context.Context, orgID string) (int64, error)
|
||||
ReviewJoinRequest(ctx context.Context, orgID string, requestID string, approved bool, reviewerID string, message string, assignRole *types.OrgMemberRole) error
|
||||
|
||||
// Role Upgrade Requests (for existing members to request higher permissions)
|
||||
RequestRoleUpgrade(ctx context.Context, orgID string, userID string, tenantID uint64, requestedRole types.OrgMemberRole, message string) (*types.OrganizationJoinRequest, error)
|
||||
GetPendingUpgradeRequest(ctx context.Context, orgID string, userID string) (*types.OrganizationJoinRequest, error)
|
||||
|
||||
// Permission Check
|
||||
IsOrgAdmin(ctx context.Context, orgID string, userID string) (bool, error)
|
||||
GetUserRoleInOrg(ctx context.Context, orgID string, userID string) (types.OrgMemberRole, error)
|
||||
}
|
||||
|
||||
// OrganizationRepository defines the organization repository interface
|
||||
type OrganizationRepository interface {
|
||||
// Organization CRUD
|
||||
Create(ctx context.Context, org *types.Organization) error
|
||||
GetByID(ctx context.Context, id string) (*types.Organization, error)
|
||||
GetByInviteCode(ctx context.Context, inviteCode string) (*types.Organization, error)
|
||||
ListByUserID(ctx context.Context, userID string) ([]*types.Organization, error)
|
||||
Update(ctx context.Context, org *types.Organization) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
|
||||
// Member operations
|
||||
AddMember(ctx context.Context, member *types.OrganizationMember) error
|
||||
RemoveMember(ctx context.Context, orgID string, userID string) error
|
||||
UpdateMemberRole(ctx context.Context, orgID string, userID string, role types.OrgMemberRole) error
|
||||
ListMembers(ctx context.Context, orgID string) ([]*types.OrganizationMember, error)
|
||||
GetMember(ctx context.Context, orgID string, userID string) (*types.OrganizationMember, error)
|
||||
CountMembers(ctx context.Context, orgID string) (int64, error)
|
||||
|
||||
// Invite code
|
||||
UpdateInviteCode(ctx context.Context, orgID string, inviteCode string, expiresAt *time.Time) error
|
||||
|
||||
// Join requests
|
||||
CreateJoinRequest(ctx context.Context, request *types.OrganizationJoinRequest) error
|
||||
GetJoinRequestByID(ctx context.Context, id string) (*types.OrganizationJoinRequest, error)
|
||||
GetPendingJoinRequest(ctx context.Context, orgID string, userID string) (*types.OrganizationJoinRequest, error)
|
||||
GetPendingRequestByType(ctx context.Context, orgID string, userID string, requestType types.JoinRequestType) (*types.OrganizationJoinRequest, error)
|
||||
ListJoinRequests(ctx context.Context, orgID string, status types.JoinRequestStatus) ([]*types.OrganizationJoinRequest, error)
|
||||
CountJoinRequests(ctx context.Context, orgID string, status types.JoinRequestStatus) (int64, error)
|
||||
UpdateJoinRequestStatus(ctx context.Context, id string, status types.JoinRequestStatus, reviewedBy string, reviewMessage string) error
|
||||
}
|
||||
|
||||
// KBShareService defines the knowledge base sharing service interface
|
||||
type KBShareService interface {
|
||||
// Share Management
|
||||
ShareKnowledgeBase(ctx context.Context, kbID string, orgID string, userID string, tenantID uint64, permission types.OrgMemberRole) (*types.KnowledgeBaseShare, error)
|
||||
UpdateSharePermission(ctx context.Context, shareID string, permission types.OrgMemberRole, userID string) error
|
||||
RemoveShare(ctx context.Context, shareID string, userID string) error
|
||||
|
||||
// Query
|
||||
ListSharesByKnowledgeBase(ctx context.Context, kbID string) ([]*types.KnowledgeBaseShare, error)
|
||||
ListSharesByOrganization(ctx context.Context, orgID string) ([]*types.KnowledgeBaseShare, error)
|
||||
ListSharedKnowledgeBases(ctx context.Context, userID string, currentTenantID uint64) ([]*types.SharedKnowledgeBaseInfo, error)
|
||||
GetShare(ctx context.Context, shareID string) (*types.KnowledgeBaseShare, error)
|
||||
GetShareByKBAndOrg(ctx context.Context, kbID string, orgID string) (*types.KnowledgeBaseShare, error)
|
||||
|
||||
// Permission Check
|
||||
CheckUserKBPermission(ctx context.Context, kbID string, userID string) (types.OrgMemberRole, bool, error)
|
||||
HasKBPermission(ctx context.Context, kbID string, userID string, requiredRole types.OrgMemberRole) (bool, error)
|
||||
|
||||
// Get source tenant for cross-tenant embedding
|
||||
GetKBSourceTenant(ctx context.Context, kbID string) (uint64, error)
|
||||
|
||||
// Count shares for knowledge bases
|
||||
CountSharesByKnowledgeBaseIDs(ctx context.Context, kbIDs []string) (map[string]int64, error)
|
||||
}
|
||||
|
||||
// KBShareRepository defines the knowledge base sharing repository interface
|
||||
type KBShareRepository interface {
|
||||
// CRUD
|
||||
Create(ctx context.Context, share *types.KnowledgeBaseShare) error
|
||||
GetByID(ctx context.Context, id string) (*types.KnowledgeBaseShare, error)
|
||||
GetByKBAndOrg(ctx context.Context, kbID string, orgID string) (*types.KnowledgeBaseShare, error)
|
||||
Update(ctx context.Context, share *types.KnowledgeBaseShare) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
// DeleteByKnowledgeBaseID soft-deletes all shares for a knowledge base (e.g. when KB is deleted)
|
||||
DeleteByKnowledgeBaseID(ctx context.Context, kbID string) error
|
||||
// DeleteByOrganizationID soft-deletes all shares for an organization (e.g. when the org is deleted)
|
||||
DeleteByOrganizationID(ctx context.Context, orgID string) error
|
||||
|
||||
// List
|
||||
ListByKnowledgeBase(ctx context.Context, kbID string) ([]*types.KnowledgeBaseShare, error)
|
||||
ListByOrganization(ctx context.Context, orgID string) ([]*types.KnowledgeBaseShare, error)
|
||||
|
||||
// Query for user's accessible shared knowledge bases
|
||||
ListSharedKBsForUser(ctx context.Context, userID string) ([]*types.KnowledgeBaseShare, error)
|
||||
|
||||
// Count shares
|
||||
CountSharesByKnowledgeBaseID(ctx context.Context, kbID string) (int64, error)
|
||||
CountSharesByKnowledgeBaseIDs(ctx context.Context, kbIDs []string) (map[string]int64, error)
|
||||
}
|
||||
@@ -36,6 +36,8 @@ type UserService interface {
|
||||
RevokeToken(ctx context.Context, token string) error
|
||||
// GetCurrentUser gets current user from context
|
||||
GetCurrentUser(ctx context.Context) (*types.User, error)
|
||||
// SearchUsers searches users by username or email
|
||||
SearchUsers(ctx context.Context, query string, limit int) ([]*types.User, error)
|
||||
}
|
||||
|
||||
// UserRepository defines the user repository interface
|
||||
@@ -54,6 +56,8 @@ type UserRepository interface {
|
||||
DeleteUser(ctx context.Context, id string) error
|
||||
// ListUsers lists users with pagination
|
||||
ListUsers(ctx context.Context, offset, limit int) ([]*types.User, error)
|
||||
// SearchUsers searches users by username or email
|
||||
SearchUsers(ctx context.Context, query string, limit int) ([]*types.User, error)
|
||||
}
|
||||
|
||||
// AuthTokenRepository defines the auth token repository interface
|
||||
|
||||
@@ -147,6 +147,12 @@ type ManualKnowledgePayload struct {
|
||||
TagID string `json:"tag_id"`
|
||||
}
|
||||
|
||||
// KnowledgeSearchScope defines a (tenant_id, knowledge_base_id) scope for knowledge search (e.g. own KBs + shared KBs).
|
||||
type KnowledgeSearchScope struct {
|
||||
TenantID uint64
|
||||
KBID string
|
||||
}
|
||||
|
||||
// NewManualKnowledgeMetadata creates a new ManualKnowledgeMetadata instance.
|
||||
func NewManualKnowledgeMetadata(content, status string, version int) *ManualKnowledgeMetadata {
|
||||
if version <= 0 {
|
||||
|
||||
@@ -81,6 +81,8 @@ type KnowledgeBase struct {
|
||||
IsProcessing bool `yaml:"is_processing" json:"is_processing" gorm:"-"`
|
||||
// ProcessingCount indicates the number of knowledge items being processed (for document type knowledge bases)
|
||||
ProcessingCount int64 `yaml:"processing_count" json:"processing_count" gorm:"-"`
|
||||
// ShareCount indicates the number of organizations this knowledge base is shared with (not stored in database)
|
||||
ShareCount int64 `yaml:"share_count" json:"share_count" gorm:"-"`
|
||||
}
|
||||
|
||||
// KnowledgeBaseConfig represents the knowledge base configuration
|
||||
|
||||
@@ -0,0 +1,373 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OrgMemberRole represents the role of an organization member
|
||||
type OrgMemberRole string
|
||||
|
||||
const (
|
||||
// OrgRoleAdmin has full control over the organization and shared knowledge bases
|
||||
OrgRoleAdmin OrgMemberRole = "admin"
|
||||
// OrgRoleEditor can edit shared knowledge base content but cannot manage settings
|
||||
OrgRoleEditor OrgMemberRole = "editor"
|
||||
// OrgRoleViewer can only view and search shared knowledge bases
|
||||
OrgRoleViewer OrgMemberRole = "viewer"
|
||||
)
|
||||
|
||||
// IsValid checks if the role is valid
|
||||
func (r OrgMemberRole) IsValid() bool {
|
||||
switch r {
|
||||
case OrgRoleAdmin, OrgRoleEditor, OrgRoleViewer:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// HasPermission checks if this role has at least the required permission level
|
||||
func (r OrgMemberRole) HasPermission(required OrgMemberRole) bool {
|
||||
roleLevel := map[OrgMemberRole]int{
|
||||
OrgRoleAdmin: 3,
|
||||
OrgRoleEditor: 2,
|
||||
OrgRoleViewer: 1,
|
||||
}
|
||||
return roleLevel[r] >= roleLevel[required]
|
||||
}
|
||||
|
||||
// Organization represents a collaboration organization for cross-tenant sharing
|
||||
type Organization struct {
|
||||
// Unique identifier of the organization
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
// Name of the organization
|
||||
Name string `json:"name" gorm:"type:varchar(255);not null"`
|
||||
// Description of the organization
|
||||
Description string `json:"description" gorm:"type:text"`
|
||||
// Avatar URL for display in list and settings
|
||||
Avatar string `json:"avatar" gorm:"type:varchar(512)"`
|
||||
// User ID of the organization owner
|
||||
OwnerID string `json:"owner_id" gorm:"type:varchar(36);not null;index"`
|
||||
// Unique invitation code for joining the organization
|
||||
InviteCode string `json:"invite_code" gorm:"type:varchar(32);uniqueIndex"`
|
||||
// When the current invite code expires; nil means no expiry
|
||||
InviteCodeExpiresAt *time.Time `json:"invite_code_expires_at" gorm:"type:timestamp with time zone"`
|
||||
// Invite link validity in days: 0=never, 1/7/30
|
||||
InviteCodeValidityDays int `json:"invite_code_validity_days" gorm:"default:7"`
|
||||
// Whether joining requires admin approval
|
||||
RequireApproval bool `json:"require_approval" gorm:"default:false"`
|
||||
// Creation time
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// Last updated time
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
// Deletion time (soft delete)
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
|
||||
// Associations (not stored in database)
|
||||
Owner *User `json:"owner,omitempty" gorm:"foreignKey:OwnerID"`
|
||||
Members []OrganizationMember `json:"members,omitempty" gorm:"foreignKey:OrganizationID"`
|
||||
Shares []KnowledgeBaseShare `json:"shares,omitempty" gorm:"foreignKey:OrganizationID"`
|
||||
}
|
||||
|
||||
// TableName returns the table name for GORM
|
||||
func (Organization) TableName() string {
|
||||
return "organizations"
|
||||
}
|
||||
|
||||
// OrganizationMember represents a member of an organization
|
||||
type OrganizationMember struct {
|
||||
// Unique identifier
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
// Organization ID
|
||||
OrganizationID string `json:"organization_id" gorm:"type:varchar(36);not null;index"`
|
||||
// User ID of the member
|
||||
UserID string `json:"user_id" gorm:"type:varchar(36);not null;index"`
|
||||
// Tenant ID that the member belongs to
|
||||
TenantID uint64 `json:"tenant_id" gorm:"not null;index"`
|
||||
// Role in the organization (admin/editor/viewer)
|
||||
Role OrgMemberRole `json:"role" gorm:"type:varchar(32);not null;default:'viewer'"`
|
||||
// Creation time
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// Last updated time
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
// Associations (not stored in database)
|
||||
Organization *Organization `json:"organization,omitempty" gorm:"foreignKey:OrganizationID"`
|
||||
User *User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
||||
}
|
||||
|
||||
// TableName returns the table name for GORM
|
||||
func (OrganizationMember) TableName() string {
|
||||
return "organization_members"
|
||||
}
|
||||
|
||||
// JoinRequestStatus represents the status of a join request
|
||||
type JoinRequestStatus string
|
||||
|
||||
const (
|
||||
JoinRequestStatusPending JoinRequestStatus = "pending"
|
||||
JoinRequestStatusApproved JoinRequestStatus = "approved"
|
||||
JoinRequestStatusRejected JoinRequestStatus = "rejected"
|
||||
)
|
||||
|
||||
// JoinRequestType represents the type of a join request
|
||||
type JoinRequestType string
|
||||
|
||||
const (
|
||||
// JoinRequestTypeJoin is for new member join requests
|
||||
JoinRequestTypeJoin JoinRequestType = "join"
|
||||
// JoinRequestTypeUpgrade is for role upgrade requests from existing members
|
||||
JoinRequestTypeUpgrade JoinRequestType = "upgrade"
|
||||
)
|
||||
|
||||
// OrganizationJoinRequest represents a request to join an organization or upgrade role
|
||||
type OrganizationJoinRequest struct {
|
||||
// Unique identifier
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
// Organization ID
|
||||
OrganizationID string `json:"organization_id" gorm:"type:varchar(36);not null;index"`
|
||||
// User ID of the requester
|
||||
UserID string `json:"user_id" gorm:"type:varchar(36);not null;index"`
|
||||
// Tenant ID of the requester
|
||||
TenantID uint64 `json:"tenant_id" gorm:"not null"`
|
||||
// Type of request: 'join' for new member, 'upgrade' for role upgrade
|
||||
RequestType JoinRequestType `json:"request_type" gorm:"type:varchar(32);not null;default:'join';index"`
|
||||
// Previous role before upgrade (only for upgrade requests)
|
||||
PrevRole OrgMemberRole `json:"prev_role" gorm:"column:prev_role;type:varchar(32)"`
|
||||
// Role requested by the applicant (admin/editor/viewer)
|
||||
RequestedRole OrgMemberRole `json:"requested_role" gorm:"type:varchar(32);not null;default:'viewer'"`
|
||||
// Status of the request
|
||||
Status JoinRequestStatus `json:"status" gorm:"type:varchar(32);not null;default:'pending';index"`
|
||||
// Optional message from the requester
|
||||
Message string `json:"message" gorm:"type:text"`
|
||||
// User ID of the admin who reviewed the request
|
||||
ReviewedBy string `json:"reviewed_by" gorm:"type:varchar(36)"`
|
||||
// Time when the request was reviewed
|
||||
ReviewedAt *time.Time `json:"reviewed_at"`
|
||||
// Optional message from the reviewer
|
||||
ReviewMessage string `json:"review_message" gorm:"type:text"`
|
||||
// Creation time
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// Last updated time
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
// Associations (not stored in database)
|
||||
Organization *Organization `json:"organization,omitempty" gorm:"foreignKey:OrganizationID"`
|
||||
User *User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
||||
Reviewer *User `json:"reviewer,omitempty" gorm:"foreignKey:ReviewedBy"`
|
||||
}
|
||||
|
||||
// TableName returns the table name for GORM
|
||||
func (OrganizationJoinRequest) TableName() string {
|
||||
return "organization_join_requests"
|
||||
}
|
||||
|
||||
// KnowledgeBaseShare represents a sharing record of a knowledge base to an organization
|
||||
type KnowledgeBaseShare struct {
|
||||
// Unique identifier
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
// Knowledge base ID being shared
|
||||
KnowledgeBaseID string `json:"knowledge_base_id" gorm:"type:varchar(36);not null;index"`
|
||||
// Organization ID receiving the share
|
||||
OrganizationID string `json:"organization_id" gorm:"type:varchar(36);not null;index"`
|
||||
// User ID who shared the knowledge base
|
||||
SharedByUserID string `json:"shared_by_user_id" gorm:"type:varchar(36);not null"`
|
||||
// Original tenant ID of the knowledge base (for cross-tenant embedding model access)
|
||||
SourceTenantID uint64 `json:"source_tenant_id" gorm:"not null;index"`
|
||||
// Permission level (admin/editor/viewer)
|
||||
Permission OrgMemberRole `json:"permission" gorm:"type:varchar(32);not null;default:'viewer'"`
|
||||
// Creation time
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// Last updated time
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
// Deletion time (soft delete)
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
|
||||
// Associations (not stored in database)
|
||||
KnowledgeBase *KnowledgeBase `json:"knowledge_base,omitempty" gorm:"foreignKey:KnowledgeBaseID"`
|
||||
Organization *Organization `json:"organization,omitempty" gorm:"foreignKey:OrganizationID"`
|
||||
}
|
||||
|
||||
// TableName returns the table name for GORM
|
||||
func (KnowledgeBaseShare) TableName() string {
|
||||
return "kb_shares"
|
||||
}
|
||||
|
||||
// SharedKnowledgeBaseInfo represents a shared knowledge base with additional sharing info
|
||||
type SharedKnowledgeBaseInfo struct {
|
||||
KnowledgeBase *KnowledgeBase `json:"knowledge_base"`
|
||||
ShareID string `json:"share_id"`
|
||||
OrganizationID string `json:"organization_id"`
|
||||
OrgName string `json:"org_name"`
|
||||
Permission OrgMemberRole `json:"permission"`
|
||||
SourceTenantID uint64 `json:"source_tenant_id"`
|
||||
SharedAt time.Time `json:"shared_at"`
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Request/Response Types
|
||||
// ----------------------
|
||||
|
||||
// CreateOrganizationRequest represents a request to create an organization
|
||||
type CreateOrganizationRequest struct {
|
||||
Name string `json:"name" binding:"required,min=1,max=255"`
|
||||
Description string `json:"description" binding:"max=1000"`
|
||||
Avatar string `json:"avatar" binding:"omitempty,max=512"` // optional avatar URL
|
||||
InviteCodeValidityDays *int `json:"invite_code_validity_days"` // optional: 0=never, 1, 7, 30; default 7
|
||||
}
|
||||
|
||||
// UpdateOrganizationRequest represents a request to update an organization
|
||||
type UpdateOrganizationRequest struct {
|
||||
Name *string `json:"name" binding:"omitempty,min=1,max=255"`
|
||||
Description *string `json:"description" binding:"omitempty,max=1000"`
|
||||
Avatar *string `json:"avatar" binding:"omitempty,max=512"` // optional avatar URL
|
||||
RequireApproval *bool `json:"require_approval"`
|
||||
InviteCodeValidityDays *int `json:"invite_code_validity_days"` // 0=never, 1, 7, 30
|
||||
}
|
||||
|
||||
// AddMemberRequest represents a request to add a member to an organization
|
||||
type AddMemberRequest struct {
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Role OrgMemberRole `json:"role" binding:"required"`
|
||||
}
|
||||
|
||||
// UpdateMemberRoleRequest represents a request to update a member's role
|
||||
type UpdateMemberRoleRequest struct {
|
||||
Role OrgMemberRole `json:"role" binding:"required"`
|
||||
}
|
||||
|
||||
// JoinOrganizationRequest represents a request to join an organization via invite code
|
||||
type JoinOrganizationRequest struct {
|
||||
InviteCode string `json:"invite_code" binding:"required,min=8,max=32"`
|
||||
}
|
||||
|
||||
// SubmitJoinRequestRequest represents a request to submit a join request for approval
|
||||
type SubmitJoinRequestRequest struct {
|
||||
InviteCode string `json:"invite_code" binding:"required,min=8,max=32"`
|
||||
Message string `json:"message" binding:"max=500"`
|
||||
Role OrgMemberRole `json:"role"` // Optional: role the applicant requests (admin/editor/viewer); default viewer
|
||||
}
|
||||
|
||||
// ReviewJoinRequestRequest represents a request to review a join request
|
||||
type ReviewJoinRequestRequest struct {
|
||||
Approved bool `json:"approved"`
|
||||
Message string `json:"message" binding:"max=500"`
|
||||
Role OrgMemberRole `json:"role"` // Optional: role to assign when approving; overrides applicant's requested role
|
||||
}
|
||||
|
||||
// RequestRoleUpgradeRequest represents a request to upgrade role in an organization
|
||||
type RequestRoleUpgradeRequest struct {
|
||||
RequestedRole OrgMemberRole `json:"requested_role" binding:"required"` // The role user wants to upgrade to
|
||||
Message string `json:"message" binding:"max=500"` // Optional message explaining the reason
|
||||
}
|
||||
|
||||
// InviteMemberRequest represents a request to directly invite a user to organization
|
||||
type InviteMemberRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"` // User ID to invite
|
||||
Role OrgMemberRole `json:"role" binding:"required"` // Role to assign: admin/editor/viewer
|
||||
}
|
||||
|
||||
// ShareKnowledgeBaseRequest represents a request to share a knowledge base
|
||||
type ShareKnowledgeBaseRequest struct {
|
||||
OrganizationID string `json:"organization_id" binding:"required"`
|
||||
Permission OrgMemberRole `json:"permission" binding:"required"`
|
||||
}
|
||||
|
||||
// UpdateSharePermissionRequest represents a request to update share permission
|
||||
type UpdateSharePermissionRequest struct {
|
||||
Permission OrgMemberRole `json:"permission" binding:"required"`
|
||||
}
|
||||
|
||||
// OrganizationResponse represents an organization in API responses
|
||||
type OrganizationResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Avatar string `json:"avatar,omitempty"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
InviteCode string `json:"invite_code,omitempty"`
|
||||
InviteCodeExpiresAt *time.Time `json:"invite_code_expires_at,omitempty"`
|
||||
InviteCodeValidityDays int `json:"invite_code_validity_days"`
|
||||
RequireApproval bool `json:"require_approval"`
|
||||
MemberCount int `json:"member_count"`
|
||||
ShareCount int `json:"share_count"` // 共享到该组织的知识库数量
|
||||
PendingJoinRequestCount int `json:"pending_join_request_count"` // 待审批加入申请数(仅管理员可见)
|
||||
IsOwner bool `json:"is_owner"`
|
||||
MyRole string `json:"my_role,omitempty"`
|
||||
HasPendingUpgrade bool `json:"has_pending_upgrade"` // 当前用户是否有待处理的权限升级申请
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// OrganizationMemberResponse represents a member in API responses
|
||||
type OrganizationMemberResponse struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Avatar string `json:"avatar"`
|
||||
Role string `json:"role"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
JoinedAt time.Time `json:"joined_at"`
|
||||
}
|
||||
|
||||
// KnowledgeBaseShareResponse represents a share record in API responses
|
||||
type KnowledgeBaseShareResponse struct {
|
||||
ID string `json:"id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
KnowledgeBaseName string `json:"knowledge_base_name"`
|
||||
KnowledgeBaseType string `json:"knowledge_base_type"`
|
||||
KnowledgeCount int64 `json:"knowledge_count"`
|
||||
ChunkCount int64 `json:"chunk_count"`
|
||||
OrganizationID string `json:"organization_id"`
|
||||
OrganizationName string `json:"organization_name"`
|
||||
SharedByUserID string `json:"shared_by_user_id"`
|
||||
SharedByUsername string `json:"shared_by_username"`
|
||||
SourceTenantID uint64 `json:"source_tenant_id"`
|
||||
Permission string `json:"permission"` // Share permission (what the space was granted: viewer/editor)
|
||||
MyRoleInOrg string `json:"my_role_in_org"` // Current user's role in this organization (admin/editor/viewer)
|
||||
MyPermission string `json:"my_permission"` // Effective permission for current user = min(Permission, MyRoleInOrg)
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
RequireApproval bool `json:"require_approval"`
|
||||
}
|
||||
|
||||
// ListOrganizationsResponse represents the response for listing organizations
|
||||
type ListOrganizationsResponse struct {
|
||||
Organizations []OrganizationResponse `json:"organizations"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// JoinRequestResponse represents a join request in API responses
|
||||
type JoinRequestResponse struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Message string `json:"message"`
|
||||
RequestType string `json:"request_type"` // 'join' or 'upgrade'
|
||||
PrevRole string `json:"prev_role"` // Previous role (only for upgrade requests)
|
||||
RequestedRole string `json:"requested_role"` // Role the applicant requested (admin/editor/viewer)
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ReviewedAt *time.Time `json:"reviewed_at,omitempty"`
|
||||
}
|
||||
|
||||
// ListJoinRequestsResponse represents the response for listing join requests
|
||||
type ListJoinRequestsResponse struct {
|
||||
Requests []JoinRequestResponse `json:"requests"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// ListMembersResponse represents the response for listing members
|
||||
type ListMembersResponse struct {
|
||||
Members []OrganizationMemberResponse `json:"members"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// ListSharesResponse represents the response for listing shares
|
||||
type ListSharesResponse struct {
|
||||
Shares []KnowledgeBaseShareResponse `json:"shares"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
@@ -22,6 +22,9 @@ type SearchTarget struct {
|
||||
Type SearchTargetType `json:"type"`
|
||||
// KnowledgeBaseID is the ID of the knowledge base to search
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
// TenantID is the tenant ID that owns this knowledge base
|
||||
// Required for cross-tenant shared KB queries
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
// KnowledgeIDs is the list of specific knowledge IDs to search within the knowledge base
|
||||
// Only used when Type is SearchTargetTypeKnowledge
|
||||
KnowledgeIDs []string `json:"knowledge_ids,omitempty"`
|
||||
@@ -43,6 +46,38 @@ func (st SearchTargets) GetAllKnowledgeBaseIDs() []string {
|
||||
return result
|
||||
}
|
||||
|
||||
// GetKBTenantMap returns a map from knowledge base ID to tenant ID
|
||||
func (st SearchTargets) GetKBTenantMap() map[string]uint64 {
|
||||
result := make(map[string]uint64)
|
||||
for _, t := range st {
|
||||
if t.KnowledgeBaseID != "" {
|
||||
result[t.KnowledgeBaseID] = t.TenantID
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetTenantIDForKB returns the tenant ID for a given knowledge base ID
|
||||
// Returns 0 if not found
|
||||
func (st SearchTargets) GetTenantIDForKB(kbID string) uint64 {
|
||||
for _, t := range st {
|
||||
if t.KnowledgeBaseID == kbID {
|
||||
return t.TenantID
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// ContainsKB checks if the search targets contain a given knowledge base ID
|
||||
func (st SearchTargets) ContainsKB(kbID string) bool {
|
||||
for _, t := range st {
|
||||
if t.KnowledgeBaseID == kbID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// SearchResult represents the search result
|
||||
type SearchResult struct {
|
||||
// ID
|
||||
|
||||
Reference in New Issue
Block a user