Files
supermarket/internal/web/core.go
T
davidandClaude Opus 5.5 1865140507 Phase 13, part 6: each team's Core
On the server and multi-tenant editions a team's admin enters its Core's
domain and account under Settings → Core & Hermes, instead of core.conf;
Hermes connects from the team's side with an agent's key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 12:29:44 -07:00

77 lines
2.2 KiB
Go

package web
import (
"errors"
"net/http"
"regexp"
"strings"
"supermarket/internal/core"
"supermarket/internal/users"
)
// The Federated Core a team uses. The local edition reads CoreDesktop's
// core.conf; on the server and multi-tenant editions each team's admin
// enters its Core's domain under Settings → Core & Hermes. Hermes and the
// CRM bridge then connect from the team's side with an agent's key: the
// platform never holds a Core's admin credentials.
const coreKey = "core:team"
type coreSetting struct {
Domain string `json:"domain"`
Email string `json:"email"` // the Core account Hermes acts for (its MCP hub user)
}
// core is the team's Core, or nil.
func (s *Server) core() *core.Core {
if !s.signsIn() {
return s.Core
}
var c coreSetting
if ok, err := s.DB.GetJSON(coreKey, &c); err != nil || !ok || c.Domain == "" {
return nil
}
return &core.Core{Domain: c.Domain, Email: c.Email}
}
var hostRe = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$`)
// coreSave sets (or, empty, forgets) the team's Core.
func (s *Server) coreSave(w http.ResponseWriter, r *http.Request) {
if !s.signsIn() {
http.NotFound(w, r)
return
}
domain := strings.ToLower(strings.TrimSpace(r.PostFormValue("domain")))
domain = strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(domain, "https://"), "http://"), "/")
email := strings.TrimSpace(r.PostFormValue("email"))
if domain == "" {
if err := s.DB.PutJSON(coreKey, coreSetting{}); err != nil {
s.serverError(w, err)
return
}
s.recordConnection(r, "core", false)
redirect(w, r, "/settings/core", "saved")
return
}
var problem error
if !hostRe.MatchString(domain) {
problem = errors.New("give the Core's domain, such as example.com")
} else if email != "" {
if _, err := users.NormalEmail(email); err != nil {
problem = errors.New("the Core account isn't an email address")
}
}
if problem != nil {
s.renderCore(w, r, sentence(problem.Error())+".")
return
}
if err := s.DB.PutJSON(coreKey, coreSetting{Domain: domain, Email: email}); err != nil {
s.serverError(w, err)
return
}
s.recordConnection(r, "core", true)
redirect(w, r, "/settings/core", "saved")
}