1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 00:09:16 +08:00

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

This commit is contained in:
zhengkunwang223 2023-04-19 11:17:00 +08:00 committed by GitHub
parent 6cee4bfe7c
commit a0b820649e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -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

View File

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