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

Add MiqExpression support for managed filters #15623

Merged
merged 2 commits into from
Aug 9, 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
1 change: 1 addition & 0 deletions app/models/entitlement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Entitlement < ApplicationRecord
belongs_to :miq_user_role

serialize :filters
serialize :filter_expression

def self.valid_filters?(filters_hash)
return true unless filters_hash # nil ok
Expand Down
5 changes: 4 additions & 1 deletion lib/rbac/filterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,11 @@ def get_self_service_objects(user, miq_group, klass)

def calc_filtered_ids(scope, user_filters, user, miq_group, scope_tenant_filter)
klass = scope.respond_to?(:klass) ? scope.klass : scope
expression = miq_group.try(:entitlement).try(:filter_expression)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably don't need the try for entitlement. In practice, every group should have an entitlement. Since removing the try will cause tests to fail, this can be done in a followup PR.

expression.set_tagged_target(klass) if expression
u_filtered_ids = pluck_ids(get_self_service_objects(user, miq_group, klass))
b_filtered_ids = get_belongsto_filter_object_ids(klass, user_filters['belongsto'])
m_filtered_ids = pluck_ids(get_managed_filter_object_ids(scope, user_filters['managed']))
m_filtered_ids = pluck_ids(get_managed_filter_object_ids(scope, expression || user_filters['managed']))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the the Entitlement model enforce setting only one of expression or user_filters?

d_filtered_ids = pluck_ids(matches_via_descendants(rbac_class(klass), user_filters['match_via_descendants'],
:user => user, :miq_group => miq_group))

Expand Down Expand Up @@ -417,6 +419,7 @@ def get_belongsto_filter_object_ids(klass, filter)
end

def get_managed_filter_object_ids(scope, filter)
return scope.where(filter.to_sql.first) if filter.kind_of?(MiqExpression)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this MiqExpression will be always convertible to sql ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpichler, yes, these expressions will be limited to tags on the main class only and will always be convertible to SQL.

klass = scope.respond_to?(:klass) ? scope.klass : scope
return nil if !TAGGABLE_FILTER_CLASSES.include?(safe_base_class(klass).name) || filter.blank?
scope.find_tags_by_grouping(filter, :ns => '*').reorder(nil)
Expand Down
50 changes: 50 additions & 0 deletions spec/lib/rbac/filterer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
describe Rbac::Filterer do
describe "using expressions as managed filters" do
it "supports OR conditions across categories" do
filter = MiqExpression.new(
"OR" => [
{"CONTAINS" => {"tag" => "managed-environment", "value" => "prod"}},
{"CONTAINS" => {"tag" => "managed-location", "value" => "ny"}}
]
)
group = create_group_with_expression(filter)
user = FactoryGirl.create(:user, :miq_groups => [group])
vm1, vm2, _vm3 = FactoryGirl.create_list(:vm_vmware, 3)
vm1.tag_with("/managed/environment/prod", :ns => "*")
vm2.tag_with("/managed/location/ny", :ns => "*")

actual, = Rbac::Filterer.search(:targets => Vm, :user => user)

expected = [vm1, vm2]
expect(actual).to match(expected)
end

it "supports AND conditions within categories" do
filter = MiqExpression.new(
"AND" => [
{"CONTAINS" => {"tag" => "managed-environment", "value" => "prod"}},
{"CONTAINS" => {"tag" => "managed-environment", "value" => "test"}}
]
)
group = create_group_with_expression(filter)
user = FactoryGirl.create(:user, :miq_groups => [group])
vm1, vm2, vm3 = FactoryGirl.create_list(:vm_vmware, 3)
vm1.tag_with("/managed/environment/prod /managed/environment/test", :ns => "*")
vm2.tag_with("/managed/environment/prod", :ns => "*")
vm3.tag_with("/managed/environment/test", :ns => "*")

actual, = Rbac::Filterer.search(:targets => Vm, :user => user)

expected = [vm1]
expect(actual).to match(expected)
end

def create_group_with_expression(expression)
role = FactoryGirl.create(:miq_user_role)
group = FactoryGirl.create(:miq_group, :tenant => Tenant.root_tenant, :miq_user_role => role)
group.entitlement = Entitlement.new
group.entitlement.filter_expression = expression
group.save!
group
end
end

describe '.combine_filtered_ids' do
# Algorithm (from Rbac::Filterer.combine_filtered_ids):
# Algorithm: b_intersection_m = (b_filtered_ids INTERSECTION m_filtered_ids)
Expand Down