package web import ( "errors" "net/http" "tug/internal/transport/web/text" ) type adminPendingData struct { Viewer Ident ID string Username Ident Expires Ident Spent bool Message text.Key } func (a *admin) pendingAccount(w http.ResponseWriter, r *http.Request) (Principal, string, AdminPendingAccount, bool) { p, ok := a.kit.principal(w, r) if !ok { return Principal{}, "", AdminPendingAccount{}, false } id, ok := a.id(w, r) if !ok { return Principal{}, "", AdminPendingAccount{}, false } acct, err := a.b.PendingAccount(r.Context(), id) if err != nil { a.failed(w, r, "pending account", err) return Principal{}, "", AdminPendingAccount{}, false } return p, id, acct, true } func (a *admin) pendingPage(w http.ResponseWriter, r *http.Request, p Principal, id string, acct AdminPendingAccount, message text.Key) { if acct.Spent && message == "" { message = text.AdminProblemConfirmSpent } a.render(w, r, http.StatusOK, "admin-user-confirm", adminPendingData{Viewer: p.Username, ID: id, Username: Ident(acct.Username), Expires: Ident(stamp(acct.ExpiresAt)), Spent: acct.Spent, Message: message}) } func (a *admin) confirmShow(w http.ResponseWriter, r *http.Request) { if p, id, acct, ok := a.pendingAccount(w, r); ok { a.pendingPage(w, r, p, id, acct, "") } } func (a *admin) confirmRun(w http.ResponseWriter, r *http.Request) { p, id, acct, ok := a.pendingAccount(w, r) if !ok { return } err := a.b.ConfirmAccount(r.Context(), p.UserID, id, r.PostFormValue("code")) switch { case errors.Is(err, ErrAdminConfirmCode): fresh, err := a.b.PendingAccount(r.Context(), id) if err != nil { a.failed(w, r, "pending account", err) return } if fresh.Spent { a.pendingPage(w, r, p, id, fresh, text.AdminProblemConfirmSpent) return } a.pendingPage(w, r, p, id, fresh, text.AdminProblemConfirmCode) case errors.Is(err, ErrAdminConfirmSpent): acct.Spent = true a.pendingPage(w, r, p, id, acct, text.AdminProblemConfirmSpent) case err != nil: a.failed(w, r, "confirm account", err) default: a.done(w, r, text.FlashAdminUserConfirmed, Ident(acct.Username), "/users/"+id) } }