From 3cf5bc8b7603b9958f31d173e4e268ef607a23fb Mon Sep 17 00:00:00 2001 From: Thomas Miceli Date: Thu, 4 May 2023 11:48:26 +0200 Subject: [PATCH 1/2] First account registering with OAuth is now admin --- internal/web/auth.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/web/auth.go b/internal/web/auth.go index 5e07826..9bc8d58 100644 --- a/internal/web/auth.go +++ b/internal/web/auth.go @@ -178,6 +178,12 @@ func oauthCallback(ctx echo.Context) error { return errorRes(500, "Cannot create user", err) } + if userDB.ID == 1 { + if err = userDB.SetAdmin(); err != nil { + return errorRes(500, "Cannot set user admin", err) + } + } + var resp *http.Response switch user.Provider { case "github": From 49807d04c776ee23d4f92e7c21f40dc7e92ee1eb Mon Sep 17 00:00:00 2001 From: Thomas Miceli Date: Sat, 6 May 2023 18:53:59 +0200 Subject: [PATCH 2/2] Disable login form via admin panel --- internal/web/auth.go | 10 ++++++++++ public/admin.ts | 1 + templates/pages/admin_index.html | 6 +++++- templates/pages/auth_form.html | 16 ++++++++++------ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/web/auth.go b/internal/web/auth.go index 9bc8d58..9861d8c 100644 --- a/internal/web/auth.go +++ b/internal/web/auth.go @@ -26,6 +26,7 @@ var title = cases.Title(language.English) func register(ctx echo.Context) error { setData(ctx, "title", "New account") setData(ctx, "htmlTitle", "New account") + setData(ctx, "disableForm", getData(ctx, "DisableLoginForm")) return html(ctx, "auth_form.html") } @@ -34,6 +35,10 @@ func processRegister(ctx echo.Context) error { return errorRes(403, "Signing up is disabled", nil) } + if getData(ctx, "DisableLoginForm") == true { + return errorRes(403, "Signing up via registration form is disabled", nil) + } + setData(ctx, "title", "New account") setData(ctx, "htmlTitle", "New account") @@ -81,10 +86,15 @@ func processRegister(ctx echo.Context) error { func login(ctx echo.Context) error { setData(ctx, "title", "Login") setData(ctx, "htmlTitle", "Login") + setData(ctx, "disableForm", getData(ctx, "DisableLoginForm")) return html(ctx, "auth_form.html") } func processLogin(ctx echo.Context) error { + if getData(ctx, "DisableLoginForm") == true { + return errorRes(403, "Logging in via login form is disabled", nil) + } + var err error sess := getSession(ctx) diff --git a/public/admin.ts b/public/admin.ts index d980530..4dffe80 100644 --- a/public/admin.ts +++ b/public/admin.ts @@ -1,6 +1,7 @@ document.addEventListener('DOMContentLoaded', () => { registerDomSetting(document.getElementById('disable-signup') as HTMLInputElement); registerDomSetting(document.getElementById('require-login') as HTMLInputElement); + registerDomSetting(document.getElementById('disable-login-form') as HTMLInputElement); }); const setSetting = (key: string, value: string) => { diff --git a/templates/pages/admin_index.html b/templates/pages/admin_index.html index c8fe35c..d59e0d9 100644 --- a/templates/pages/admin_index.html +++ b/templates/pages/admin_index.html @@ -84,9 +84,13 @@
- +
+
+ + +
diff --git a/templates/pages/auth_form.html b/templates/pages/auth_form.html index 6b83e25..d26db93 100644 --- a/templates/pages/auth_form.html +++ b/templates/pages/auth_form.html @@ -14,6 +14,8 @@
+ + {{ if not .disableForm }}
@@ -48,14 +50,16 @@ {{ end }} {{ .csrfHtml }} - + {{ end }} {{ if or .githubOauth .giteaOauth }} -
- -
+
+ {{ end }}
{{ if .githubOauth }}