From 6937b7b21e91b092ace26aeba8c17ca720f83160 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Thu, 13 Feb 2014 15:08:20 +0600 Subject: [PATCH 1/3] Open invitation setting Closes #64 --- cmd/droned/drone.go | 2 + pkg/database/schema/schema.go | 1 + pkg/database/schema/schema.sql | 1 + pkg/database/settings.go | 2 +- pkg/handler/admin.go | 10 ++++- pkg/handler/app.go | 26 ++++++++++++- pkg/model/settings.go | 2 + pkg/template/pages/admin_settings.html | 2 + pkg/template/pages/login.html | 3 ++ pkg/template/pages/signup.html | 51 ++++++++++++++++++++++++++ pkg/template/template.go | 1 + 11 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 pkg/template/pages/signup.html diff --git a/cmd/droned/drone.go b/cmd/droned/drone.go index 86b6ba13f..3fa3701ca 100644 --- a/cmd/droned/drone.go +++ b/cmd/droned/drone.go @@ -86,6 +86,8 @@ func setupHandlers() { m.Post("/forgot", handler.ErrorHandler(handler.ForgotPost)) m.Get("/reset", handler.ErrorHandler(handler.Reset)) m.Post("/reset", handler.ErrorHandler(handler.ResetPost)) + m.Get("/signup", handler.ErrorHandler(handler.SignUp)) + m.Post("/signup", handler.ErrorHandler(handler.SignUpPost)) m.Get("/register", handler.ErrorHandler(handler.Register)) m.Post("/register", handler.ErrorHandler(handler.RegisterPost)) m.Get("/accept", handler.UserHandler(handler.TeamMemberAccept)) diff --git a/pkg/database/schema/schema.go b/pkg/database/schema/schema.go index 993db07e2..a72415183 100644 --- a/pkg/database/schema/schema.go +++ b/pkg/database/schema/schema.go @@ -127,6 +127,7 @@ CREATE TABLE settings ( ,smtp_password VARCHAR(1024) ,hostname VARCHAR(1024) ,scheme VARCHAR(5) + ,open_invitations INTEGER ); ` diff --git a/pkg/database/schema/schema.sql b/pkg/database/schema/schema.sql index 950458508..39cda73a3 100644 --- a/pkg/database/schema/schema.sql +++ b/pkg/database/schema/schema.sql @@ -112,6 +112,7 @@ CREATE TABLE settings ( ,smtp_password VARCHAR(1024) ,hostname VARCHAR(1024) ,scheme VARCHAR(5) + ,open_invitations INTEGER ); CREATE UNIQUE INDEX member_uix ON members (team_id, user_id); diff --git a/pkg/database/settings.go b/pkg/database/settings.go index 0e210c991..188ac3a5e 100644 --- a/pkg/database/settings.go +++ b/pkg/database/settings.go @@ -11,7 +11,7 @@ const settingsTable = "settings" // SQL Queries to retrieve the system settings const settingsStmt = ` SELECT id, github_key, github_secret, bitbucket_key, bitbucket_secret, -smtp_server, smtp_port, smtp_address, smtp_username, smtp_password, hostname, scheme +smtp_server, smtp_port, smtp_address, smtp_username, smtp_password, hostname, scheme, open_invitations FROM settings WHERE id = 1 ` diff --git a/pkg/handler/admin.go b/pkg/handler/admin.go index 1d553d988..1d028f783 100644 --- a/pkg/handler/admin.go +++ b/pkg/handler/admin.go @@ -33,8 +33,7 @@ func AdminUserAdd(w http.ResponseWriter, r *http.Request, u *User) error { return RenderTemplate(w, "admin_users_add.html", &struct{ User *User }{u}) } -// Invite a user to join the system -func AdminUserInvite(w http.ResponseWriter, r *http.Request, u *User) error { +func UserInvite(w http.ResponseWriter, r *http.Request) error { // generate the password reset token email := r.FormValue("email") token := authcookie.New(email, time.Now().Add(12*time.Hour), secret) @@ -66,6 +65,11 @@ func AdminUserInvite(w http.ResponseWriter, r *http.Request, u *User) error { return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK) } +// Invite a user to join the system +func AdminUserInvite(w http.ResponseWriter, r *http.Request, u *User) error { + return UserInvite(w, r) +} + // Form to edit a user func AdminUserEdit(w http.ResponseWriter, r *http.Request, u *User) error { idstr := r.FormValue("id") @@ -176,6 +180,8 @@ func AdminSettingsUpdate(w http.ResponseWriter, r *http.Request, u *User) error settings.SmtpUsername = r.FormValue("SmtpUsername") settings.SmtpPassword = r.FormValue("SmtpPassword") + settings.OpenInvitations = (r.FormValue("OpenInvitations") == "on") + // persist changes if err := database.SaveSettings(settings); err != nil { return RenderError(w, err, http.StatusBadRequest) diff --git a/pkg/handler/app.go b/pkg/handler/app.go index 73df372b7..48858e90e 100644 --- a/pkg/handler/app.go +++ b/pkg/handler/app.go @@ -47,7 +47,13 @@ func Index(w http.ResponseWriter, r *http.Request) error { // Return an HTML form for the User to login. func Login(w http.ResponseWriter, r *http.Request) error { - return RenderTemplate(w, "login.html", nil) + var settings = database.SettingsMust() + + data := struct { + Settings *Settings + }{settings} + + return RenderTemplate(w, "login.html", &data) } // Terminate the User session. @@ -70,6 +76,15 @@ func Reset(w http.ResponseWriter, r *http.Request) error { return RenderTemplate(w, "reset.html", &struct{ Error string }{""}) } +// Return an HTML form for the User to signup. +func SignUp(w http.ResponseWriter, r *http.Request) error { + if !database.SettingsMust().OpenInvitations { + http.Redirect(w, r, "/login", http.StatusSeeOther) + return nil + } + return RenderTemplate(w, "signup.html", nil) +} + // Return an HTML form to register for a new account. This // page must be visited from a Signup email that contains // a hash to verify the Email address is correct. @@ -144,6 +159,15 @@ func ResetPost(w http.ResponseWriter, r *http.Request) error { return nil } +func SignUpPost(w http.ResponseWriter, r *http.Request) error { + if !database.SettingsMust().OpenInvitations { + http.Redirect(w, r, "/login", http.StatusSeeOther) + return nil + } + + return UserInvite(w, r) +} + func RegisterPost(w http.ResponseWriter, r *http.Request) error { // verify the token and extract the username token := r.FormValue("token") diff --git a/pkg/model/settings.go b/pkg/model/settings.go index ec7f69158..04db729f8 100644 --- a/pkg/model/settings.go +++ b/pkg/model/settings.go @@ -27,6 +27,8 @@ type Settings struct { // Scheme of the server, eg https Scheme string `meddler:"scheme"` + + OpenInvitations bool `meddler:"open_invitations"` } func (s *Settings) URL() *url.URL { diff --git a/pkg/template/pages/admin_settings.html b/pkg/template/pages/admin_settings.html index 76097d610..3e44de4a8 100644 --- a/pkg/template/pages/admin_settings.html +++ b/pkg/template/pages/admin_settings.html @@ -33,6 +33,8 @@ {{ end }} + + enable open invintation requests from users
GitHub OAuth Consumer Key and Secret
diff --git a/pkg/template/pages/login.html b/pkg/template/pages/login.html index 99b0ee5f1..49d668b28 100644 --- a/pkg/template/pages/login.html +++ b/pkg/template/pages/login.html @@ -10,6 +10,9 @@
+ {{ if .Settings.OpenInvitations }} + request invitation  + {{ end }} forgot password
{{ end }} diff --git a/pkg/template/pages/signup.html b/pkg/template/pages/signup.html new file mode 100644 index 000000000..ef9606cd4 --- /dev/null +++ b/pkg/template/pages/signup.html @@ -0,0 +1,51 @@ +{{ define "title" }}Sign up · drone.io{{ end }} + +{{ define "content" }} +

Sign up

+
+
+ +
+
+
+
+
+
+ +
+
+{{ end }} + +{{ define "script" }} + +{{ end }} \ No newline at end of file diff --git a/pkg/template/template.go b/pkg/template/template.go index 76117728b..d5ff5d4ca 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -45,6 +45,7 @@ func init() { "forgot.html", "forgot_sent.html", "reset.html", + "signup.html", "register.html", "install.html", From 0bd8ef28dd7cda04c32591376079e80daaa7ef54 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Thu, 13 Feb 2014 19:43:30 +0600 Subject: [PATCH 2/3] Fixup signup and settings template --- pkg/template/pages/admin_settings.html | 5 +++-- pkg/template/pages/signup.html | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/template/pages/admin_settings.html b/pkg/template/pages/admin_settings.html index 3e44de4a8..bc607fa5e 100644 --- a/pkg/template/pages/admin_settings.html +++ b/pkg/template/pages/admin_settings.html @@ -33,8 +33,9 @@ {{ end }} - - enable open invintation requests from users +
GitHub OAuth Consumer Key and Secret
diff --git a/pkg/template/pages/signup.html b/pkg/template/pages/signup.html index ef9606cd4..bb4538262 100644 --- a/pkg/template/pages/signup.html +++ b/pkg/template/pages/signup.html @@ -4,7 +4,7 @@

Sign up

- +
@@ -32,7 +32,7 @@ if (this.status == 200) { var msg = "User Invitation was sent successfully"; if (this.responseText != "OK") { - msg = "Email is not currently enabled. In order to invite the user, you'll need to provide them the following link:
" + this.responseText + ""; + msg = "Email is not currently enables. Follow the link:
" + this.responseText + ""; } $("#successAlert").html(msg); $("#successAlert").show().removeClass("hide"); From 460ec90049edcc9a2f924fe48109ca4897b103f2 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sat, 15 Feb 2014 13:49:38 +0600 Subject: [PATCH 3/3] Fixup behavior on uninstalled state --- pkg/handler/app.go | 7 +++++-- pkg/template/pages/login.html | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/handler/app.go b/pkg/handler/app.go index 48858e90e..b0bfb1d18 100644 --- a/pkg/handler/app.go +++ b/pkg/handler/app.go @@ -47,7 +47,7 @@ func Index(w http.ResponseWriter, r *http.Request) error { // Return an HTML form for the User to login. func Login(w http.ResponseWriter, r *http.Request) error { - var settings = database.SettingsMust() + var settings, _ = database.GetSettings() data := struct { Settings *Settings @@ -78,10 +78,13 @@ func Reset(w http.ResponseWriter, r *http.Request) error { // Return an HTML form for the User to signup. func SignUp(w http.ResponseWriter, r *http.Request) error { - if !database.SettingsMust().OpenInvitations { + var settings, _ = database.GetSettings() + + if settings == nil || !settings.OpenInvitations { http.Redirect(w, r, "/login", http.StatusSeeOther) return nil } + return RenderTemplate(w, "signup.html", nil) } diff --git a/pkg/template/pages/login.html b/pkg/template/pages/login.html index 49d668b28..8c383b044 100644 --- a/pkg/template/pages/login.html +++ b/pkg/template/pages/login.html @@ -10,8 +10,8 @@
- {{ if .Settings.OpenInvitations }} - request invitation  + {{ if .Settings ne nil and .Settings.OpenInvitations }} + request invitation ·  {{ end }} forgot password