|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + |
| 7 | + "github.com/sarg3nt/gearbox/internal/framework/database" |
| 8 | + "github.com/sarg3nt/gearbox/internal/framework/templates/pages" |
| 9 | +) |
| 10 | + |
| 11 | +// WelcomePage renders the first-run onboarding screen. Issue #49. |
| 12 | +// |
| 13 | +// Shown only while nothing has been configured — no enabled boxes and no |
| 14 | +// enabled system gears. Once the admin makes any choice, the welcome screen |
| 15 | +// is no longer the landing fallback and direct visits redirect away. |
| 16 | +func (h *Handler) WelcomePage(w http.ResponseWriter, r *http.Request) { |
| 17 | + user, err := h.authManager.GetUser(r) |
| 18 | + if err != nil { |
| 19 | + http.Redirect(w, r, "/login", http.StatusSeeOther) |
| 20 | + return |
| 21 | + } |
| 22 | + if !user.IsAdmin() { |
| 23 | + http.Error(w, "Forbidden", http.StatusForbidden) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + if h.onboardingComplete() { |
| 28 | + http.Redirect(w, r, "/", http.StatusSeeOther) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + systemGears, err := h.db.GetGears(database.SystemServerID) |
| 33 | + if err != nil { |
| 34 | + h.logger.Warn("failed to load system gears for welcome page", "error", err) |
| 35 | + systemGears = nil |
| 36 | + } |
| 37 | + // Only offer gears the admin hasn't already enabled. Onboarding is |
| 38 | + // one-shot; if a gear is enabled the page wouldn't be rendered at all |
| 39 | + // (see onboardingComplete above), so this filter is mostly defensive. |
| 40 | + disabled := make([]database.Gear, 0, len(systemGears)) |
| 41 | + for _, g := range systemGears { |
| 42 | + if !g.Enabled { |
| 43 | + disabled = append(disabled, g) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + csrfToken, _ := h.authManager.GetCSRFToken(r) |
| 48 | + errorMessage := r.URL.Query().Get("error") |
| 49 | + |
| 50 | + component := pages.WelcomePage(user, disabled, csrfToken, errorMessage) |
| 51 | + if err := component.Render(r.Context(), w); err != nil { |
| 52 | + h.logger.Error("Failed to render welcome template", "error", err) |
| 53 | + http.Error(w, "Internal server error", http.StatusInternalServerError) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// OnboardingPost handles the welcome form. Two submit buttons share one form: |
| 58 | +// |
| 59 | +// - action=add-box → enable any checked system gears, then redirect to |
| 60 | +// /settings/boxes/new |
| 61 | +// - action=skip → enable any checked system gears, then redirect to / |
| 62 | +// and let RootRedirect's fallback chain pick the landing page |
| 63 | +// |
| 64 | +// If nothing is checked and the admin clicked "skip", the redirect lands back |
| 65 | +// on /welcome via RootRedirect (no decision made, no-op). |
| 66 | +func (h *Handler) OnboardingPost(w http.ResponseWriter, r *http.Request) { |
| 67 | + user, err := h.authManager.GetUser(r) |
| 68 | + if err != nil { |
| 69 | + http.Redirect(w, r, "/login", http.StatusSeeOther) |
| 70 | + return |
| 71 | + } |
| 72 | + if !user.IsAdmin() { |
| 73 | + http.Error(w, "Forbidden", http.StatusForbidden) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + if err := h.authManager.ValidateCSRFToken(r); err != nil { |
| 78 | + http.Redirect(w, r, "/welcome?error="+url.QueryEscape("Invalid CSRF token"), http.StatusSeeOther) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + if err := r.ParseForm(); err != nil { |
| 83 | + http.Redirect(w, r, "/welcome?error="+url.QueryEscape("Invalid form submission"), http.StatusSeeOther) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + action := r.FormValue("action") |
| 88 | + checked := r.Form["gears"] |
| 89 | + |
| 90 | + // Only enable gears that are actually system-scoped — defence in depth |
| 91 | + // against form tampering. |
| 92 | + for _, name := range checked { |
| 93 | + if !database.IsSystemGear(name) { |
| 94 | + h.logger.Warn("onboarding ignored non-system gear", "name", name, "user", user.ID) |
| 95 | + continue |
| 96 | + } |
| 97 | + if err := h.db.SetGearEnabled(database.SystemServerID, name, true, &user.ID); err != nil { |
| 98 | + h.logger.Error("failed to enable system gear during onboarding", |
| 99 | + "name", name, "error", err, "user", user.ID) |
| 100 | + http.Redirect(w, r, "/welcome?error="+url.QueryEscape("Failed to enable selected tools"), http.StatusSeeOther) |
| 101 | + return |
| 102 | + } |
| 103 | + h.logger.Info("onboarding enabled system gear", "name", name, "user", user.ID) |
| 104 | + } |
| 105 | + |
| 106 | + if action == "add-box" { |
| 107 | + http.Redirect(w, r, "/settings/boxes/new", http.StatusSeeOther) |
| 108 | + return |
| 109 | + } |
| 110 | + // action == "skip" (or unknown) — let RootRedirect's chain decide. |
| 111 | + http.Redirect(w, r, "/", http.StatusSeeOther) |
| 112 | +} |
| 113 | + |
| 114 | +// onboardingComplete reports whether the admin has finished onboarding — |
| 115 | +// at least one box is enabled, or at least one system-scoped gear is |
| 116 | +// enabled. Used as the one-shot guard for the welcome screen. |
| 117 | +func (h *Handler) onboardingComplete() bool { |
| 118 | + if count, err := h.db.CountEnabledBoxes(); err == nil && count > 0 { |
| 119 | + return true |
| 120 | + } |
| 121 | + if anyEnabled, err := h.anySystemGearEnabled(); err == nil && anyEnabled { |
| 122 | + return true |
| 123 | + } |
| 124 | + return false |
| 125 | +} |
| 126 | + |
| 127 | +// anySystemGearEnabled reports whether any box-agnostic gear is enabled on |
| 128 | +// the system row. |
| 129 | +func (h *Handler) anySystemGearEnabled() (bool, error) { |
| 130 | + gears, err := h.db.GetGears(database.SystemServerID) |
| 131 | + if err != nil { |
| 132 | + return false, err |
| 133 | + } |
| 134 | + for _, g := range gears { |
| 135 | + if g.Enabled { |
| 136 | + return true, nil |
| 137 | + } |
| 138 | + } |
| 139 | + return false, nil |
| 140 | +} |
0 commit comments