81 lines
3.1 KiB
Go
81 lines
3.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
// These bounded lookups do not grant access. Callers must check the live
|
|
// path/locality policy before exposing an alias or a projected URL.
|
|
func (s *Store) ObjectAliasPaths(ctx context.Context, domain, object string) (out []string, err error) {
|
|
defer func() { err = s.sqlError("object aliases", err) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT path FROM paths WHERE domain_uuid=`+s.sqlParam(1)+` AND object_uuid=`+s.sqlParam(2)+` ORDER BY length(path),path LIMIT 100`, domain, object)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var path string
|
|
if err = rows.Scan(&path); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, path)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) ActiveDomainMember(ctx context.Context, domain, principal string) (active bool, err error) {
|
|
defer func() { err = s.sqlError("active membership", err) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
err = s.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domain_memberships m JOIN principals p ON p.uuid=m.principal_uuid JOIN domains d ON d.uuid=m.domain_uuid WHERE m.domain_uuid=`+s.sqlParam(1)+` AND m.principal_uuid=`+s.sqlParam(2)+` AND m.state='active' AND p.state='active' AND d.enabled=1)`, domain, principal).Scan(&active)
|
|
return
|
|
}
|
|
|
|
func (s *Store) CityZoneKind(ctx context.Context, domain, id string) (kind string, err error) {
|
|
defer func() { err = s.sqlError("city zone kind", err) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
err = s.DB.QueryRowContext(ctx, `SELECT kind FROM zones WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2), id, domain).Scan(&kind)
|
|
return
|
|
}
|
|
|
|
func (s *Store) PeriodicalProjectionTargets(ctx context.Context, source, kind string) (out []string, err error) {
|
|
defer func() { err = s.sqlError("periodical projection targets", err) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT domain_uuid FROM periodical_projections WHERE source_uuid=`+s.sqlParam(1)+` AND kind=`+s.sqlParam(2)+` ORDER BY domain_uuid LIMIT 65`, source, kind)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var id string
|
|
if err = rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, id)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// ConfigTargetCampus resolves the exact target of a management request.
|
|
// Missing/invalid targets must not fall back to the current request's campus.
|
|
func (s *Store) ConfigTargetCampus(ctx context.Context, domainTarget bool, id int64) (campus string, err error) {
|
|
defer func() { err = s.sqlError("configuration target", err) }()
|
|
if id < 1 {
|
|
return "", sql.ErrNoRows
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
query := `SELECT uuid FROM campuses WHERE id=` + s.sqlParam(1)
|
|
if domainTarget {
|
|
query = `SELECT campus_uuid FROM domains WHERE id=` + s.sqlParam(1)
|
|
}
|
|
err = s.DB.QueryRowContext(ctx, query, id).Scan(&campus)
|
|
return
|
|
}
|