From aa771d4ec8852545c80efd095d5f6bad6cfff42f Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Fri, 27 Nov 2026 11:53:18 +0100 Subject: [PATCH] fix(graph): look up the user drive by id when the user is unknown Since #3243 GetUserDrive resolves the {user-id} segment through the identity backend, so a user that was deleted in an external LDAP or AD turned into a "not found" error although the personal space still exists. When the identity backend does not know the user, the given segment is used for the storage lookup as before. --- services/graph/pkg/service/v0/users.go | 11 +++-- services/graph/pkg/service/v0/users_test.go | 51 +++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/services/graph/pkg/service/v0/users.go b/services/graph/pkg/service/v0/users.go index aed1fcdc49..0eef300f61 100644 --- a/services/graph/pkg/service/v0/users.go +++ b/services/graph/pkg/service/v0/users.go @@ -166,12 +166,17 @@ func (g Graph) GetUserDrive(w http.ResponseWriter, r *http.Request) { userID = u.GetId().GetOpaqueId() } else { user, err := g.identityBackend.GetUser(ctx, userID, odataReq) - if err != nil { - log.Debug().Err(err).Str("userID", userID).Msg("could not get user drive: failed to resolve user") + switch { + case err == nil: + userID = user.GetId() + case errorcode.IsErrorCode(err, errorcode.ItemNotFound): + // a user deleted in an external identity provider keeps the personal space until it is cleaned up + log.Debug().Err(err).Msg("could not resolve user, looking up the drive with the given id") + default: + log.Debug().Err(err).Msg("could not get user drive: failed to resolve user") errorcode.RenderError(w, r, err) return } - userID = user.GetId() } log.Debug().Msg("calling list storage spaces with user and personal filter") diff --git a/services/graph/pkg/service/v0/users_test.go b/services/graph/pkg/service/v0/users_test.go index 3cb9fb90b8..2e99e28712 100644 --- a/services/graph/pkg/service/v0/users_test.go +++ b/services/graph/pkg/service/v0/users_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "io" "net/http" "net/http/httptest" @@ -42,6 +43,7 @@ import ( "github.com/opencloud-eu/opencloud/services/graph/pkg/config" "github.com/opencloud-eu/opencloud/services/graph/pkg/config/defaults" "github.com/opencloud-eu/opencloud/services/graph/pkg/errorcode" + "github.com/opencloud-eu/opencloud/services/graph/pkg/identity" identitymocks "github.com/opencloud-eu/opencloud/services/graph/pkg/identity/mocks" service "github.com/opencloud-eu/opencloud/services/graph/pkg/service/v0" ) @@ -215,6 +217,55 @@ var _ = Describe("Users", func() { Expect(rr.Code).To(Equal(http.StatusOK)) identityBackend.AssertCalled(GinkgoT(), "GetUser", mock.Anything, "alice", mock.Anything) }) + + It("looks up the personal space with the given id when the user is unknown", func() { + permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil) + identityBackend.On("GetUser", mock.Anything, "deleted-user-id", mock.Anything).Return(nil, identity.ErrNotFound) + gatewayClient.On("ListStorageSpaces", mock.Anything, mock.MatchedBy(func(req *provider.ListStorageSpacesRequest) bool { + for _, f := range req.GetFilters() { + if f.GetUser().GetOpaqueId() == "deleted-user-id" { + return true + } + } + return false + }), mock.Anything).Return(&provider.ListStorageSpacesResponse{ + Status: status.NewOK(ctx), + StorageSpaces: []*provider.StorageSpace{ + { + Id: &provider.StorageSpaceId{OpaqueId: "drive1"}, + Root: &provider.ResourceId{StorageId: "storage", SpaceId: "space", OpaqueId: "space"}, + SpaceType: "personal", + }, + }, + }, nil) + gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{ + Status: status.NewOK(ctx), + TotalBytes: 10, + }, nil) + valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything). + Return(&settings.GetValueResponse{}, nil) + + r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users/deleted-user-id/drive", nil) + rctx := chi.NewRouteContext() + rctx.URLParams.Add("userID", "deleted-user-id") + r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) + svc.GetUserDrive(rr, r) + + Expect(rr.Code).To(Equal(http.StatusOK)) + }) + + It("fails when the user cannot be resolved for another reason", func() { + identityBackend.On("GetUser", mock.Anything, "alice", mock.Anything).Return(nil, errors.New("backend down")) + + r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users/alice/drive", nil) + rctx := chi.NewRouteContext() + rctx.URLParams.Add("userID", "alice") + r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx)) + svc.GetUserDrive(rr, r) + + Expect(rr.Code).To(Equal(http.StatusInternalServerError)) + gatewayClient.AssertNotCalled(GinkgoT(), "ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything) + }) }) Describe("GetMe", func() {