From 79ae2f563c8c7bbd64c23ea7c550a228d9d7be2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 19 Jan 2023 09:35:30 +0000 Subject: [PATCH 1/3] fix populating user drive and drives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- services/graph/pkg/service/v0/users.go | 39 +++++++++++++++------ services/graph/pkg/service/v0/users_test.go | 3 +- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/services/graph/pkg/service/v0/users.go b/services/graph/pkg/service/v0/users.go index c0bddc77752..5fe333a0930 100644 --- a/services/graph/pkg/service/v0/users.go +++ b/services/graph/pkg/service/v0/users.go @@ -273,7 +273,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 @@ -285,14 +298,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 @@ -302,13 +314,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 { @@ -320,16 +337,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 d516782534b..66247fde70a 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", }, @@ -391,7 +392,7 @@ var _ = Describe("Users", func() { err = json.Unmarshal(data, &responseUser) Expect(err).ToNot(HaveOccurred()) Expect(responseUser.GetId()).To(Equal("user1")) - Expect(*responseUser.GetDrive().Id).To(Equal("personal")) + Expect(responseUser.GetDrive().Id).To(Equal("personal")) }) It("includes the drives if requested", func() { From 1437f617fc254b298a2b739d224af90845a5b1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 19 Jan 2023 09:37:00 +0000 Subject: [PATCH 2/3] update changelog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- changelog/unreleased/populate-expanded-properties.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog/unreleased/populate-expanded-properties.md b/changelog/unreleased/populate-expanded-properties.md index b3ea06a4357..81c242e9544 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 From 93d92e0e2c22a799bd4259b9420cd3b74b3776d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 19 Jan 2023 09:47:04 +0000 Subject: [PATCH 3/3] fix test condition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- services/graph/pkg/service/v0/users_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/graph/pkg/service/v0/users_test.go b/services/graph/pkg/service/v0/users_test.go index 66247fde70a..35bcc180916 100644 --- a/services/graph/pkg/service/v0/users_test.go +++ b/services/graph/pkg/service/v0/users_test.go @@ -392,7 +392,7 @@ var _ = Describe("Users", func() { err = json.Unmarshal(data, &responseUser) Expect(err).ToNot(HaveOccurred()) Expect(responseUser.GetId()).To(Equal("user1")) - Expect(responseUser.GetDrive().Id).To(Equal("personal")) + Expect(*responseUser.GetDrive().Id).To(Equal("personal")) }) It("includes the drives if requested", func() {