From 4b412d3a26aae9c8395937b30d5ea9cb0323b995 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 15 Feb 2014 18:56:48 -0700 Subject: [PATCH] added code to prevent panic if /login is reached but no settings exist minor styling. added success and fail message to top of /signup screen modified open_registration to boolean value in database --- pkg/database/schema/sample.sql | 2 +- pkg/database/schema/schema.go | 6 +++++- pkg/database/schema/schema.sql | 2 +- pkg/handler/admin.go | 7 +------ pkg/handler/app.go | 23 +++++++++++++++++++++-- pkg/template/pages/login.html | 7 ++++--- pkg/template/pages/signup.html | 16 +++++----------- 7 files changed, 38 insertions(+), 25 deletions(-) diff --git a/pkg/database/schema/sample.sql b/pkg/database/schema/sample.sql index 6b3f6f297..594caaa3e 100644 --- a/pkg/database/schema/sample.sql +++ b/pkg/database/schema/sample.sql @@ -49,7 +49,7 @@ insert into builds values (9, 3, 'node_0.80', 'Success', '2013-09-16 00:00:00',' -- insert default, dummy settings -insert into settings values (1,'','','','','','','','','','localhost:8080','http'); +insert into settings values (1,'','','','','','','','','','localhost:8080','http', 0); -- add public & private keys to all repositories diff --git a/pkg/database/schema/schema.go b/pkg/database/schema/schema.go index a72415183..ec93774a8 100644 --- a/pkg/database/schema/schema.go +++ b/pkg/database/schema/schema.go @@ -127,7 +127,7 @@ CREATE TABLE settings ( ,smtp_password VARCHAR(1024) ,hostname VARCHAR(1024) ,scheme VARCHAR(5) - ,open_invitations INTEGER + ,open_invitations BOOLEAN ); ` @@ -195,5 +195,9 @@ func Load(db *sql.DB) error { db.Exec(buildCommitIndex) db.Exec(buildSlugIndex) + // migrations for backward compatibility + db.Exec("ALTER TABLE settings ADD COLUMN open_invitations BOOLEAN") + db.Exec("UPDATE settings SET open_invitations=0 WHERE open_invitations IS NULL") + return nil } diff --git a/pkg/database/schema/schema.sql b/pkg/database/schema/schema.sql index 39cda73a3..d0babf327 100644 --- a/pkg/database/schema/schema.sql +++ b/pkg/database/schema/schema.sql @@ -112,7 +112,7 @@ CREATE TABLE settings ( ,smtp_password VARCHAR(1024) ,hostname VARCHAR(1024) ,scheme VARCHAR(5) - ,open_invitations INTEGER + ,open_invitations BOOLEAN ); CREATE UNIQUE INDEX member_uix ON members (team_id, user_id); diff --git a/pkg/handler/admin.go b/pkg/handler/admin.go index 1d028f783..3d7fa8b5a 100644 --- a/pkg/handler/admin.go +++ b/pkg/handler/admin.go @@ -2,7 +2,6 @@ package handler import ( "fmt" - "log" "net/http" "strconv" "time" @@ -56,11 +55,7 @@ func UserInvite(w http.ResponseWriter, r *http.Request) error { }{hostname, email, token} // send the email message async - go func() { - if err := mail.SendActivation(email, data); err != nil { - log.Printf("error sending account activation email to %s. %s", email, err) - } - }() + go mail.SendActivation(email, data) return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK) } diff --git a/pkg/handler/app.go b/pkg/handler/app.go index b0bfb1d18..9d720113c 100644 --- a/pkg/handler/app.go +++ b/pkg/handler/app.go @@ -163,12 +163,31 @@ func ResetPost(w http.ResponseWriter, r *http.Request) error { } func SignUpPost(w http.ResponseWriter, r *http.Request) error { + // if self-registration is disabled we should display an + // error message to the user. if !database.SettingsMust().OpenInvitations { - http.Redirect(w, r, "/login", http.StatusSeeOther) + http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) return nil } - return UserInvite(w, r) + // generate the password reset token + email := r.FormValue("email") + token := authcookie.New(email, time.Now().Add(12*time.Hour), secret) + + // get the hostname from the database for use in the email + hostname := database.SettingsMust().URL().String() + + // data used to generate the email template + data := struct { + Host string + Email string + Token string + }{hostname, email, token} + + // send the email message async + go mail.SendActivation(email, data) + + return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK) } func RegisterPost(w http.ResponseWriter, r *http.Request) error { diff --git a/pkg/template/pages/login.html b/pkg/template/pages/login.html index 8c383b044..4967f119c 100644 --- a/pkg/template/pages/login.html +++ b/pkg/template/pages/login.html @@ -10,10 +10,11 @@
- {{ if .Settings ne nil and .Settings.OpenInvitations }} - request invitation ยท  - {{ end }} + {{ if .Settings }} + request invitation | forgot password + {{ else }} forgot password + {{ end }}
{{ end }} diff --git a/pkg/template/pages/signup.html b/pkg/template/pages/signup.html index bb4538262..26b1efc2e 100644 --- a/pkg/template/pages/signup.html +++ b/pkg/template/pages/signup.html @@ -3,15 +3,13 @@ {{ define "content" }}

Sign up

+
+
-
-
-
-
- +
{{ end }} @@ -30,11 +28,7 @@ xhr.open('POST', form.action); xhr.onload = function() { if (this.status == 200) { - var msg = "User Invitation was sent successfully"; - if (this.responseText != "OK") { - msg = "Email is not currently enables. Follow the link:
" + this.responseText + ""; - } - $("#successAlert").html(msg); + $("#successAlert").text("User Invitation was sent successfully"); $("#successAlert").show().removeClass("hide"); $('#submitButton').button('reset') @@ -46,6 +40,6 @@ }; xhr.send(formData); return false; - } + } {{ end }} \ No newline at end of file