135 lines
4.9 KiB
Go
135 lines
4.9 KiB
Go
package adminweb
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/dingtalkconfig"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/secretstore"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/settings"
|
|
)
|
|
|
|
type ConfigRepository interface {
|
|
ActiveDingTalkConfig(context.Context, string) (string, string, bool, error)
|
|
CandidateDingTalkConfig(context.Context, string) (string, string, error)
|
|
SaveDingTalkConfig(context.Context, string, string, string, bool) error
|
|
MarkDingTalkCandidateFailed(context.Context, string, string, string) error
|
|
AdministratorCount(context.Context, string) (int, error)
|
|
ActiveBindings(context.Context, string) (map[string]settings.ResourceBinding, error)
|
|
CreateBootstrapChallenge(context.Context, string, string, time.Time) error
|
|
ConfirmBootstrap(context.Context, string) error
|
|
}
|
|
type CandidateActivator interface{ ActivateCandidate(context.Context) error }
|
|
type CredentialVerifier interface {
|
|
Verify(context.Context, string, string) dingtalkconfig.Result
|
|
}
|
|
type ControlService struct {
|
|
tenant string
|
|
repo ConfigRepository
|
|
secrets *secretstore.Store
|
|
verifier CredentialVerifier
|
|
activator CandidateActivator
|
|
}
|
|
|
|
func NewControlService(tenant string, repo ConfigRepository, secrets *secretstore.Store, verifier CredentialVerifier) (*ControlService, error) {
|
|
if strings.TrimSpace(tenant) == "" || repo == nil || secrets == nil || verifier == nil {
|
|
return nil, errors.New("admin control dependencies are required")
|
|
}
|
|
return &ControlService{tenant: strings.TrimSpace(tenant), repo: repo, secrets: secrets, verifier: verifier}, nil
|
|
}
|
|
func (s *ControlService) Snapshot(ctx context.Context) (Snapshot, error) {
|
|
id, ref, verified, err := s.repo.ActiveDingTalkConfig(ctx, s.tenant)
|
|
if err != nil {
|
|
return Snapshot{}, err
|
|
}
|
|
admins, err := s.repo.AdministratorCount(ctx, s.tenant)
|
|
if err != nil {
|
|
return Snapshot{}, err
|
|
}
|
|
bindings, err := s.repo.ActiveBindings(ctx, s.tenant)
|
|
if err != nil {
|
|
return Snapshot{}, err
|
|
}
|
|
state := settings.DetermineState(settings.Snapshot{ClientID: id, SecretConfigured: s.secrets.Configured(ref), CredentialsVerified: verified, AdministratorCount: admins})
|
|
components := []Component{{Name: "PostgreSQL", State: "ok", Detail: "配置仓库可用"}, {Name: "钉钉OAuth", State: status(verified), Detail: "凭据验证状态"}, {Name: "管理员", State: countStatus(admins), Detail: itoa(admins) + " 位有效管理员"}, {Name: "技术支持群", State: bindingStatus(bindings, "support_group")}, {Name: "问题AI表格", State: bindingStatus(bindings, "question_table")}, {Name: "FAQ表格", State: bindingStatus(bindings, "faq_table")}}
|
|
return Snapshot{SystemState: string(state), ClientIDMasked: maskID(id), SecretConfigured: s.secrets.Configured(ref), Components: components}, nil
|
|
}
|
|
func (s *ControlService) VerifyCredentials(ctx context.Context, id, secret string) Verification {
|
|
r := s.verifier.Verify(ctx, id, secret)
|
|
return Verification{OK: r.OK, Code: r.Code, CheckedAt: r.CheckedAt}
|
|
}
|
|
func (s *ControlService) SaveCredentials(ctx context.Context, id, secret string) error {
|
|
_, oldRef, _ := s.repo.CandidateDingTalkConfig(ctx, s.tenant)
|
|
ref, err := s.secrets.Put("dingtalk", secret)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = s.repo.SaveDingTalkConfig(ctx, s.tenant, strings.TrimSpace(id), ref, false); err != nil {
|
|
_ = s.secrets.Delete(ref)
|
|
return err
|
|
}
|
|
if oldRef != "" && oldRef != ref {
|
|
_ = s.secrets.Delete(oldRef)
|
|
}
|
|
return nil
|
|
}
|
|
func (s *ControlService) SetCandidateActivator(activator CandidateActivator) { s.activator = activator }
|
|
func (s *ControlService) ActivateCandidate(ctx context.Context) error {
|
|
if s.activator == nil {
|
|
return errors.New("candidate activator unavailable")
|
|
}
|
|
_, ref, err := s.repo.CandidateDingTalkConfig(ctx, s.tenant)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = s.activator.ActivateCandidate(ctx); err != nil {
|
|
_ = s.repo.MarkDingTalkCandidateFailed(ctx, s.tenant, ref, "activation_failed")
|
|
return errors.New("candidate activation failed")
|
|
}
|
|
return nil
|
|
}
|
|
func (s *ControlService) CreateBootstrap(ctx context.Context, code string) error {
|
|
return s.repo.CreateBootstrapChallenge(ctx, s.tenant, code, time.Now().Add(10*time.Minute))
|
|
}
|
|
func (s *ControlService) ConfirmBootstrap(ctx context.Context) error {
|
|
return s.repo.ConfirmBootstrap(ctx, s.tenant)
|
|
}
|
|
func maskID(v string) string {
|
|
v = strings.TrimSpace(v)
|
|
if len(v) <= 6 {
|
|
return v
|
|
}
|
|
return v[:3] + "***" + v[len(v)-3:]
|
|
}
|
|
func status(ok bool) string {
|
|
if ok {
|
|
return "ok"
|
|
}
|
|
return "unconfigured"
|
|
}
|
|
func countStatus(n int) string {
|
|
if n > 0 {
|
|
return "ok"
|
|
}
|
|
return "unconfigured"
|
|
}
|
|
func bindingStatus(m map[string]settings.ResourceBinding, k string) string {
|
|
if _, ok := m[k]; ok {
|
|
return "configured"
|
|
}
|
|
return "unconfigured"
|
|
}
|
|
func itoa(n int) string {
|
|
if n == 0 {
|
|
return "0"
|
|
}
|
|
b := make([]byte, 0, 8)
|
|
for n > 0 {
|
|
b = append([]byte{byte('0' + n%10)}, b...)
|
|
n /= 10
|
|
}
|
|
return string(b)
|
|
}
|