From ab93161867447a1af4c78a8e44b46eb89c1e7c10 Mon Sep 17 00:00:00 2001 From: surtur Date: Wed, 16 Aug 2023 15:07:10 +0200 Subject: [PATCH] go,tmpl: allow conditionally disabling the sign-up --- app/routes.go | 7 +++++-- app/settings/settings.go | 5 +++++ handlers/config.go | 16 +++++++++------- handlers/page.go | 5 +++++ templates/navbar.tmpl | 4 ++++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/app/routes.go b/app/routes.go index 035cfb2..2940397 100644 --- a/app/routes.go +++ b/app/routes.go @@ -56,8 +56,11 @@ func (a *App) SetupRoutes() error { base.HEAD("/", handlers.Index()) base.GET("/signin", handlers.Signin(), compress) base.POST("/signin", handlers.SigninPost(a.db)) - base.GET("/signup", handlers.Signup(), compress) - base.POST("/signup", handlers.SignupPost(a.db)) + + if a.setting.RegistrationAllowed { + base.GET("/signup", handlers.Signup(), compress) + base.POST("/signup", handlers.SignupPost(a.db)) + } base.GET("/home", handlers.Home(a.db)) diff --git a/app/settings/settings.go b/app/settings/settings.go index cc0c610..b9e7928 100644 --- a/app/settings/settings.go +++ b/app/settings/settings.go @@ -39,6 +39,7 @@ type Settings struct { dbConnstring string dbType string dbIsSetUp bool + RegistrationAllowed bool } const ( @@ -160,6 +161,10 @@ func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, dev } } + if conf.Registration.Allowed { + s.RegistrationAllowed = true + } + s.SetVersion(version) } diff --git a/handlers/config.go b/handlers/config.go index 30554d5..c45ba38 100644 --- a/handlers/config.go +++ b/handlers/config.go @@ -11,13 +11,14 @@ import ( ) var ( - setting *settings.Settings - appver string - appName = "pcmt" - appIsDevel bool - slogger *slogging.Slogger - log slogging.Slogger - dbclient *ent.Client + setting *settings.Settings + appver string + appName = "pcmt" + appIsDevel bool + slogger *slogging.Slogger + log slogging.Slogger + dbclient *ent.Client + registrationAllowed bool ) func SetDBClient(client *ent.Client) { @@ -36,4 +37,5 @@ func InitHandlers(s *settings.Settings) { appName = setting.AppName() appver = setting.Version() appIsDevel = setting.IsDevel() + registrationAllowed = setting.RegistrationAllowed } diff --git a/handlers/page.go b/handlers/page.go index f6d869c..bffca83 100644 --- a/handlers/page.go +++ b/handlers/page.go @@ -21,10 +21,15 @@ type page struct { } func newPage() *page { + data := make(map[string]any, 0) + + data["RegistrationAllowed"] = registrationAllowed + p := &page{ AppName: appName, AppVer: appver, DevelMode: appIsDevel, + Data: data, } return p diff --git a/templates/navbar.tmpl b/templates/navbar.tmpl index 93429b2..ef551de 100644 --- a/templates/navbar.tmpl +++ b/templates/navbar.tmpl @@ -46,6 +46,7 @@
  • About
  • + {{ if and .Data .Data.RegistrationAllowed }} {{ if or (not .User) (and .User (not .User.IsLoggedIn)) }}
  • {{ if pageIs .Current "signup" }} @@ -56,6 +57,9 @@ Register
  • + {{end}} + {{end}} + {{ if or (not .User) (and .User (not .User.IsLoggedIn)) }}
  • {{ if pageIs .Current "signin" }}