59 lines
2.1 KiB
Go
59 lines
2.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/metadata"
|
|
"time"
|
|
)
|
|
|
|
// CreateObjectInDomain records provenance atomically for the API's create,
|
|
// save, then bind workflow. It does not turn globally unique Objects into
|
|
// Domain-local identities, nor infer ownership of legacy unbound Objects.
|
|
func (s *Store) CreateObjectInDomain(domainUUID, kind string) (string, error) {
|
|
if s.ReadOnly {
|
|
return "", ErrReadOnly
|
|
}
|
|
if s.sqlBackend == storePostgreSQL {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
repo, err := metadata.NewApplication(s.DB, metadata.PostgreSQL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return repo.CreateObject(ctx, domainUUID, kind)
|
|
}
|
|
if err := s.ensureLocalDomain(domainUUID); err != nil {
|
|
return "", err
|
|
}
|
|
id := uuid.NewString()
|
|
tx, err := s.DB.Begin()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer tx.Rollback()
|
|
if _, err := tx.Exec(`INSERT INTO objects(uuid,kind,created_at) VALUES(?,?,strftime('%s','now'))`, id, kind); err != nil {
|
|
return "", err
|
|
}
|
|
if _, err := tx.Exec(`INSERT INTO object_origins(object_uuid,domain_uuid,created_at) VALUES(?,?,strftime('%s','now'))`, id, domainUUID); err != nil {
|
|
return "", err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return "", err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// ObjectInDomainAuthorityScope allows broad Domain grants only through a live
|
|
// binding, or through recorded creation provenance while there are NO bindings.
|
|
// Moving the last binding to another Domain never leaves source authority.
|
|
func (s *Store) ObjectInDomainAuthorityScope(domainUUID, objectUUID string) (bool, error) {
|
|
var allowed bool
|
|
err := s.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM objects o WHERE o.uuid=`+s.sqlParam(1)+` AND (
|
|
EXISTS(SELECT 1 FROM paths p WHERE p.object_uuid=o.uuid AND p.domain_uuid=`+s.sqlParam(2)+`) OR (
|
|
NOT EXISTS(SELECT 1 FROM paths p WHERE p.object_uuid=o.uuid) AND
|
|
EXISTS(SELECT 1 FROM object_origins x WHERE x.object_uuid=o.uuid AND x.domain_uuid=`+s.sqlParam(3)+`))
|
|
))`, objectUUID, domainUUID, domainUUID).Scan(&allowed)
|
|
return allowed, s.sqlError("object authority scope", err)
|
|
}
|