Skip to content

Commit

Permalink
refactor share storage id filter
Browse files Browse the repository at this point in the history
  • Loading branch information
David Christofas committed Jan 17, 2022
1 parent d9c2baf commit 2d3cae2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
25 changes: 4 additions & 21 deletions internal/grpc/services/gateway/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
20 changes: 19 additions & 1 deletion pkg/publicshare/publicshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion pkg/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2d3cae2

Please sign in to comment.