Skip to content

Commit 58621d4

Browse files
committed
PUT /api/permission add recursive and owner check
1 parent df50581 commit 58621d4

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

pkg/fileutils/file.go

+16
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ func CommonPrefix(sep byte, paths ...string) string {
206206
return string(c)
207207
}
208208

209+
func ChownRecursive(fs afero.Fs, path string, uid, gid int) error {
210+
return filepath.Walk(path, func(name string, info os.FileInfo, err error) error {
211+
if err != nil {
212+
return err
213+
}
214+
215+
if err := Chown(fs, name, uid, gid); err != nil {
216+
fmt.Printf("Failed to chown %s: %v\n", name, err)
217+
return err
218+
}
219+
220+
fmt.Printf("Chowned %s\n", name)
221+
return nil
222+
})
223+
}
224+
209225
func Chown(fs afero.Fs, path string, uid, gid int) error {
210226
start := time.Now()
211227
klog.Infoln("Function Chown starts at", start)

pkg/http/data.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,17 @@ func handle(fn handleFunc, prefix string, server *settings.Server) http.Handler
4646
return stripPrefix(prefix, handler)
4747
}
4848

49+
func NeedCheckPrefix(prefix string) bool {
50+
switch prefix {
51+
case "/api/resources", "/api/raw", "/api/preview", "/api/paste", "/api/permission", "/api/share":
52+
return true
53+
default:
54+
return false
55+
}
56+
}
57+
4958
func CheckPathOwner(r *http.Request, prefix string) (bool, error) {
50-
if prefix != "/api/resources" && prefix != "/api/raw" && prefix != "/api/preview" && prefix != "/api/paste" {
59+
if NeedCheckPrefix(prefix) {
5160
return true, nil
5261
}
5362

pkg/http/permission.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ func permissionPutHandler(w http.ResponseWriter, r *http.Request, d *common.Data
4646
}
4747
gid := uid
4848

49-
err = fileutils.Chown(files.DefaultFs, r.URL.Path, uid, gid)
49+
recursiveStr := r.URL.Query().Get("recursive")
50+
recursive := 0
51+
if recursiveStr != "" {
52+
recursive, err = strconv.Atoi(recursiveStr)
53+
if err != nil {
54+
return http.StatusBadRequest, err
55+
}
56+
}
57+
58+
if recursive == 0 {
59+
err = fileutils.Chown(files.DefaultFs, r.URL.Path, uid, gid)
60+
} else {
61+
err = fileutils.ChownRecursive(files.DefaultFs, r.URL.Path, uid, gid)
62+
}
5063
if err != nil {
5164
return http.StatusInternalServerError, err
5265
}

0 commit comments

Comments
 (0)