Browse Source

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.
pull/434/head
Tristan Storch 11 years ago
parent
commit
82f8c4c1ca
  1. 11
      modules/setting/setting.go
  2. 12
      routers/install.go

11
modules/setting/setting.go

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"os/user"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -213,13 +214,13 @@ func NewConfigContext() {
} }
RunUser = Cfg.MustValue("", "RUN_USER") RunUser = Cfg.MustValue("", "RUN_USER")
curUser := os.Getenv("USER") curUser, err := user.Current()
if len(curUser) == 0 { if err != nil {
curUser = os.Getenv("USERNAME") log.Fatal(4, "Couldn't determine current User", err)
} }
// Does not check run user when the install lock is off. // Does not check run user when the install lock is off.
if InstallLock && RunUser != curUser { if InstallLock && RunUser != curUser.Username {
log.Fatal(4, "Expect user(%s) but current user is: %s", RunUser, curUser) log.Fatal(4, "Expect user(%s) but current user is: %s", RunUser, curUser.Username)
} }
// Determine and create root git reposiroty path. // Determine and create root git reposiroty path.

12
routers/install.go

@ -8,6 +8,7 @@ import (
"errors" "errors"
"os" "os"
"os/exec" "os/exec"
"os/user"
"path" "path"
"strings" "strings"
@ -181,14 +182,13 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
return return
} }
// Check run user. curUser, err := user.Current()
curUser := os.Getenv("USER") if err != nil {
if len(curUser) == 0 { log.Fatal(4, "Couldn't determine current User", err)
curUser = os.Getenv("USERNAME")
} }
// Does not check run user when the install lock is off. // Does not check run user when the install lock is off.
if form.RunUser != curUser { if form.RunUser != curUser.Username {
ctx.RenderWithErr("Run user isn't the current user: "+form.RunUser+" -> "+curUser, INSTALL, &form) ctx.RenderWithErr("Run user isn't the current user: "+form.RunUser+" -> "+curUser.Username, INSTALL, &form)
return return
} }

Loading…
Cancel
Save