37 lines
1.2 KiB
Go
37 lines
1.2 KiB
Go
package im
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"github.com/Tencent/WeKnora/internal/types"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// A digital human's default Agent carries its creator's personal connections.
|
||
|
|
// Tenant administrator privileges do not transfer that personal ownership.
|
||
|
|
func (s *Service) CheckPersonalChannelAgentAccess(ctx context.Context, agentID string, tenantID uint64) error {
|
||
|
|
var human types.DigitalHuman
|
||
|
|
err := s.db.WithContext(ctx).Select("id", "owner_id").Where("primary_agent_id = ? AND tenant_id = ?", agentID, tenantID).First(&human).Error
|
||
|
|
if err == gorm.ErrRecordNotFound {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
userID, hasUser := types.UserIDFromContext(ctx)
|
||
|
|
if !hasUser || types.IsSyntheticUserID(userID) || userID != human.OwnerID {
|
||
|
|
return gorm.ErrRecordNotFound
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func excludeOtherPersonalChannels(query *gorm.DB, ctx context.Context) *gorm.DB {
|
||
|
|
userID, hasUser := types.UserIDFromContext(ctx)
|
||
|
|
if !hasUser || types.IsSyntheticUserID(userID) {
|
||
|
|
userID = ""
|
||
|
|
}
|
||
|
|
return query.Where(`NOT EXISTS (SELECT 1 FROM digital_humans AS dh
|
||
|
|
WHERE dh.primary_agent_id = c.agent_id AND dh.tenant_id = c.tenant_id
|
||
|
|
AND dh.deleted_at IS NULL AND dh.owner_id <> ?)`, userID)
|
||
|
|
}
|