Phase 10, part 8: the audit log, and who made what
Sign-ins and failed ones, everything admins do to users, connections, posting accounts, each post published and email sent, pull requests and deletions go in an audit log (schema 36), kept two years; admins read it under Settings → Activity. Drafts, published posts and outreach emails show who made them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+14
-2
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"supermarket/internal/appmail"
|
||||
"supermarket/internal/audit"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
@@ -199,6 +200,7 @@ func (s *Server) finishSignIn(w http.ResponseWriter, r *http.Request, u *users.U
|
||||
return
|
||||
}
|
||||
s.Log.Info("signin", "user", u.Email, "ip", clientIP(r))
|
||||
s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "signin", IP: clientIP(r)})
|
||||
if u.TwoFactor && u.RecoveryLeft < 3 {
|
||||
redirect(w, r, "/settings/account#two-factor", "codes-low")
|
||||
return
|
||||
@@ -220,11 +222,14 @@ func (s *Server) countFailure(ipKey, emailKey string, u *users.User, now time.Ti
|
||||
s.Log.Warn("signin: counting a failure", "err", err)
|
||||
}
|
||||
}
|
||||
ip := strings.TrimPrefix(ipKey, "signin:ip:")
|
||||
if u == nil {
|
||||
s.Log.Info("signin: failed, no such account", "key", emailKey, "ip", strings.TrimPrefix(ipKey, "signin:ip:"))
|
||||
s.Log.Info("signin: failed, no such account", "key", emailKey, "ip", ip)
|
||||
s.record(audit.Entry{Action: "signin-failed", Target: strings.TrimPrefix(emailKey, "signin:email:"), Detail: "no such account", IP: ip})
|
||||
return
|
||||
}
|
||||
s.Log.Info("signin: failed", "user", u.Email, "ip", strings.TrimPrefix(ipKey, "signin:ip:"))
|
||||
s.Log.Info("signin: failed", "user", u.Email, "ip", ip)
|
||||
s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "signin-failed", Target: u.Email, IP: ip})
|
||||
locked, err := users.FailedSignIn(s.DB, u, now)
|
||||
if err != nil {
|
||||
s.Log.Warn("signin: counting a failure", "err", err)
|
||||
@@ -232,6 +237,7 @@ func (s *Server) countFailure(ipKey, emailKey string, u *users.User, now time.Ti
|
||||
}
|
||||
if locked {
|
||||
s.Log.Warn("signin: account locked", "user", u.Email)
|
||||
s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "locked", Target: u.Email, Detail: strconv.Itoa(users.LockAfter) + " failed sign-ins in a row", IP: ip})
|
||||
s.sendMail(appmail.Message{To: u.Email, ToName: u.Name, Subject: "Your Supermarket account is locked",
|
||||
Body: "Hello" + nameSuffix(u.Name) + ",\n\n" +
|
||||
"Your account at " + s.PublicURL + " was locked after " + strconv.Itoa(users.LockAfter) + " failed sign-ins in a row.\n\n" +
|
||||
@@ -242,6 +248,9 @@ func (s *Server) countFailure(ipKey, emailKey string, u *users.User, now time.Ti
|
||||
}
|
||||
|
||||
func (s *Server) signout(w http.ResponseWriter, r *http.Request) {
|
||||
if viewerOf(r).User != nil {
|
||||
s.audit(r, "signout", "", "")
|
||||
}
|
||||
if err := s.endSession(w, r); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
@@ -286,6 +295,7 @@ func (s *Server) forgot(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
s.Log.Info("signin: password reset asked for", "user", u.Email, "ip", clientIP(r))
|
||||
s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "reset-asked", Target: u.Email, IP: clientIP(r)})
|
||||
s.sendMail(appmail.Message{To: u.Email, ToName: u.Name, Subject: "Reset your Supermarket password",
|
||||
Body: "Hello" + nameSuffix(u.Name) + ",\n\n" +
|
||||
"Someone, probably you, asked to reset your password at " + s.PublicURL + ".\n\n" +
|
||||
@@ -361,6 +371,7 @@ func (s *Server) reset(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
_ = users.ClearFails(s.DB, "signin:email:"+u.Email)
|
||||
s.Log.Info("signin: password reset", "user", u.Email, "ip", clientIP(r))
|
||||
s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "password-reset", Target: u.Email, IP: clientIP(r)})
|
||||
redirect(w, r, "/signin?email="+url.QueryEscape(u.Email), "password-reset")
|
||||
}
|
||||
|
||||
@@ -407,6 +418,7 @@ func (s *Server) RunAccounts(ctx context.Context) {
|
||||
if err := users.PruneThrottle(s.DB, now); err != nil {
|
||||
s.Log.Warn("accounts: pruning counts", "err", err)
|
||||
}
|
||||
s.pruneAudit(now)
|
||||
})
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
||||
Reference in New Issue
Block a user