amuse.git

commit 7ff3327f9049ee41ecae0b2557cbd59757146c4b

Author: Adam <git@apiote.tk>

conditionally close registration

 front/html.go | 10 +++++++
 i18n/en-GB.toml | 6 ++++
 i18n/pl-PL.toml | 4 +++
 main.go | 17 +++++++++----
 router.go | 9 +++++++
 templates/signup_locked.html | 32 ++++++++++++++++++++++++++
 utils/config.go | 46 +++++++++++++++++++++++++++++++++++++


diff --git a/front/html.go b/front/html.go
index 6a97673d243210f588454ba3391f2ee228c738bc..cfd47b7b0e1eac54cb4d6f3163b8219f4ebbcc1c 100644
--- a/front/html.go
+++ b/front/html.go
@@ -154,7 +154,15 @@ 		Qr         template.URL
 	}{key.Secret(), sfaEnabled, username, template.URL(qr)}
 	data := RenderData{Data: secret}
 	data.State.Error = authError
-	return render(languages, data, "signup")
+
+	var template string
+	if utils.OpenRegistration {
+		template = "signup"
+	} else {
+		template = "signup_locked"
+	}
+
+	return render(languages, data, template)
 }
 
 func (r HtmlRenderer) RenderSignedup(languages []language.Tag, recoveryCodes []string) string {




diff --git a/i18n/en-GB.toml b/i18n/en-GB.toml
index a1f788e304daf1e4d851b82047acf4c0ba2db411..408a890f480e3e8ef156d536ddfeb1ff2ad7906c 100644
--- a/i18n/en-GB.toml
+++ b/i18n/en-GB.toml
@@ -218,6 +218,7 @@ enable_sfa = "Enable second factor authentication"
 use_totp_app = "Use Your favourite TOTP app"
 confirm_sfa = "Confirm second factor authentication"
 sign_up = "Sign up"
+registration_closed = "Registrations are closed on this instance."
 already_have_account = "Already have an account?"
 log_in = "Log in"
 
@@ -292,6 +293,11 @@ 422_character = "The Narrator"
 422_title = "Fight Club"
 #untranslatable
 422_name = "Unprocessable Entity"
+423_quote = "‘I can’t accept this.’"
+423_character = "Kristoff"
+423_title = "Frozen"
+#untranslatable
+423_name = "Locked"
 500_quote = "‘Houston, we may have a problem’"
 500_character = "Henry Brown"
 500_title = "Paddington"




diff --git a/i18n/pl-PL.toml b/i18n/pl-PL.toml
index 81e3224759b995a4777944a119e8b08252b8258b..acf01de75996567cfa3b6a5d7e2e9bf70472055d 100644
--- a/i18n/pl-PL.toml
+++ b/i18n/pl-PL.toml
@@ -206,6 +206,7 @@ enable_sfa = "Włącz dwuskładnikowe uwierzytelnianie"
 use_totp_app = "Użyj ulubionej aplikacji TOTP"
 confirm_sfa = "Potwierdź kod z aplikacji"
 sign_up = "Załóż konto"
+registration_closed = "Rejestracja jest zamknięta na tej instancji"
 already_have_account = "Masz już konto?"
 log_in = "Zaloguj się"
 
@@ -268,6 +269,9 @@ 410_title = "Piraci z Karaibów: Na krańcu świata"
 422_quote = "„Nie wiem. Nie rozumiem”"
 422_character = "Narrator"
 422_title = "Fight Club"
+423_quote = "„No nie mogę tego zaakceptować.”"
+423_character = "Kristoff"
+423_title = "Kraina lodu"
 500_quote = "„Houston, chyba mamy problem.”"
 500_character = "Henry Brown"
 500_title = "Paddington"




diff --git a/main.go b/main.go
index fbbf1b767e1eaaa9d7ab4bd3aa4ecc416810fcf6..698409be3efaef6f50c7e25962f7318e973e9347 100644
--- a/main.go
+++ b/main.go
@@ -1,9 +1,9 @@
 package main
 
 import (
+	"notabug.org/apiote/amuse/db"
 	"notabug.org/apiote/amuse/libamuse"
 	"notabug.org/apiote/amuse/utils"
-	"notabug.org/apiote/amuse/db"
 
 	"flag"
 	"fmt"
@@ -11,12 +11,19 @@ 	"os"
 )
 
 func main() {
-	port := flag.Int("p", 5008, "port to run amuse on")
-	dataHome := flag.String("d", "/usr/local/share/amuse", "data directory")
+	port := flag.Int("p", -1, "port to run amuse on")
+	dataHome := flag.String("d", "", "data directory")
 	manage := flag.String("m", "", "manage command")
+	configPath := flag.String("c", "/etc/amuse.toml", "configPath")
 	flag.Parse()
 
-	utils.DataHome = *dataHome
+	utils.ReadConfig(*configPath)
+	if *dataHome != "" {
+		utils.DataHome = *dataHome
+	}
+	if *port != -1 {
+		utils.Port = *port
+	}
 
 	db.Migrate()
 
@@ -33,5 +40,5 @@ 			return
 		}
 	}
 
-	route(*port)
+	route(utils.Port)
 }




diff --git a/router.go b/router.go
index 1b0d39dec9c4039d2f14d50614d94bf7e611190a..bd59ce9e2c6ee16ca55efb92150eeb3bdb8a8dfb 100644
--- a/router.go
+++ b/router.go
@@ -242,6 +242,13 @@ }
 
 func signupPost(w http.ResponseWriter, r *http.Request, acceptLanguages, mimetype string) {
 	// todo check mimetype (html,capnproto)
+
+	if !utils.OpenRegistration {
+		err := errors.New("423")
+		render("", err, w, acceptLanguages, mimetype)
+		return
+	}
+
 	r.ParseForm()
 	username := r.PostForm.Get("username")
 	password := r.PostForm.Get("password")
@@ -595,6 +602,8 @@ 				renderError(401, w, e, languages, mimetype)
 			} else {
 				renderError(403, w, e, languages, mimetype)
 			}
+		} else if e.Error() == "423" {
+			renderError(423, w, e, languages, mimetype)
 		} else {
 			renderError(500, w, e, languages, mimetype)
 		}




diff --git a/templates/signup_locked.html b/templates/signup_locked.html
new file mode 100644
index 0000000000000000000000000000000000000000..ac171a4380542913761289b29349866764084d92
--- /dev/null
+++ b/templates/signup_locked.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html>
+	<head>
+		<meta charset="UTF-8">
+		<meta name="viewport" content="width=device-width, initial-scale=1.0">
+		<title>{{.Strings.Signup.title}}</title>
+		<link rel="stylesheet" href="/static/style/style.css" />
+		<link rel="icon" type="image/svg+xml" href="/static/img/logo.svg">
+		<link rel="apple-touch-icon" type="image/svg+xml" href="/static/img/logo.svg">
+	</head>
+	<body class="flex flex-column height-all">
+		<header class="w12 padding-bottom-_25 flex flex-row flex-justify-space flex-align-centre flex-content">
+			<a href="/" class="decoration-none">
+				<h1 class="inline valign-mid text sans margin-lr-1">a·muse</h1>
+			</a>
+		</header>
+		<main class="margin-lr-1 flex-fill">
+			<div class="flex flex-column height-fill flex-centre">
+				<div class="w12 flex flex-centre border-box left">
+					<div>
+						<div class="sans italic centre">{{.Strings.Signup.swear}}</div>
+						<hr/>
+						<p class="clear-float">
+							{{.Strings.Signup.registration_closed}}
+						</p>
+						<p class="sans font-_875">{{.Strings.Signup.already_have_account}} <a href="/login">{{.Strings.Signup.log_in}}</a></p>
+					</div>
+				</div>
+			</div>
+		</main>
+	</body>
+</html>




diff --git a/utils/config.go b/utils/config.go
index 6f5d7ceae37476cbf60e54e294c7a6c3d204e79c..8b2e65d948ff7e6bc15697c5c34f20ecd8635c1a 100644
--- a/utils/config.go
+++ b/utils/config.go
@@ -1,3 +1,47 @@
 package utils
 
-var DataHome string
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strings"
+)
+
+var (
+	OpenRegistration = false
+	DataHome         = "/usr/local/amuse"
+	Port             = 5008
+)
+
+func ReadConfig(path string) error {
+	file, err := os.Open(path)
+	if err != nil {
+		if os.IsNotExist(err) {
+			return nil
+		} else {
+			fmt.Printf("error opening configuration %v\n", err)
+			return err
+		}
+	}
+	defer file.Close()
+
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		line := scanner.Text()
+		assignment := strings.Split(line, "=")
+		variable := strings.Trim(assignment[0], " ")
+		value := strings.Trim(assignment[1], " ")
+		switch variable {
+		case "OpenRegistration":
+			OpenRegistration = value == "true"
+		case "DataHome":
+			DataHome = value
+		}
+	}
+
+	if err := scanner.Err(); err != nil {
+		fmt.Printf("error reading configuration %v\n", err)
+		return err
+	}
+	return nil
+}