From 2d3cae253fe3bb5c168f7c76c5f41f5b94b6d79e Mon Sep 17 00:00:00 2001 From: David Christofas Date: Mon, 17 Jan 2022 15:12:23 +0100 Subject: [PATCH] refactor share storage id filter --- .../grpc/services/gateway/storageprovider.go | 25 +++---------------- pkg/publicshare/publicshare.go | 20 ++++++++++++++- pkg/share/share.go | 20 ++++++++++++++- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/internal/grpc/services/gateway/storageprovider.go b/internal/grpc/services/gateway/storageprovider.go index a2c1d7f002..01b0baee29 100644 --- a/internal/grpc/services/gateway/storageprovider.go +++ b/internal/grpc/services/gateway/storageprovider.go @@ -42,23 +42,18 @@ import ( "github.com/cs3org/reva/pkg/appctx" ctxpkg "github.com/cs3org/reva/pkg/ctx" "github.com/cs3org/reva/pkg/errtypes" + "github.com/cs3org/reva/pkg/publicshare" "github.com/cs3org/reva/pkg/rgrpc/status" "github.com/cs3org/reva/pkg/rgrpc/todo/pool" "github.com/cs3org/reva/pkg/rhttp/router" sdk "github.com/cs3org/reva/pkg/sdk/common" + "github.com/cs3org/reva/pkg/share" "github.com/cs3org/reva/pkg/utils" "github.com/golang-jwt/jwt" "github.com/pkg/errors" gstatus "google.golang.org/grpc/status" ) -const ( - // ShareSpaceFilterType represents a share filter type to filter by space ids. - ShareSpaceFilterType collaborationv1beta1.Filter_Type = 7 - // PublicShareSpaceFilterType represents a publicshare filter type to filter by space ids. - PublicShareSpaceFilterType linkv1beta1.ListPublicSharesRequest_Filter_Type = 4 -) - /* About caching The gateway is doing a lot of requests to look up the responsible storage providers for a reference. - when the reference uses an id we can use a global id -> provider cache because it is the same for all users @@ -344,13 +339,7 @@ func (s *svc) DeleteStorageSpace(ctx context.Context, req *provider.DeleteStorag log.Debug().Msg("purging storage space") // List all shares in this storage space lsRes, err := s.ListShares(ctx, &collaborationv1beta1.ListSharesRequest{ - Filters: []*collaborationv1beta1.Filter{ - { - // TODO: introduce the new fiter type to the CS3 API - Type: ShareSpaceFilterType, - Term: &collaborationv1beta1.Filter_ResourceId{ResourceId: &provider.ResourceId{StorageId: storageid}}, - }, - }, + Filters: []*collaborationv1beta1.Filter{share.StorageIDFilter(storageid)}, }) switch { case err != nil: @@ -375,13 +364,7 @@ func (s *svc) DeleteStorageSpace(ctx context.Context, req *provider.DeleteStorag // List all public shares in this storage space lpsRes, err := s.ListPublicShares(ctx, &linkv1beta1.ListPublicSharesRequest{ - Filters: []*linkv1beta1.ListPublicSharesRequest_Filter{ - { - // TODO: introduce the new fiter type to the CS3 API - Type: PublicShareSpaceFilterType, - Term: &linkv1beta1.ListPublicSharesRequest_Filter_ResourceId{ResourceId: &provider.ResourceId{StorageId: storageid}}, - }, - }, + Filters: []*linkv1beta1.ListPublicSharesRequest_Filter{publicshare.StorageIDFilter(storageid)}, }) switch { case err != nil: diff --git a/pkg/publicshare/publicshare.go b/pkg/publicshare/publicshare.go index 1fd214ef59..eb8dec3cdc 100644 --- a/pkg/publicshare/publicshare.go +++ b/pkg/publicshare/publicshare.go @@ -33,6 +33,12 @@ import ( "github.com/cs3org/reva/pkg/utils" ) +const ( + // StorageIDFilterType defines a new filter type for storage id. + // TODO: Remove this once the filter type is in the CS3 API. + StorageIDFilterType link.ListPublicSharesRequest_Filter_Type = 4 +) + // Manager manipulates public shares. type Manager interface { CreatePublicShare(ctx context.Context, u *user.User, md *provider.ResourceInfo, g *link.Grant) (*link.PublicShare, error) @@ -95,12 +101,24 @@ func ResourceIDFilter(id *provider.ResourceId) *link.ListPublicSharesRequest_Fil } } +// StorageIDFilter is an abstraction for creating filter by storage id. +func StorageIDFilter(id string) *link.ListPublicSharesRequest_Filter { + return &link.ListPublicSharesRequest_Filter{ + Type: StorageIDFilterType, + Term: &link.ListPublicSharesRequest_Filter_ResourceId{ + ResourceId: &provider.ResourceId{ + StorageId: id, + }, + }, + } +} + // MatchesFilter tests if the share passes the filter. func MatchesFilter(share *link.PublicShare, filter *link.ListPublicSharesRequest_Filter) bool { switch filter.Type { case link.ListPublicSharesRequest_Filter_TYPE_RESOURCE_ID: return utils.ResourceIDEqual(share.ResourceId, filter.GetResourceId()) - case link.ListPublicSharesRequest_Filter_Type(4): + case StorageIDFilterType: return share.ResourceId.StorageId == filter.GetResourceId().GetStorageId() default: return false diff --git a/pkg/share/share.go b/pkg/share/share.go index c22317c846..9ecb37f4c3 100644 --- a/pkg/share/share.go +++ b/pkg/share/share.go @@ -29,6 +29,12 @@ import ( "google.golang.org/genproto/protobuf/field_mask" ) +const ( + // StorageIDFilterType defines a new filter type for storage id. + // TODO: Remove once this filter type is in the CS3 API. + StorageIDFilterType collaboration.Filter_Type = 7 +) + //go:generate mockery -name Manager // Manager is the interface that manipulates shares. @@ -89,6 +95,18 @@ func ResourceIDFilter(id *provider.ResourceId) *collaboration.Filter { } } +// StorageIDFilter is an abstraction for creating filter by storage id. +func StorageIDFilter(id string) *collaboration.Filter { + return &collaboration.Filter{ + Type: StorageIDFilterType, + Term: &collaboration.Filter_ResourceId{ + ResourceId: &provider.ResourceId{ + StorageId: id, + }, + }, + } +} + // IsCreatedByUser checks if the user is the owner or creator of the share. func IsCreatedByUser(share *collaboration.Share, user *userv1beta1.User) bool { return utils.UserEqual(user.Id, share.Owner) || utils.UserEqual(user.Id, share.Creator) @@ -121,7 +139,7 @@ func MatchesFilter(share *collaboration.Share, filter *collaboration.Filter) boo // This filter type is used to filter out "denial shares". These are currently implemented by having the permission "0". // I.e. if the permission is 0 we don't want to show it. return int(conversions.RoleFromResourcePermissions(share.Permissions.Permissions).OCSPermissions()) != 0 - case collaboration.Filter_Type(7): + case StorageIDFilterType: return share.ResourceId.StorageId == filter.GetResourceId().GetStorageId() default: return false