Add domain to form Also fix a bug where you cant update email (oops)
alex wennerberg alex@alexwennerberg.com
Sat, 09 Jan 2021 14:41:07 -0800
3 files changed,
27 insertions(+),
15 deletions(-)
M
db.go
→
db.go
@@ -71,12 +71,14 @@
} type User struct { - Username string - Email string - Active bool - Admin bool - CreatedAt int // timestamp - Reference string + Username string + Email string + Active bool + Admin bool + CreatedAt int // timestamp + Reference string + Domain string + DomainEnabled bool } func getActiveUserNames() ([]string, error) {@@ -99,8 +101,8 @@ }
func getUserByName(username string) (*User, error) { var user User - row := DB.QueryRow(`SELECT username, email, active, admin, created_at, reference from user WHERE username = ?`, username) - err := row.Scan(&user.Username, &user.Email, &user.Active, &user.Admin, &user.CreatedAt, &user.Reference) + row := DB.QueryRow(`SELECT username, email, active, admin, created_at, reference, domain, domain_enabled from user WHERE username = ?`, username) + err := row.Scan(&user.Username, &user.Email, &user.Active, &user.Admin, &user.CreatedAt, &user.Reference, &user.Domain, &user.DomainEnabled) if err != nil { return nil, err }@@ -180,7 +182,7 @@ reference TEXT NOT NULL default "",
active boolean NOT NULL DEFAULT false, admin boolean NOT NULL DEFAULT false, created_at INTEGER DEFAULT (strftime('%s', 'now')), - domain TEXT, + domain TEXT NOT NULL default "", domain_enabled BOOLEAN NOT NULL DEFAULT false );
M
http.go
→
http.go
@@ -283,10 +283,10 @@ me, _ := getUserByName(user.Username)
type pageData struct { Config Config AuthUser AuthUser - Email string + MyUser *User Errors []string } - data := pageData{c, user, me.Email, nil} + data := pageData{c, user, me, nil} if r.Method == "GET" { err := t.ExecuteTemplate(w, "me.html", data)@@ -298,10 +298,19 @@ r.ParseForm()
newUsername := r.Form.Get("username") errors := []string{} newEmail := r.Form.Get("email") + newDomain := r.Form.Get("domain") newUsername = strings.ToLower(newUsername) var err error + if newDomain != me.Domain { + _, err = DB.Exec("update user set domain = ? where username = ?", newDomain, me.Username) // TODO use transaction + if err != nil { + errors = append(errors, err.Error()) + } else { + log.Printf("Changed domain for %s from %s to %s", authUser, me.Domain, newDomain) + } + } if newEmail != me.Email { - _, err = DB.Exec("update user set email = ? where username = ?", newEmail, me.Email) + _, err = DB.Exec("update user set email = ? where username = ?", newEmail, me.Username) if err != nil { // TODO better error not sql errors = append(errors, err.Error())@@ -325,7 +334,8 @@ // reset auth
user = newGetAuthUser(r) data.Errors = errors data.AuthUser = user - data.Email = newEmail + data.MyUser.Email = newEmail + data.MyUser.Domain = newDomain _ = t.ExecuteTemplate(w, "me.html", data) } }
M
templates/me.html
→
templates/me.html
@@ -16,13 +16,13 @@ />
</div> <div> <label for="email">Email</label><br> - <input id="email" name="email" size="32" type="text" value="{{.Email}}" /> + <input id="email" name="email" size="32" type="text" value="{{.MyUser.Email}}" /> </div> <div> <details> <summary><label for="domain">Custom domain</label></summary> <em>For more information on setting up a custom domain, see <a href="https://admin.flounder.online/custom-domains.gmi">Custom Domains</a></em> - <input id="domain" name="domain" size="32" type="text" /> enabled/disabled + <input id="domain" name="domain" size="32" type="text" value="{{.MyUser.Domain}}"/> {{if ne .MyUser.Domain ""}}{{if .MyUser.DomainEnabled}}✅{{else}} ❌{{end}}{{end}} </details> </div> <div class="error">{{ range .Errors}}{{.}}<br>{{end}} </div>