diff --git a/cmd/droned/drone.go b/cmd/droned/drone.go index 86b6ba13..3fa3701c 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 993db07e..a7241518 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 95045850..39cda73a 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 0e210c99..188ac3a5 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 1d553d98..1d028f78 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 73df372b..b0bfb1d1 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.GetSettings() + + data := struct { + Settings *Settings + }{settings} + + return RenderTemplate(w, "login.html", &data) } // Terminate the User session. @@ -70,6 +76,18 @@ 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 { + var settings, _ = database.GetSettings() + + if settings == nil || !settings.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 +162,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 ec7f6915..04db729f 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 76097d61..bc607fa5 100644 --- a/pkg/template/pages/admin_settings.html +++ b/pkg/template/pages/admin_settings.html @@ -33,6 +33,9 @@ {{ end }} +
GitHub OAuth Consumer Key and Secret
diff --git a/pkg/template/pages/login.html b/pkg/template/pages/login.html index 99b0ee5f..8c383b04 100644 --- a/pkg/template/pages/login.html +++ b/pkg/template/pages/login.html @@ -10,6 +10,9 @@
+ {{ if .Settings ne nil and .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 00000000..bb453826 --- /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 76117728..d5ff5d4c 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",