diff --git a/internal/dao/memory.go b/internal/dao/memory.go index 4925312d35..5a39aeddb1 100644 --- a/internal/dao/memory.go +++ b/internal/dao/memory.go @@ -378,3 +378,32 @@ func (dao *MemoryDAO) GetByFilter(userID string, tenantIDs []string, memoryTypes return memories, total, nil } + +// Accessible check if it is possible for user to access the memory +func (dao *MemoryDAO) Accessible(userID, memoryID string) (bool, error) { + memory, err := dao.GetByID(memoryID) + if err != nil { + return false, err + } + + if memory.TenantID == userID { + return true, nil + } + + if memory.Permissions != string(entity.TenantPermissionTeam) { + return false, fmt.Errorf("user %s have no access to this memory", userID) + } + + var count int64 + err = DB.Table("user_tenant"). + Where("tenant_id = ? AND user_id = ? AND status = ?", memory.TenantID, userID, "1"). + Count(&count).Error + if err != nil { + return false, err + } + if count > 0 { + return true, nil + } + + return false, fmt.Errorf("user %s have no access to this memory", userID) +} diff --git a/internal/service/memory.go b/internal/service/memory.go index 40491b4b65..3e0129434e 100644 --- a/internal/service/memory.go +++ b/internal/service/memory.go @@ -447,6 +447,9 @@ func (s *MemoryService) CreateMemory(tenantID string, req *CreateMemoryRequest) // resp, err := service.UpdateMemory("tenant123", "memory456", req) func (s *MemoryService) UpdateMemory(tenantID string, memoryID string, req *UpdateMemoryRequest) (*CreateMemoryResponse, error) { updateDict := make(map[string]interface{}) + if ok, err := s.memoryDAO.Accessible(tenantID, memoryID); !ok || err != nil { + return nil, err + } currentMemory, err := s.memoryDAO.GetByID(memoryID) if err != nil {