Skip to content
quarry Artifact [06e287da5d]
Login

Artifact [06e287da5d]

Artifact 06e287da5d6ded6b9b372eab4bfa340f3b90e3eb37b2c8923d63ee3673501fb4:


package api

import (
	"context"
	"errors"
	"fmt"
	html2 "html"
	"html/template"

	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

// setSiteAdmin is account.siteadmin: the flag is a person's to give or
// take from a person, never through a token (a leaked one must not
// mint a successor) and never by an agent.
func (d Deps) setSiteAdmin(ctx context.Context, in accountSiteAdminIn) error {
	if _, byToken := ops.TokenOf(ctx); byToken {
		return fmt.Errorf("the site-admin flag is given and taken by a person signed in, not by a token: %w", ops.ErrForbidden)
	}
	actor := ops.User(ctx)
	if actor == "" {
		return fmt.Errorf("changing the site-admin flag needs a signed-in account: %w", ops.ErrInvalid)
	}
	if d.operatorOf(actor) != "" {
		return fmt.Errorf("%s is an agent, and an agent never changes the site-admin flag: %w", actor, ops.ErrForbidden)
	}
	if d.Accounts == nil {
		return errNoAccounts()
	}
	if !d.isSiteAdmin(ctx, actor) {
		return fmt.Errorf("only a site admin changes the site-admin flag: %w", ops.ErrForbidden)
	}
	st, ok := d.Accounts.(SiteAdminStore)
	if !ok {
		return fmt.Errorf("this forge's account store cannot change the site-admin flag: %w", ops.ErrUnavailable)
	}
	if err := st.SetSiteAdmin(in.Name, in.On, actor); err != nil {
		var ref oltp.Refusal
		if errors.As(err, &ref) {
			return fmt.Errorf("%s: %w", ref.Reason, ops.ErrInvalid)
		}
		return fmt.Errorf("%v: %w", err, ops.ErrNotFound)
	}
	d.siteAdminNotice(ctx, actor, in.Name, in.On)
	return nil
}

// siteAdminNotice tells the account concerned, and every other open
// site admin, who changed whom and which way. Nobody is told of their
// own act, and a missing address is not a failure of the change.
func (d Deps) siteAdminNotice(ctx context.Context, actor, name string, on bool) {
	accts, err := d.Accounts.Accounts()
	if err != nil {
		return
	}
	verb := "took the site-admin flag from"
	if on {
		verb = "made"
	}
	tail := ""
	if on {
		tail = " a site admin"
	}
	what := actor + " " + verb + " " + name + tail
	detail := "A site admin runs the forge: every repository, every account. Changes like this are logged."
	m := accountMail{
		Kind: kindSiteAdmin, Subject: what, Text: what + ".\n\n" + detail,
		Context: template.HTML(strong(actor) + html2.EscapeString(what[len(actor):])),
		Body:    template.HTML("<p style=\"margin:0 0 12px;\">" + html2.EscapeString(detail) + "</p>"),
		Why:     "You're getting this as a security notice because you are a site admin, or the account concerned.",
	}
	if link := d.link("/fleet/people"); link != "" {
		m.Text += "\n\n" + link
		m.Open, m.URL = "People on this forge", link
	}
	if name != actor {
		d.mailAccount(ctx, name, m)
	}
	for _, a := range accts {
		if a.Admin && !a.AdminByInheritance && a.Operator == "" && a.Disabled == "" && a.Name != actor && a.Name != name {
			d.mailAccount(ctx, a.Name, m)
		}
	}
}