Settings → Billing shows the team's plan, trial, people, agents, storage and next invoice. The owner subscribes through Stripe Checkout (monthly, or yearly when there's a price) and pays or reads invoices in Stripe's Customer Portal; admins see the plan only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
201 lines
8.7 KiB
Go
201 lines
8.7 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"supermarket/internal/audit"
|
|
"supermarket/internal/users"
|
|
)
|
|
|
|
// The audit log: who did what. Sign-ins, accounts and connections are
|
|
// recorded where they happen; what's sent, published or deleted, by the
|
|
// table below, after the request succeeds.
|
|
|
|
// audit records something the viewer did.
|
|
func (s *Server) audit(r *http.Request, action, target, detail string) {
|
|
v := viewerOf(r)
|
|
e := audit.Entry{Action: action, Target: target, Detail: detail, IP: clientIP(r)}
|
|
if v.User != nil {
|
|
e.UserID, e.UserName = v.User.ID, displayName(v.User)
|
|
}
|
|
s.record(e)
|
|
}
|
|
|
|
// auditFor records something done for a user, without a request: a
|
|
// scheduled post going out, an email sent from the queue.
|
|
func (s *Server) auditFor(user int64, action, target, detail string) {
|
|
e := audit.Entry{Kind: audit.System, Action: action, Target: target, Detail: detail, UserID: user}
|
|
if user != 0 {
|
|
if u, err := users.ByID(s.DB, user); err == nil {
|
|
e.UserName = displayName(u)
|
|
}
|
|
}
|
|
s.record(e)
|
|
}
|
|
|
|
func (s *Server) record(e audit.Entry) {
|
|
if err := audit.Record(s.DB, e); err != nil {
|
|
s.Log.Error("audit: not recorded", "action", e.Action, "err", err)
|
|
}
|
|
}
|
|
|
|
func displayName(u *users.User) string {
|
|
if u.Name != "" {
|
|
return u.Name
|
|
}
|
|
return u.Email
|
|
}
|
|
|
|
// auditedRoutes are the changes recorded by their route, when they work:
|
|
// the route's pattern, then its {action} ("" for routes without one).
|
|
var auditedRoutes = map[string]map[string]string{
|
|
"POST /drafts/{id}/{action}": {"schedule": "scheduled", "posted": "marked-posted", "discard": "deleted"},
|
|
"POST /outreach/{id}/{action}": {"queue": "queued-email", "send-now": "sent-email", "mark-sent": "marked-sent", "discard": "deleted"},
|
|
"POST /prospects/{kind}/{id}/{action}": {"accept": "queued-for-crm", "accept-all": "queued-for-crm", "list": "added-to-list", "delete": "deleted", "suppress": "suppressed"},
|
|
"POST /seo/changes/{id}/{action}": {"yes": "opened-pull-request"},
|
|
"POST /seo/briefs/{id}/{action}": {"pr": "opened-pull-request", "drop": "deleted"},
|
|
"POST /sites/pages/{id}/{action}": {"remove": "deleted", "close": "closed-pull-request"},
|
|
"POST /sites/new": {"": "created-site"},
|
|
"POST /sites/pages/add": {"": "opened-pull-request"},
|
|
"POST /sites/theme/update": {"": "opened-pull-request"},
|
|
"POST /sites/previews/delete": {"": "deleted"},
|
|
"POST /seo/engines/setup": {"": "opened-pull-request"},
|
|
"POST /seo/engines/ping": {"": "sent-to-indexnow"},
|
|
"POST /metrics/publish": {"": "published"},
|
|
"POST /market/{id}/delete": {"": "deleted"},
|
|
"POST /signals/{id}/delete": {"": "deleted"},
|
|
"POST /funnel/{id}/delete": {"": "deleted"},
|
|
"POST /metrics/spend/{id}/delete": {"": "deleted"},
|
|
"POST /settings/backups/{action}": {"restore": "restored-backup", "delete": "deleted"},
|
|
"POST /crm/{id}/{action}": {"cancel": "deleted"},
|
|
"POST /prospects/suppressed": {"": "changed-suppression"},
|
|
"POST /settings/email/send-test": {"": "sent-email"},
|
|
"POST /settings/email/listmonk/sync": {"": "synced-mailing-lists"},
|
|
"POST /settings/profile": {"": "changed-profile"},
|
|
}
|
|
|
|
// statusWriter notes the response's status.
|
|
type statusWriter struct {
|
|
http.ResponseWriter
|
|
code int
|
|
}
|
|
|
|
func (w *statusWriter) WriteHeader(code int) {
|
|
if w.code == 0 {
|
|
w.code = code
|
|
}
|
|
w.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
func (w *statusWriter) Write(b []byte) (int, error) {
|
|
if w.code == 0 {
|
|
w.code = http.StatusOK
|
|
}
|
|
return w.ResponseWriter.Write(b)
|
|
}
|
|
|
|
// audited wraps a route in the table: a request that worked (a redirect
|
|
// or a page, not an error) is recorded.
|
|
func (s *Server) audited(pattern string, next http.Handler) http.Handler {
|
|
actions, ok := auditedRoutes[pattern]
|
|
if !ok {
|
|
return next
|
|
}
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
action, ok := actions[r.PathValue("action")]
|
|
if !ok {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
sw := &statusWriter{ResponseWriter: w}
|
|
next.ServeHTTP(sw, r)
|
|
// A failed form re-renders its page with 200 and an error: only
|
|
// redirects count as done.
|
|
if sw.code == http.StatusSeeOther || sw.code == http.StatusFound {
|
|
target := r.URL.Path
|
|
if k := r.PathValue("kind"); k != "" {
|
|
target = k + " " + r.PathValue("id")
|
|
}
|
|
s.audit(r, action, target, "")
|
|
}
|
|
})
|
|
}
|
|
|
|
// Settings → Activity.
|
|
|
|
type activityData struct {
|
|
Entries []audit.Entry
|
|
People []users.TeamUser
|
|
Filter audit.Filter
|
|
More int64 // the ID to page from; 0: no more
|
|
}
|
|
|
|
const activityPage = 100
|
|
|
|
func (s *Server) activityPage(w http.ResponseWriter, r *http.Request) {
|
|
if s.admin(w, r) == nil {
|
|
return
|
|
}
|
|
q := r.URL.Query()
|
|
f := audit.Filter{Action: strings.TrimSpace(q.Get("action")), Text: q.Get("q")}
|
|
f.UserID, _ = strconv.ParseInt(q.Get("user"), 10, 64)
|
|
f.Before, _ = strconv.ParseInt(q.Get("before"), 10, 64)
|
|
entries, err := audit.List(s.DB, f, activityPage+1)
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
d := activityData{Filter: f}
|
|
if len(entries) > activityPage {
|
|
entries = entries[:activityPage]
|
|
d.More = entries[len(entries)-1].ID
|
|
}
|
|
d.Entries = entries
|
|
d.People, _ = users.Members(s.DB, s.team())
|
|
s.render(w, r, "activity", page{Title: "Activity", Nav: "settings/activity", Data: d})
|
|
}
|
|
|
|
// actionLabels say what an action was, in the Activity list.
|
|
var actionLabels = map[string]string{
|
|
"setup": "set up Supermarket as its owner", "signin": "signed in", "signin-failed": "failed to sign in",
|
|
"locked": "was locked out", "signout": "signed out", "reset-asked": "asked to reset the password",
|
|
"password-reset": "reset the password", "password-changed": "changed the password",
|
|
"two-factor-on": "turned two-factor sign-in on", "two-factor-off": "turned two-factor sign-in off",
|
|
"recovery-codes": "made new recovery codes", "joined": "joined",
|
|
"invited": "invited", "invitation-resent": "sent an invitation again", "invitation-withdrawn": "withdrew an invitation",
|
|
"access-changed": "changed the access of", "disabled": "turned off the account of", "enabled": "turned on the account of",
|
|
"unlocked": "unlocked", "two-factor-reset": "reset two-factor sign-in for", "signed-out-user": "signed out everywhere",
|
|
"removed-user": "removed", "connected": "connected", "disconnected": "removed the connection to",
|
|
"account-signed-out": "signed out the posting account", "posting-grants": "changed who may post to which account",
|
|
"published": "published", "scheduled": "scheduled", "marked-posted": "marked as posted", "deleted": "deleted",
|
|
"queued-email": "queued an email", "sent-email": "sent an email", "marked-sent": "marked an email as sent",
|
|
"queued-for-crm": "queued for the CRM", "added-to-list": "added to a mailing list", "suppressed": "added to the suppression list",
|
|
"opened-pull-request": "opened a pull request", "closed-pull-request": "closed a pull request", "created-site": "started a site",
|
|
"sent-to-indexnow": "sent pages to IndexNow", "restored-backup": "restored a backup", "changed-suppression": "changed the suppression list",
|
|
"synced-mailing-lists": "synced the mailing lists", "changed-profile": "changed the company profile",
|
|
"agent-created": "made the agent", "agent-changed": "changed what this agent may do:", "key-rotated": "rotated the key of",
|
|
"key-made": "made a new key for", "key-revoked": "revoked a key of",
|
|
"asked-approval": "asked for approval to", "did-on-its-own": "did on its own:", "approved": "approved", "refused": "refused",
|
|
"allowed-on-its-own": "let an agent act on its own:", "withdrew-on-its-own": "took back what an agent may do on its own:",
|
|
"signin-settings": "changed how people sign in:", "billing-checkout": "went to subscribe:", "team-created": "set up the team", "viewed-as": "looked at the team's data:", "team-suspended": "suspended the team", "team-restored": "restored the team",
|
|
"domain-released": "released the domain", "exported": "exported", "downloaded-export": "downloaded the export", "team-deleting": "asked to delete the team", "team-undeleted": "undid the team's deletion", "invitations-resent": "sent the invitations again:", "domain-proved": "proved the domain", "agent-paused": "paused the agent", "agent-resumed": "resumed the agent", "agent-limits": "changed the limits of",
|
|
"wrote-draft": "wrote a draft:", "edited-draft": "edited a draft:", "edited-change": "edited a proposed change to", "market-added": "added to the Market", "market-changed": "changed in the Market",
|
|
}
|
|
|
|
func actionLabel(a string) string {
|
|
if l, ok := actionLabels[a]; ok {
|
|
return l
|
|
}
|
|
return strings.ReplaceAll(a, "-", " ")
|
|
}
|
|
|
|
// pruneAudit keeps two years of the log.
|
|
func (s *Server) pruneAudit(now time.Time) {
|
|
if _, err := audit.Prune(s.DB, 2*365*24*time.Hour, now); err != nil {
|
|
s.Log.Warn("audit: pruning", "err", err)
|
|
}
|
|
}
|