package handlers import ( "fmt" "log" "net/http" ssstripe "git.nixc.us/a250/ss-atlas/internal/stripe" "git.nixc.us/a250/ss-atlas/internal/version" ) func (a *App) handleDashboard(w http.ResponseWriter, r *http.Request) { remoteUser := r.Header.Get("Remote-User") remoteEmail := r.Header.Get("Remote-Email") remoteGroups := r.Header.Get("Remote-Groups") isSubscribed := remoteGroups != "" && contains(remoteGroups, "customers") // Authelia session may be stale (user was added to customers after login). if !isSubscribed && remoteUser != "" { inGroup, _ := a.ldap.IsInGroup(remoteUser, "customers") if inGroup { isSubscribed = true } } var customerID string stackDeployed := false stackRunning := false var subStatus *ssstripe.SubscriptionStatus if isSubscribed && remoteUser != "" { cid, err := a.ldap.GetStripeCustomerID(remoteUser) if err != nil { log.Printf("dashboard: failed to get stripe customer id for %s: %v", remoteUser, err) } customerID = cid if cid != "" { subStatus = a.stripe.GetCustomerSubscriptionStatus(cid) } if subStatus == nil { subStatus = &ssstripe.SubscriptionStatus{Label: "Active", Badge: "badge-active"} } stackName := fmt.Sprintf("customer-%s", remoteUser) exists, err := a.swarm.StackExists(stackName) if err != nil { log.Printf("dashboard: stack check failed for %s: %v", remoteUser, err) } stackDeployed = exists if exists { replicas, _ := a.swarm.GetWebReplicas(stackName) stackRunning = replicas > 0 } } data := map[string]any{ "AppURL": a.cfg.AppURL, "AutheliaURL": a.cfg.AutheliaURL, "User": remoteUser, "Email": remoteEmail, "Groups": remoteGroups, "Domain": a.cfg.TraefikDomain, "IsSubscribed": isSubscribed, "CustomerID": customerID, "SubStatus": subStatus, "StackDeployed": stackDeployed, "StackRunning": stackRunning, "Commit": version.Commit, "BuildTime": version.BuildTime, } if err := a.tmpl.ExecuteTemplate(w, "dashboard.html", data); err != nil { log.Printf("template error: %v", err) http.Error(w, "internal error", http.StatusInternalServerError) } } func contains(s, substr string) bool { return len(s) >= len(substr) && searchString(s, substr) } func searchString(s, sub string) bool { for i := 0; i <= len(s)-len(sub); i++ { if s[i:i+len(sub)] == sub { return true } } return false }