@@ -0,0 +1,147 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package auth |
| 4 | + |
| 5 | +import ( |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + "unicode/utf8" |
| 10 | + |
| 11 | + usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc" |
| 12 | + "github.com/tenseleyFlow/shithub/internal/web/middleware" |
| 13 | +) |
| 14 | + |
| 15 | +// Field length caps. These mirror the CHECK constraints added in |
| 16 | +// migration 0014; the handler validates before the DB does so we surface |
| 17 | +// a friendly error instead of a 500 from a constraint violation. |
| 18 | +const ( |
| 19 | + maxDisplayName = 100 |
| 20 | + maxBio = 500 |
| 21 | + maxLocation = 80 |
| 22 | + maxWebsite = 200 |
| 23 | + maxCompany = 80 |
| 24 | + maxPronouns = 40 |
| 25 | +) |
| 26 | + |
| 27 | +// profileForm is the editor's form state. We round-trip on validation |
| 28 | +// errors so the user doesn't lose their input. |
| 29 | +type profileForm struct { |
| 30 | + DisplayName string |
| 31 | + Bio string |
| 32 | + Location string |
| 33 | + Website string |
| 34 | + Company string |
| 35 | + Pronouns string |
| 36 | +} |
| 37 | + |
| 38 | +// settingsProfileForm renders GET /settings/profile prefilled from the DB. |
| 39 | +func (h *Handlers) settingsProfileForm(w http.ResponseWriter, r *http.Request) { |
| 40 | + user := middleware.CurrentUserFromContext(r.Context()) |
| 41 | + row, err := h.q.GetUserByID(r.Context(), h.d.Pool, user.ID) |
| 42 | + if err != nil { |
| 43 | + h.d.Logger.ErrorContext(r.Context(), "settings/profile: load", "error", err) |
| 44 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 45 | + return |
| 46 | + } |
| 47 | + h.renderProfileForm(w, r, profileForm{ |
| 48 | + DisplayName: row.DisplayName, |
| 49 | + Bio: row.Bio, |
| 50 | + Location: row.Location, |
| 51 | + Website: row.Website, |
| 52 | + Company: row.Company, |
| 53 | + Pronouns: row.Pronouns, |
| 54 | + }, "", "") |
| 55 | +} |
| 56 | + |
| 57 | +// settingsProfileSubmit handles POST /settings/profile. |
| 58 | +func (h *Handlers) settingsProfileSubmit(w http.ResponseWriter, r *http.Request) { |
| 59 | + if err := r.ParseForm(); err != nil { |
| 60 | + h.d.Render.HTTPError(w, r, http.StatusBadRequest, "form parse") |
| 61 | + return |
| 62 | + } |
| 63 | + user := middleware.CurrentUserFromContext(r.Context()) |
| 64 | + form := profileForm{ |
| 65 | + DisplayName: strings.TrimSpace(r.PostFormValue("display_name")), |
| 66 | + Bio: strings.TrimRight(r.PostFormValue("bio"), " \t\r\n"), |
| 67 | + Location: strings.TrimSpace(r.PostFormValue("location")), |
| 68 | + Website: strings.TrimSpace(r.PostFormValue("website")), |
| 69 | + Company: strings.TrimSpace(r.PostFormValue("company")), |
| 70 | + Pronouns: strings.TrimSpace(r.PostFormValue("pronouns")), |
| 71 | + } |
| 72 | + if msg := validateProfile(&form); msg != "" { |
| 73 | + h.renderProfileForm(w, r, form, msg, "") |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + if err := h.q.UpdateUserProfile(r.Context(), h.d.Pool, usersdb.UpdateUserProfileParams{ |
| 78 | + ID: user.ID, |
| 79 | + DisplayName: form.DisplayName, |
| 80 | + Bio: form.Bio, |
| 81 | + Location: form.Location, |
| 82 | + Website: form.Website, |
| 83 | + Company: form.Company, |
| 84 | + Pronouns: form.Pronouns, |
| 85 | + }); err != nil { |
| 86 | + h.d.Logger.ErrorContext(r.Context(), "settings/profile: update", "error", err) |
| 87 | + h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "") |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + h.renderProfileForm(w, r, form, "", "Profile updated.") |
| 92 | +} |
| 93 | + |
| 94 | +// renderProfileForm is the shared render path. errMsg / successMsg are |
| 95 | +// mutually exclusive in practice but we let the template show whichever is set. |
| 96 | +func (h *Handlers) renderProfileForm(w http.ResponseWriter, r *http.Request, form profileForm, errMsg, successMsg string) { |
| 97 | + user := middleware.CurrentUserFromContext(r.Context()) |
| 98 | + h.renderPage(w, r, "settings/profile", map[string]any{ |
| 99 | + "Title": "Public profile", |
| 100 | + "CSRFToken": middleware.CSRFTokenForRequest(r), |
| 101 | + "SettingsActive": "profile", |
| 102 | + "Username": user.Username, |
| 103 | + "Form": form, |
| 104 | + "Error": errMsg, |
| 105 | + "Success": successMsg, |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +// validateProfile enforces length caps and (if present) URL shape on |
| 110 | +// the website. It returns a friendly message or "" on success. |
| 111 | +func validateProfile(f *profileForm) string { |
| 112 | + if utf8.RuneCountInString(f.DisplayName) > maxDisplayName { |
| 113 | + return "Display name is too long." |
| 114 | + } |
| 115 | + if utf8.RuneCountInString(f.Bio) > maxBio { |
| 116 | + return "Bio is too long (max 500 characters)." |
| 117 | + } |
| 118 | + if utf8.RuneCountInString(f.Location) > maxLocation { |
| 119 | + return "Location is too long." |
| 120 | + } |
| 121 | + if utf8.RuneCountInString(f.Website) > maxWebsite { |
| 122 | + return "Website URL is too long." |
| 123 | + } |
| 124 | + if utf8.RuneCountInString(f.Company) > maxCompany { |
| 125 | + return "Company is too long." |
| 126 | + } |
| 127 | + if utf8.RuneCountInString(f.Pronouns) > maxPronouns { |
| 128 | + return "Pronouns is too long." |
| 129 | + } |
| 130 | + if f.Website != "" { |
| 131 | + // Auto-prefix bare hosts so users can paste "example.com". |
| 132 | + if !strings.Contains(f.Website, "://") { |
| 133 | + f.Website = "https://" + f.Website |
| 134 | + } |
| 135 | + u, err := url.Parse(f.Website) |
| 136 | + if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" { |
| 137 | + return "Website must be an http(s) URL." |
| 138 | + } |
| 139 | + } |
| 140 | + if strings.ContainsAny(f.DisplayName, "\r\n") || |
| 141 | + strings.ContainsAny(f.Location, "\r\n") || |
| 142 | + strings.ContainsAny(f.Company, "\r\n") || |
| 143 | + strings.ContainsAny(f.Pronouns, "\r\n") { |
| 144 | + return "Single-line fields cannot contain newlines." |
| 145 | + } |
| 146 | + return "" |
| 147 | +} |