diff --git a/lib/rbac/filterer.rb b/lib/rbac/filterer.rb index adbbe88b6f1..cd49a6dc86d 100644 --- a/lib/rbac/filterer.rb +++ b/lib/rbac/filterer.rb @@ -359,9 +359,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) - # 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 + # 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 # # 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 @@ -375,13 +375,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) { [operand1, operand2].compact.reduce(&:&) } - union = ->(operand1, operand2, operand3 = nil) { [operand1, operand2, operand3].compact.reduce(&:|) } + intersection = ->(operand1, operand2, operand3 = nil) { [operand1, operand2, operand3].compact.reduce(&:&) } + union = ->(operand1, operand2) { [operand1, operand2].compact.reduce(&:|) } - 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) + b_intersection_m = intersection.call(b_filtered_ids, m_filtered_ids) + d_union_b_intersection_m = union.call(d_filtered_ids, b_intersection_m) - intersection.call(u_union_d_union_b_intersection_m, tenant_filter_ids) + intersection.call(d_union_b_intersection_m, tenant_filter_ids, u_filtered_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 88a03ba9a42..43c1d64d3f6 100644 --- a/spec/lib/rbac/filterer_spec.rb +++ b/spec/lib/rbac/filterer_spec.rb @@ -1,9 +1,9 @@ describe Rbac::Filterer do describe '.combine_filtered_ids' do # Algorithm (from Rbac::Filterer.combine_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 + # 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 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) @@ -38,15 +38,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 match_array([1, 3]) + expect(combine_filtered_ids([1], [2, 3], [3, 4], nil, nil)).to be_empty 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], [2, 3], [3, 4], [5, 6], nil)).to match_array([1, 3, 5, 6]) + expect(combine_filtered_ids([1, 5, 6], [2, 3], [3, 4], [5, 6], nil)).to match_array([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], [2, 3], [3, 4], [5, 6], [1, 6])).to match_array([1, 6]) + expect(combine_filtered_ids([1, 6], [2, 3], [3, 4], [5, 6], [1, 6])).to match_array([6]) end it 'belongs to managed filters, descendants filter and tenant filter(self service user, Host & Cluster filter and tags)' do