Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make user filter as restriction in RBAC #15367

Merged
merged 4 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/rbac/filterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -375,13 +375,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