diff --git a/pkg/cbox/share/sql/sql.go b/pkg/cbox/share/sql/sql.go index 5e786db4fa1..90c5f9786e6 100644 --- a/pkg/cbox/share/sql/sql.go +++ b/pkg/cbox/share/sql/sql.go @@ -494,14 +494,6 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, ref *collaboration.ShareR return rs, nil } -func groupFiltersByType(filters []*collaboration.Filter) map[collaboration.Filter_Type][]*collaboration.Filter { - grouped := make(map[collaboration.Filter_Type][]*collaboration.Filter) - for _, f := range filters { - grouped[f.Type] = append(grouped[f.Type], f) - } - return grouped -} - func granteeTypeToShareType(granteeType provider.GranteeType) int { switch granteeType { case provider.GranteeType_GRANTEE_TYPE_USER: @@ -519,7 +511,7 @@ func translateFilters(filters []*collaboration.Filter) (string, []interface{}, e params []interface{} ) - groupedFilters := groupFiltersByType(filters) + groupedFilters := share.GroupFiltersByType(filters) // If multiple filters of the same type are passed to this function, they need to be combined with the `OR` operator. // That is why the filters got grouped by type. // For every given filter type, iterate over the filters and if there are more than one combine them. diff --git a/pkg/share/manager/json/json.go b/pkg/share/manager/json/json.go index 45c2494b65b..b0ddd401d4d 100644 --- a/pkg/share/manager/json/json.go +++ b/pkg/share/manager/json/json.go @@ -361,14 +361,7 @@ func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) ( continue } // check filters - allFiltersMatch := true - for _, f := range filters { - if !share.MatchesFilter(s, f) { - allFiltersMatch = false - break - } - } - if allFiltersMatch { + if share.MatchesFilters(s, filters) { ss = append(ss, s) } } @@ -394,14 +387,7 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F continue } - allFiltersMatch := true - for _, f := range filters { - if !share.MatchesFilter(s, f) { - allFiltersMatch = false - break - } - } - if allFiltersMatch { + if share.MatchesFilters(s, filters) { rs := m.convert(ctx, s) rss = append(rss, rs) } diff --git a/pkg/share/manager/memory/memory.go b/pkg/share/manager/memory/memory.go index fc86422093f..13c808be766 100644 --- a/pkg/share/manager/memory/memory.go +++ b/pkg/share/manager/memory/memory.go @@ -228,14 +228,7 @@ func (m *manager) ListShares(ctx context.Context, filters []*collaboration.Filte continue } // check filters - allFiltersMatch := true - for _, f := range filters { - if !share.MatchesFilter(s, f) { - allFiltersMatch = false - break - } - } - if allFiltersMatch { + if share.MatchesFilters(s, filters) { ss = append(ss, s) } } @@ -260,14 +253,8 @@ func (m *manager) ListReceivedShares(ctx context.Context, filters []*collaborati rss = append(rss, rs) continue } - allFiltersMatch := true - for _, f := range filters { - if !share.MatchesFilter(s, f) { - allFiltersMatch = false - break - } - } - if allFiltersMatch { + + if share.MatchesFilters(s, filters) { rs := m.convert(ctx, s) rss = append(rss, rs) } diff --git a/pkg/share/manager/sql/sql.go b/pkg/share/manager/sql/sql.go index d935dd796c1..f6be2752d21 100644 --- a/pkg/share/manager/sql/sql.go +++ b/pkg/share/manager/sql/sql.go @@ -522,14 +522,6 @@ func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey) return m.convertToCS3ReceivedShare(ctx, s, m.storageMountID) } -func groupFiltersByType(filters []*collaboration.Filter) map[collaboration.Filter_Type][]*collaboration.Filter { - grouped := make(map[collaboration.Filter_Type][]*collaboration.Filter) - for _, f := range filters { - grouped[f.Type] = append(grouped[f.Type], f) - } - return grouped -} - func granteeTypeToShareType(granteeType provider.GranteeType) int { switch granteeType { case provider.GranteeType_GRANTEE_TYPE_USER: @@ -547,7 +539,7 @@ func translateFilters(filters []*collaboration.Filter) (string, []interface{}, e params []interface{} ) - groupedFilters := groupFiltersByType(filters) + groupedFilters := share.GroupFiltersByType(filters) // If multiple filters of the same type are passed to this function, they need to be combined with the `OR` operator. // That is why the filters got grouped by type. // For every given filter type, iterate over the filters and if there are more than one combine them. diff --git a/pkg/share/share.go b/pkg/share/share.go index 1da5efa3e09..5985b9aef6c 100644 --- a/pkg/share/share.go +++ b/pkg/share/share.go @@ -122,3 +122,37 @@ func MatchesFilter(share *collaboration.Share, filter *collaboration.Filter) boo return false } } + +// MatchesAnyFilter checks if the share passes at least one of the given filters. +func MatchesAnyFilter(share *collaboration.Share, filters []*collaboration.Filter) bool { + for _, f := range filters { + if MatchesFilter(share, f) { + return true + } + } + return false +} + +// MatchesFilters checks if the share passes the given filters. +// Filters of the same type form a disjuntion, a logical OR. Filters of separate type form a conjunction, a logical AND. +// Here is an example: +// (resource_id=1 OR resource_id=2) AND (grantee_type=USER OR grantee_type=GROUP) +func MatchesFilters(share *collaboration.Share, filters []*collaboration.Filter) bool { + grouped := GroupFiltersByType(filters) + for _, f := range grouped { + // + if !MatchesAnyFilter(share, f) { + return false + } + } + return true +} + +// GroupFiltersByTypes groups the given filters and returns a map using the filter type as the key. +func GroupFiltersByType(filters []*collaboration.Filter) map[collaboration.Filter_Type][]*collaboration.Filter { + grouped := make(map[collaboration.Filter_Type][]*collaboration.Filter) + for _, f := range filters { + grouped[f.Type] = append(grouped[f.Type], f) + } + return grouped +}