From 9cc18a81e3e1b8bf96795bfbe3d83ced294ecfd7 Mon Sep 17 00:00:00 2001 From: mehmet turac Date: Sun, 17 May 2026 16:01:02 +0300 Subject: [PATCH] fix: show item shares from all users to admins (#5941) --- http/share.go | 10 ++++- share/storage.go | 17 +++++++++ share/storage_test.go | 85 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 share/storage_test.go diff --git a/http/share.go b/http/share.go index 35125dba..ec95d066 100644 --- a/http/share.go +++ b/http/share.go @@ -57,7 +57,15 @@ var shareListHandler = withPermShare(func(w http.ResponseWriter, r *http.Request }) var shareGetsHandler = withPermShare(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { - s, err := d.store.Share.Gets(r.URL.Path, d.user.ID) + var ( + s []*share.Link + err error + ) + if d.user.Perm.Admin { + s, err = d.store.Share.GetsByPath(r.URL.Path) + } else { + s, err = d.store.Share.Gets(r.URL.Path, d.user.ID) + } if errors.Is(err, fberrors.ErrNotExist) { return renderJSON(w, r, []*share.Link{}) } diff --git a/share/storage.go b/share/storage.go index 76ba26eb..9c28cfd1 100644 --- a/share/storage.go +++ b/share/storage.go @@ -110,6 +110,23 @@ func (s *Storage) Gets(path string, id uint) ([]*Link, error) { return links, nil } +// GetsByPath returns all non-expired links for a path. +func (s *Storage) GetsByPath(path string) ([]*Link, error) { + links, err := s.All() + if err != nil { + return nil, err + } + + filtered := make([]*Link, 0, len(links)) + for _, link := range links { + if link.Path == path { + filtered = append(filtered, link) + } + } + + return filtered, nil +} + // Save wraps a StorageBackend.Save func (s *Storage) Save(l *Link) error { return s.back.Save(l) diff --git a/share/storage_test.go b/share/storage_test.go new file mode 100644 index 00000000..e402f6e9 --- /dev/null +++ b/share/storage_test.go @@ -0,0 +1,85 @@ +package share + +import ( + "reflect" + "testing" +) + +type fakeBackend struct { + links []*Link +} + +func (f fakeBackend) All() ([]*Link, error) { + return f.links, nil +} + +func (f fakeBackend) FindByUserID(id uint) ([]*Link, error) { + var links []*Link + for _, link := range f.links { + if link.UserID == id { + links = append(links, link) + } + } + return links, nil +} + +func (f fakeBackend) GetByHash(hash string) (*Link, error) { + for _, link := range f.links { + if link.Hash == hash { + return link, nil + } + } + return nil, nil +} + +func (f fakeBackend) GetPermanent(path string, id uint) (*Link, error) { + for _, link := range f.links { + if link.Path == path && link.UserID == id && link.Expire == 0 { + return link, nil + } + } + return nil, nil +} + +func (f fakeBackend) Gets(path string, id uint) ([]*Link, error) { + var links []*Link + for _, link := range f.links { + if link.Path == path && link.UserID == id { + links = append(links, link) + } + } + return links, nil +} + +func (f fakeBackend) Save(_ *Link) error { + return nil +} + +func (f fakeBackend) Delete(_ string) error { + return nil +} + +func (f fakeBackend) DeleteWithPathPrefix(_ string) error { + return nil +} + +func TestGetsByPathReturnsLinksFromAllUsers(t *testing.T) { + t.Parallel() + + expected := []*Link{ + {Hash: "a", Path: "/file.txt", UserID: 1}, + {Hash: "b", Path: "/file.txt", UserID: 2}, + } + store := NewStorage(fakeBackend{ + links: append(expected, &Link{Hash: "c", Path: "/other.txt", UserID: 3}), + }) + + links, err := store.GetsByPath("/file.txt") + if err != nil { + t.Fatalf("GetsByPath returned error: %v", err) + } + + if !reflect.DeepEqual(links, expected) { + t.Fatalf("GetsByPath returned %#v, want %#v", links, expected) + } +}