From f416775268085c7fb7f90cf54ea2149267b960e6 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Wed, 25 Jan 2023 01:23:58 +0100 Subject: Add user impersonation support for admins --- imgs.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'imgs.go') diff --git a/imgs.go b/imgs.go index 78705ca..4ee448b 100644 --- a/imgs.go +++ b/imgs.go @@ -17,6 +17,13 @@ const pageSize = 5 func imgs(w http.ResponseWriter, r *http.Request) { u, err := checkSession(w, r) if u != nil && err == nil { + username := u.Username + if u.IsAdmin { + name := r.URL.Query().Get("user") + if name != "" { + username = name + } + } fragment := r.URL.Query().Has("fragment") pageQuery := r.URL.Query().Get("page") if pageQuery == "" { @@ -27,7 +34,7 @@ func imgs(w http.ResponseWriter, r *http.Request) { sendError(w, r, err.Error(), http.StatusInternalServerError) return } - userDocPath := filepath.Join(baseDocDir, u.Username) + userDocPath := filepath.Join(baseDocDir, username) err = os.Mkdir(userDocPath, 0750) if err != nil && !os.IsExist(err) { sendError(w, r, err.Error(), http.StatusInternalServerError) @@ -67,6 +74,7 @@ func imgs(w http.ResponseWriter, r *http.Request) { start = len(imgs) } flasherr := consumeFlash(w, r, "error") + userImpersonation := r.URL.Query().Get("user") data := struct { Imgs []Doc Start int @@ -74,6 +82,7 @@ func imgs(w http.ResponseWriter, r *http.Request) { NbFiles int Page int Error string + UserImpersonation string }{ imgs[start:end], start + 1, @@ -81,6 +90,7 @@ func imgs(w http.ResponseWriter, r *http.Request) { len(imgs), page, flasherr, + userImpersonation, } if fragment { //serveSimple(w, r, data, "templates/imgs_stub.html") @@ -92,7 +102,7 @@ func imgs(w http.ResponseWriter, r *http.Request) { } else if err != nil { log.Println(err) } - sendFlash(w, r, "redirect", "/imgs") + sendFlash(w, r, "redirect", r.URL.String()) http.Redirect(w, r, "/login", http.StatusSeeOther) } -- cgit v1.2.3