From 0702a2b0cb57c9f85ce9249d53ee0169a155ac03 Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Mon, 23 Feb 2026 17:59:13 +0100 Subject: [PATCH] Add a configuration to disable signing up --- internal/config/config.go | 30 +++++++++++++++-------------- internal/myvalidator/myvalidator.go | 2 +- internal/routes/usersignup.go | 11 +++++++++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 393cc2a..d73dc10 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,24 +11,26 @@ import ( ) type Config struct { - Port string `toml:"port" default:"8080" comment:"The port to listen on for the server."` - DatabaseFilePath string `toml:"database-file-path" default:"plm.db" comment:"Path to sqlite database file."` - DemoDataPath string `toml:"demo-data-path" comment:"The path to the sql file to load for demo data."` - JWTKey string `toml:"jwt-key" comment:"The key used to encrypt the generated JWT. Encoded in base64. If empty a random one will be generated on every restart."` - ImageFolderPath string `toml:"image-folder-path" default:"img" comment:"Folder where uploaded files will be stored."` - Limit int `toml:"limit" default:"100" comment:"A single API call will return at most this number of records."` - InventaireUrl string `toml:"inventaire-url" default:"https://inventaire.io" comment:"An inventaire.io instance URL."` + Port string `toml:"port" default:"8080" comment:"The port to listen on for the server."` + DatabaseFilePath string `toml:"database-file-path" default:"plm.db" comment:"Path to sqlite database file."` + DemoDataPath string `toml:"demo-data-path" comment:"The path to the sql file to load for demo data."` + JWTKey string `toml:"jwt-key" comment:"The key used to encrypt the generated JWT. Encoded in base64. If empty a random one will be generated on every restart."` + ImageFolderPath string `toml:"image-folder-path" default:"img" comment:"Folder where uploaded files will be stored."` + Limit int `toml:"limit" default:"100" comment:"A single API call will return at most this number of records."` + InventaireUrl string `toml:"inventaire-url" default:"https://inventaire.io" comment:"An inventaire.io instance URL."` + DisableRegistration bool `toml:"disable-registration" default:"false" comment:"Disable new account creation."` } func defaultConfig() Config { return Config{ - Port: "8080", - DatabaseFilePath: "plm.db", - DemoDataPath: "", - JWTKey: "", - ImageFolderPath: "img", - Limit: 100, - InventaireUrl: "https://inventaire.io", + Port: "8080", + DatabaseFilePath: "plm.db", + DemoDataPath: "", + JWTKey: "", + ImageFolderPath: "img", + Limit: 100, + InventaireUrl: "https://inventaire.io", + DisableRegistration: false, } } diff --git a/internal/myvalidator/myvalidator.go b/internal/myvalidator/myvalidator.go index fcd1879..e3031df 100644 --- a/internal/myvalidator/myvalidator.go +++ b/internal/myvalidator/myvalidator.go @@ -46,7 +46,7 @@ func ReturnErrorsAsJsonResponse(ac *appcontext.AppContext, err error) { if errors.As(err, &ve) { ac.C.JSON(http.StatusBadRequest, getValidationErrors(ac, &ve)) } else if errors.As(err, &httpError) { - ac.C.JSON(httpError.StatusCode, gin.H{"error": httpError.Err}) + ac.C.JSON(httpError.StatusCode, gin.H{"error": httpError.Err.Error()}) } else { ac.C.JSON(http.StatusInternalServerError, gin.H{"error": err}) } diff --git a/internal/routes/usersignup.go b/internal/routes/usersignup.go index d5fb115..1cf05ab 100644 --- a/internal/routes/usersignup.go +++ b/internal/routes/usersignup.go @@ -1,6 +1,9 @@ package routes import ( + "errors" + "net/http" + "git.artlef.fr/PersonalLibraryManager/internal/appcontext" "git.artlef.fr/PersonalLibraryManager/internal/dto" "git.artlef.fr/PersonalLibraryManager/internal/model" @@ -9,6 +12,14 @@ import ( ) func PostSignupHandler(ac appcontext.AppContext) { + if ac.Config.DisableRegistration { + myvalidator.ReturnErrorsAsJsonResponse(&ac, + myvalidator.HttpError{ + StatusCode: http.StatusForbidden, + Err: errors.New("Registration has been disabled on this instance."), + }) + return + } var user dto.UserSignup err := ac.C.ShouldBindJSON(&user) if err != nil {