94 lines
3.5 KiB
Go
94 lines
3.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"github.com/google/uuid"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Topology validation, default settings and inherited administrators form one
|
|
// transaction. A second connection must never observe a Domain without its
|
|
// built-in groups, or retain Campus privileges after a Domain moves.
|
|
func (s *Store) writeDomain(ctx context.Context, id int64, update bool, campus, name, title, host, placement, instance, fingerprint string, enabled bool) error {
|
|
if s.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
name = normalizeDomainName(name)
|
|
title = strings.TrimSpace(title)
|
|
host = normalizeDomainHost(host)
|
|
if name == "" {
|
|
return namespaceValidationError("domain name is required")
|
|
}
|
|
if update && id <= 0 {
|
|
return namespaceValidationError("domain id is required")
|
|
}
|
|
if title == "" {
|
|
title = name
|
|
}
|
|
var err error
|
|
placement, err = normalizeDomainPlacement(placement)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.namespaceWrite(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
|
if err := s.ensureCampusExists(ctx, tx, campus); err != nil {
|
|
return err
|
|
}
|
|
instance, fingerprint, err = s.normalizeDomainInstance(ctx, tx, campus, instance, fingerprint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := s.ensureDomainUnique(ctx, tx, id, campus, name, host); err != nil {
|
|
return err
|
|
}
|
|
key := uuid.NewString()
|
|
if update {
|
|
var mode string
|
|
if err := tx.QueryRowContext(ctx, `SELECT d.uuid,c.authority_mode FROM domains d JOIN campuses c ON c.uuid=d.campus_uuid WHERE d.id=`+s.sqlParam(1), id).Scan(&key, &mode); err != nil {
|
|
return err
|
|
}
|
|
if mode == "managed" {
|
|
return namespaceValidationError("managed campus topology is read-only")
|
|
}
|
|
if placement == "remote" {
|
|
var used bool
|
|
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM paths WHERE domain_uuid=`+s.sqlParam(1)+`)`, key).Scan(&used); err != nil {
|
|
return err
|
|
}
|
|
if used {
|
|
return namespaceValidationError("domain with local paths cannot become remote")
|
|
}
|
|
}
|
|
}
|
|
active := 0
|
|
if enabled {
|
|
active = 1
|
|
}
|
|
now := time.Now().Unix()
|
|
if update {
|
|
_, err = tx.ExecContext(ctx, `UPDATE domains SET name=`+s.sqlParam(1)+`,title=`+s.sqlParam(2)+`,domain=`+s.sqlParam(3)+`,enabled=`+s.sqlParam(4)+`,campus_uuid=`+s.sqlParam(5)+`,placement=`+s.sqlParam(6)+`,instance_fqdn=`+s.sqlParam(7)+`,instance_fingerprint=`+s.sqlParam(8)+` WHERE id=`+s.sqlParam(9), name, title, host, active, campus, placement, instance, fingerprint, id)
|
|
} else {
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO domains(uuid,name,title,domain,enabled,created_at,campus_uuid,placement,instance_fqdn,instance_fingerprint) VALUES(`+s.sqlValues(10)+`)`, key, name, title, host, active, now, campus, placement, instance, fingerprint)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
hasConfig := true
|
|
if s.sqlBackend == storeSQLite {
|
|
// Legacy SQLite maintenance stores initialize configuration lazily.
|
|
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='config')`).Scan(&hasConfig); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if !update && hasConfig {
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO config(domain_uuid,key,value,updated_at) SELECT `+s.sqlParam(1)+`,'locale',CASE WHEN lower(default_lang) LIKE 'ja%' THEN 'ja' ELSE 'en' END,`+s.sqlParam(2)+` FROM campuses WHERE uuid=`+s.sqlParam(3)+` ON CONFLICT(domain_uuid,key) DO NOTHING`, key, now, campus)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return s.ensureBuiltinAdminGroupsContext(ctx, tx)
|
|
})
|
|
}
|