From aa12a60d16970eef662ff151770ff11c4c42bd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 26 Jan 2023 11:54:32 +0100 Subject: [PATCH] fix populating user drive and drives (#5426) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix populating user drive and drives Signed-off-by: Jörn Friedrich Dreyer * update changelog Signed-off-by: Jörn Friedrich Dreyer * fix test condition Signed-off-by: Jörn Friedrich Dreyer Signed-off-by: Jörn Friedrich Dreyer --- .../populate-expanded-properties.md | 3 +- services/graph/pkg/service/v0/users.go | 39 ++++++++++++++----- services/graph/pkg/service/v0/users_test.go | 1 + 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/changelog/unreleased/populate-expanded-properties.md b/changelog/unreleased/populate-expanded-properties.md index b3ea06a435..81c242e954 100644 --- a/changelog/unreleased/populate-expanded-properties.md +++ b/changelog/unreleased/populate-expanded-properties.md @@ -3,4 +3,5 @@ Bugfix: Populate expanded properties We now return an empty array when an expanded relation has no entries. This makes consuming the responses a little easier. https://github.com/owncloud/ocis/pull/5421 -https://github.com/owncloud/ocis/issues/5419 \ No newline at end of file +https://github.com/owncloud/ocis/issues/5419 +https://github.com/owncloud/ocis/pull/5426 \ No newline at end of file diff --git a/services/graph/pkg/service/v0/users.go b/services/graph/pkg/service/v0/users.go index 47012c4112..09ea4512bb 100644 --- a/services/graph/pkg/service/v0/users.go +++ b/services/graph/pkg/service/v0/users.go @@ -269,7 +269,20 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) { } sel := strings.Split(r.URL.Query().Get("$select"), ",") exp := strings.Split(r.URL.Query().Get("$expand"), ",") - if slices.Contains(sel, "drive") || slices.Contains(sel, "drives") || slices.Contains(exp, "drive") || slices.Contains(exp, "drives") { + listDrives := slices.Contains(sel, "drives") || slices.Contains(exp, "drives") + listDrive := slices.Contains(sel, "drive") || slices.Contains(exp, "drive") + + // do we need to list all or only the personal drive + filters := []*storageprovider.ListStorageSpacesRequest_Filter{} + if listDrives || listDrive { + filters = append(filters, listStorageSpacesUserFilter(user.GetId())) + if !listDrives { + // TODO filter by owner when decomposedfs supports the OWNER filter + filters = append(filters, listStorageSpacesTypeFilter("personal")) + } + } + + if len(filters) > 0 { wdu, err := g.getWebDavBaseURL() if err != nil { // log error, wrong configuration @@ -281,14 +294,13 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) { render.Status(r, http.StatusInternalServerError) return } - logger.Debug().Str("id", user.GetId()).Msg("calling list storage spaces with user id filter") - f := listStorageSpacesUserFilter(user.GetId()) + logger.Debug().Str("id", user.GetId()).Msg("calling list storage spaces with filter") // use the unrestricted flag to get all possible spaces // users with the canListAllSpaces permission should see all spaces opaque := utils.AppendPlainToOpaque(nil, "unrestricted", "T") lspr, err := g.gatewayClient.ListStorageSpaces(r.Context(), &storageprovider.ListStorageSpacesRequest{ Opaque: opaque, - Filters: []*storageprovider.ListStorageSpacesRequest_Filter{f}, + Filters: filters, }) if err != nil { // transport error, needs to be fixed by admin @@ -298,13 +310,18 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) { return } if lspr.GetStatus().GetCode() != cs3rpc.Code_CODE_OK { - logger.Debug().Str("grpc", lspr.GetStatus().GetMessage()).Msg("could not get drive for user") + logger.Debug().Str("grpc", lspr.GetStatus().GetMessage()).Msg("could not list drives for user") // in case of NOT_OK, we can just return the user object with empty drives render.Status(r, status.HTTPStatusFromCode(http.StatusOK)) render.JSON(w, r, user) return } - drives := []libregraph.Drive{} + if listDrives { + user.Drives = make([]libregraph.Drive, 0, len(lspr.GetStorageSpaces())) + } + if listDrive { + user.Drive = &libregraph.Drive{} + } for _, sp := range lspr.GetStorageSpaces() { d, err := g.cs3StorageSpaceToDrive(r.Context(), wdu, sp) if err != nil { @@ -316,16 +333,18 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) { logger.Debug().Err(err).Interface("id", sp.Id).Msg("error calling get quota on drive") } d.Quota = "a - if slices.Contains(sel, "drive") || slices.Contains(exp, "drive") { - if *d.DriveType == "personal" { + if d.GetDriveType() == "personal" && sp.GetOwner().GetId().GetOpaqueId() == user.GetId() { + if listDrive { user.Drive = d } } else { - drives = append(drives, *d) - user.Drives = drives + if listDrives { + user.Drives = append(user.Drives, *d) + } } } } + // expand appRoleAssignments if requested if slices.Contains(exp, "appRoleAssignments") { user.AppRoleAssignments, err = g.fetchAppRoleAssignments(r.Context(), user.GetId()) diff --git a/services/graph/pkg/service/v0/users_test.go b/services/graph/pkg/service/v0/users_test.go index e3b1a5b8f2..9f21aa1a19 100644 --- a/services/graph/pkg/service/v0/users_test.go +++ b/services/graph/pkg/service/v0/users_test.go @@ -372,6 +372,7 @@ var _ = Describe("Users", func() { }, { Id: &provider.StorageSpaceId{OpaqueId: "personal"}, + Owner: &userv1beta1.User{Id: &userv1beta1.UserId{OpaqueId: "user1"}}, Root: &provider.ResourceId{SpaceId: "personal", OpaqueId: "personal"}, SpaceType: "personal", },