From 82f8c4c1cab236830c07f8cefb1b2d5798d2517b Mon Sep 17 00:00:00 2001 From: Tristan Storch Date: Wed, 3 Sep 2014 14:58:57 +0200 Subject: [PATCH] Robust User determination Use go's statndard lib to determine Username. This prevents hicups in minimal settings where $USER and $USERNAME might not be set. --- modules/setting/setting.go | 11 ++++++----- routers/install.go | 12 ++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index ebc1020a3..a5e49995e 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "os/exec" + "os/user" "path" "path/filepath" "strings" @@ -213,13 +214,13 @@ func NewConfigContext() { } RunUser = Cfg.MustValue("", "RUN_USER") - curUser := os.Getenv("USER") - if len(curUser) == 0 { - curUser = os.Getenv("USERNAME") + curUser, err := user.Current() + if err != nil { + log.Fatal(4, "Couldn't determine current User", err) } // Does not check run user when the install lock is off. - if InstallLock && RunUser != curUser { - log.Fatal(4, "Expect user(%s) but current user is: %s", RunUser, curUser) + if InstallLock && RunUser != curUser.Username { + log.Fatal(4, "Expect user(%s) but current user is: %s", RunUser, curUser.Username) } // Determine and create root git reposiroty path. diff --git a/routers/install.go b/routers/install.go index 3ac35f35c..0bd19fab2 100644 --- a/routers/install.go +++ b/routers/install.go @@ -8,6 +8,7 @@ import ( "errors" "os" "os/exec" + "os/user" "path" "strings" @@ -181,14 +182,13 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) { return } - // Check run user. - curUser := os.Getenv("USER") - if len(curUser) == 0 { - curUser = os.Getenv("USERNAME") + curUser, err := user.Current() + if err != nil { + log.Fatal(4, "Couldn't determine current User", err) } // Does not check run user when the install lock is off. - if form.RunUser != curUser { - ctx.RenderWithErr("Run user isn't the current user: "+form.RunUser+" -> "+curUser, INSTALL, &form) + if form.RunUser != curUser.Username { + ctx.RenderWithErr("Run user isn't the current user: "+form.RunUser+" -> "+curUser.Username, INSTALL, &form) return }