Browse Source

fix: 解决网站设置用户/组报错的问题 (#710)

pull/721/head v1.1.2
zhengkunwang223 2 years ago committed by GitHub
parent
commit
a0b820649e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      backend/app/service/website.go
  2. 11
      backend/utils/cmd/cmd.go

8
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

11
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
}

Loading…
Cancel
Save