diff --git a/lib/rbac/filterer.rb b/lib/rbac/filterer.rb index 2b3752354ae..2e170809eaf 100644 --- a/lib/rbac/filterer.rb +++ b/lib/rbac/filterer.rb @@ -431,9 +431,9 @@ def calc_filtered_ids(scope, user_filters, user, miq_group, scope_tenant_filter) end # - # Algorithm: b_intersection_m = (b_filtered_ids INTERSECTION m_filtered_ids) - # d_union_b_and_m = d_filtered_ids UNION b_intersection_m - # filter = d_union_b_and_m INTERSECTION tenant_filter_ids INTERSECTION u_filtered_ids + # Algorithm: b_intersection_m = (b_filtered_ids INTERSECTION m_filtered_ids) + # u_union_d_union_b_and_m = u_filtered_ids UNION d_filtered_ids UNION b_intersection_m + # filter = u_union_d_union_b_and_m INTERSECTION tenant_filter_ids # # a nil as input for any field means it DOES NOT apply the operation(INTERSECTION, UNION) # a nil as output means there is not filter @@ -447,13 +447,13 @@ def calc_filtered_ids(scope, user_filters, user, miq_group, scope_tenant_filter) # @return [Array] target ids for filter def combine_filtered_ids(u_filtered_ids, b_filtered_ids, m_filtered_ids, d_filtered_ids, tenant_filter_ids) - intersection = ->(operand1, operand2, operand3 = nil) { [operand1, operand2, operand3].compact.reduce(&:&) } - union = ->(operand1, operand2) { [operand1, operand2].compact.reduce(&:|) } + intersection = ->(operand1, operand2) { [operand1, operand2].compact.reduce(&:&) } + union = ->(operand1, operand2, operand3 = nil) { [operand1, operand2, operand3].compact.reduce(&:|) } - b_intersection_m = intersection.call(b_filtered_ids, m_filtered_ids) - d_union_b_intersection_m = union.call(d_filtered_ids, b_intersection_m) + b_intersection_m = intersection.call(b_filtered_ids, m_filtered_ids) + u_union_d_union_b_intersection_m = union.call(u_filtered_ids, d_filtered_ids, b_intersection_m) - intersection.call(d_union_b_intersection_m, tenant_filter_ids, u_filtered_ids) + intersection.call(u_union_d_union_b_intersection_m, tenant_filter_ids) end # @param parent_class [Class] Class of parent (e.g. Host) diff --git a/spec/lib/rbac/filterer_spec.rb b/spec/lib/rbac/filterer_spec.rb index d4f73040f0d..3f97479d19c 100644 --- a/spec/lib/rbac/filterer_spec.rb +++ b/spec/lib/rbac/filterer_spec.rb @@ -51,9 +51,9 @@ def create_group_with_expression(expression) describe '.combine_filtered_ids' do # Algorithm (from Rbac::Filterer.combine_filtered_ids): - # Algorithm: b_intersection_m = (b_filtered_ids INTERSECTION m_filtered_ids) - # d_union_b_and_m = d_filtered_ids UNION b_intersection_m - # filter = d_union_b_and_m INTERSECTION tenant_filter_ids INTERSECTION u_filtered_ids + # b_intersection_m = (belongsto_filtered_ids INTERSECTION managed_filtered_ids) + # u_union_d_union_b_and_m = user_filtered_ids UNION descendant_filtered_ids UNION belongsto_filtered_ids + # filter = u_union_d_union_b_and_m INTERSECTION tenant_filter_ids def combine_filtered_ids(user_filtered_ids, belongsto_filtered_ids, managed_filtered_ids, descendant_filtered_ids, tenant_filter_ids) Rbac::Filterer.new.send(:combine_filtered_ids, user_filtered_ids, belongsto_filtered_ids, managed_filtered_ids, descendant_filtered_ids, tenant_filter_ids) @@ -88,15 +88,15 @@ def combine_filtered_ids(user_filtered_ids, belongsto_filtered_ids, managed_filt end it 'user filter, belongs to and managed filters(self service user, Host & Cluster filter and tags)' do - expect(combine_filtered_ids([1], [2, 3], [3, 4], nil, nil)).to be_empty + expect(combine_filtered_ids([1], [2, 3], [3, 4], nil, nil)).to match_array([1, 3]) end it 'user filter, belongs to, managed filters and descendants filter(self service user, Host & Cluster filter and tags)' do - expect(combine_filtered_ids([1, 5, 6], [2, 3], [3, 4], [5, 6], nil)).to match_array([5, 6]) + expect(combine_filtered_ids([1], [2, 3], [3, 4], [5, 6], nil)).to match_array([1, 3, 5, 6]) end it 'user filter, belongs to managed filters, descendants filter and tenant filter(self service user, Host & Cluster filter and tags)' do - expect(combine_filtered_ids([1, 6], [2, 3], [3, 4], [5, 6], [1, 6])).to match_array([6]) + expect(combine_filtered_ids([1], [2, 3], [3, 4], [5, 6], [1, 6])).to match_array([1, 6]) end it 'belongs to managed filters, descendants filter and tenant filter(self service user, Host & Cluster filter and tags)' do