Skip to content

Commit

Permalink
Merge pull request #15367 from lpichler/make_user_filter_as_restriction
Browse files Browse the repository at this point in the history
Make user filter as restriction in RBAC
  • Loading branch information
gtanzillo authored Jun 19, 2017
2 parents ed707ba + a5fe841 commit ed5da67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions lib/rbac/filterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,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
Expand All @@ -376,13 +376,13 @@ def calc_filtered_ids(scope, user_filters, user, miq_group, scope_tenant_filter)
# @return [Array<Integer>] 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)
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/rbac/filterer_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ed5da67

Please sign in to comment.