From a0b820649eafa69f771b049e3068826551c54c2c Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 19 Apr 2023 11:17:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E7=BD=91=E7=AB=99?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=94=A8=E6=88=B7/=E7=BB=84=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=20(#710)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/website.go | 8 +++++++- backend/utils/cmd/cmd.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 807063731..863247781 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -1092,7 +1092,13 @@ func (w WebsiteService) UpdateSitePermission(req request.WebsiteUpdateDirPermiss absoluteIndexPath = path.Join(absoluteIndexPath, website.SiteDir) } chownCmd := fmt.Sprintf("chown -R %s:%s %s", req.User, req.Group, absoluteIndexPath) - if _, err := cmd.ExecWithTimeOut(chownCmd, 1*time.Second); err != nil { + if cmd.HasNoPasswordSudo() { + chownCmd = fmt.Sprintf("sudo %s", chownCmd) + } + if out, err := cmd.ExecWithTimeOut(chownCmd, 1*time.Second); err != nil { + if out != "" { + return errors.New(out) + } return err } website.User = req.User diff --git a/backend/utils/cmd/cmd.go b/backend/utils/cmd/cmd.go index 6ece0cb49..bbe7f92c5 100644 --- a/backend/utils/cmd/cmd.go +++ b/backend/utils/cmd/cmd.go @@ -88,3 +88,14 @@ func Execf(cmdStr string, a ...interface{}) (string, error) { } return stdout.String(), nil } + +func HasNoPasswordSudo() bool { + cmd := exec.Command("sudo", "-v") + err := cmd.Run() + if err != nil { + return false + } + cmd2 := exec.Command("sudo", "-n", "ls") + err2 := cmd2.Run() + return err2 == nil +}