Files
archie/modules/governance/people.go
T
sam 1026d5e3b6 feat(httpapi): the API is described by an OpenAPI document generated with Huma (#2028)
Every /api operation is registered with Huma behind one admin-token
middleware, served at /api/openapi.json with a docs page at /api/docs,
and committed as api/openapi.yaml, which task check keeps current.
2026-11-16 20:05:05 +11:00

163 lines
5.4 KiB
Go

package governance
import (
"context"
"errors"
"fmt"
"net/http"
"slices"
"strings"
"github.com/danielgtaylor/huma/v2"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/modules/governance/internal/store"
)
// Verified is a person an identity was verified as.
type Verified struct {
// ID is the person's id, as the settings name them.
ID string
Name string
// Authority is whether the person may approve changes to policy and
// ceilings, such as raising an action class.
Authority bool
// Approver is whether the person may answer an approval for an action:
// they hold authority, are the default owner, or are on the escalation
// path. A person a request was put to as the target's owner may answer
// it without.
Approver bool
}
// Verify returns the person who holds identity, such as "telegram:1001"; ok is
// false when no person does, so the identity is not verified.
func (m *Module) Verify(ctx context.Context, identity string) (Verified, bool, error) {
p, ok, err := m.store.PersonByIdentity(ctx, m.orgID, identity)
if err != nil || !ok {
return Verified{}, false, err
}
set, err := m.Settings(ctx)
if err != nil {
return Verified{}, false, err
}
id := p.ID.String()
onPath := slices.ContainsFunc(set.Escalation, func(st Step) bool { return st.Person == id })
approver := p.Authority || set.DefaultOwner == id || onPath
return Verified{ID: id, Name: p.Name, Authority: p.Authority, Approver: approver}, true, nil
}
// PersonID returns the id of the person who holds identity; ok is false when
// nobody does.
func (m *Module) PersonID(ctx context.Context, identity string) (string, bool, error) {
p, ok, err := m.store.PersonByIdentity(ctx, m.orgID, identity)
if err != nil || !ok {
return "", false, err
}
return p.ID.String(), true, nil
}
// personRequest is a person as the API shows and sets them. An empty id is a
// new person.
type personRequest struct {
ID string `json:"id" required:"false" doc:"Empty for a new person"`
Name string `json:"name"`
Authority bool `json:"authority"`
Identities []string `json:"identities"`
}
func (m *Module) getPeople(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]personRequest], error) {
people, err := m.store.People(ctx, m.orgID)
if err != nil {
m.log.ErrorContext(ctx, "read people", "error", err)
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the people")
}
out := make([]personRequest, 0, len(people))
for _, p := range people {
out = append(out, personRequest{ID: p.ID.String(), Name: p.Name, Authority: p.Authority, Identities: p.Identities})
}
return httpapi.OK(out), nil
}
// putPeople replaces the organisation's people with the list in the request.
func (m *Module) putPeople(ctx context.Context, in *struct{ Body []personRequest }) (*httpapi.Reply[[]personRequest], error) {
req := in.Body
people, err := peopleOf(req)
if err != nil {
return nil, huma.NewError(http.StatusBadRequest, err.Error())
}
set, err := m.Settings(ctx)
if err != nil {
m.log.ErrorContext(ctx, "read settings", "error", err)
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the people")
}
if err := set.stillKnows(people); err != nil {
return nil, huma.NewError(http.StatusConflict, err.Error())
}
if err := m.store.ReplacePeople(ctx, m.orgID, people); err != nil {
m.log.ErrorContext(ctx, "put people", "error", err)
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the people")
}
return m.getPeople(ctx, nil)
}
// stillKnows refuses to remove a person the settings name.
func (s Settings) stillKnows(people []store.Person) error {
if err := s.knows(people); err != nil {
return errors.New("a person the settings name can't be removed; change the settings first")
}
return nil
}
// peopleOf validates a request and returns the people it describes. A name is
// kept once, and an identity belongs to one person.
func peopleOf(req []personRequest) ([]store.Person, error) {
people := make([]store.Person, 0, len(req))
names := map[string]bool{}
held := map[string]bool{}
for _, pr := range req {
name := strings.TrimSpace(pr.Name)
if name == "" {
return nil, errors.New("every person needs a name")
}
key := strings.ToLower(name)
if names[key] {
return nil, fmt.Errorf("%q is listed twice", name)
}
names[key] = true
id := uuid.New()
if pr.ID != "" {
parsed, err := uuid.Parse(pr.ID)
if err != nil {
return nil, fmt.Errorf("%q has an id that isn't valid", name)
}
id = parsed
}
idents := make([]string, 0, len(pr.Identities))
for _, raw := range pr.Identities {
identity, err := ParseIdentity(raw)
if err != nil {
return nil, err
}
if held[identity] {
return nil, fmt.Errorf("%q belongs to one person only", identity)
}
held[identity] = true
idents = append(idents, identity)
}
people = append(people, store.Person{ID: id, Name: name, Authority: pr.Authority, Identities: idents})
}
return people, nil
}
// ParseIdentity returns raw as an identity: a channel and an account on it,
// such as "telegram:1001".
func ParseIdentity(raw string) (string, error) {
identity := strings.TrimSpace(raw)
channel, account, ok := strings.Cut(identity, ":")
if !ok || channel == "" || account == "" || strings.ContainsAny(identity, " \t\n") {
return "", fmt.Errorf("%q isn't an identity; write the channel and the account, such as telegram:1001", raw)
}
return identity, nil
}