Skip to content

Commit

Permalink
Merge pull request ManageIQ#2261 from lgalis/add_tag_expression_to_gr…
Browse files Browse the repository at this point in the history
…oup_editor

Add tag expression to the group editor
  • Loading branch information
h-kataria authored Oct 10, 2017
2 parents f777e6e + 84271be commit bdef0ac
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 40 deletions.
88 changes: 77 additions & 11 deletions app/controllers/ops_controller/ops_rbac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def rbac_group_add
def rbac_group_edit
assert_privileges("rbac_group_edit")
case params[:button]
when 'cancel' then rbac_edit_cancel('group')
when 'save', 'add' then rbac_edit_save_or_add('group')
when 'reset', nil then rbac_edit_reset(params[:typ], 'group', MiqGroup) # Reset or first time in
when 'cancel' then rbac_edit_cancel('group')
when 'save', 'add' then rbac_edit_save_or_add('group')
when 'reset', nil then rbac_edit_reset(params[:typ], 'group', MiqGroup) # Reset or first time in
end
end

Expand Down Expand Up @@ -442,6 +442,15 @@ def rbac_group_seq_edit
end
end

def rbac_group_filter_expression
rbac_group_set_form_vars
@changed = session[:changed] = (@edit[:new] != @edit[:current])
@expkey = params[:button].to_sym
@edit[:filter_expression_table] = exp_build_table_or_nil(@edit[:new][:filter_expression])
@in_a_form = true
replace_right_cell(:nodetype => x_node)
end

def rbac_group_seq_edit_screen
@edit = {}
@edit[:new] = {}
Expand Down Expand Up @@ -815,7 +824,7 @@ def rbac_build_list(rec_type)
# AJAX driven routine to check for changes in ANY field on the form
def rbac_field_changed(rec_type)
id = params[:id].split('__').first # Get the record id
id = from_cid(id) unless id == "new" || id == "seq" # Decompress id if not "new"
id = from_cid(id) unless %w(new seq).include?(id)
return unless load_edit("rbac_#{rec_type}_edit__#{id}", "replace_cell__explorer")

case rec_type
Expand Down Expand Up @@ -849,6 +858,8 @@ def rbac_field_changed(rec_type)
:partial => @refresh_partial,
:locals => {:type => "classifications", :action_url => 'rbac_group_field_changed'}) if @refresh_div

page.replace("customer_tags_div", :partial => "ops/rbac_group/customer_tags") if params[:use_filter_expression].present?

# Only update description field value if ldap group user field was selected
page << "$('#description').val('#{j_str(@edit[:new][:ldap_groups_user])}');" if params[:ldap_groups_user]

Expand Down Expand Up @@ -942,8 +953,11 @@ def rbac_group_get_details(id)
@record = @group = MiqGroup.find_by(:id => from_cid(id))
@belongsto = {}
@filters = {}
@filter_expression = []
@use_filter_expression = false
if @record.present?
get_tagdata(@group)
@use_filter_expression = @group.entitlement[:filter_expression].kind_of?(MiqExpression)
# Build the belongsto filters hash
@group.get_belongsto_filters.each do |b| # Go thru the belongsto tags
bobj = MiqFilter.belongsto2object(b) # Convert to an object
Expand Down Expand Up @@ -1143,6 +1157,17 @@ def rbac_group_get_form_vars
end
end

if params[:use_filter_expression]
@edit[:new][:use_filter_expression] = params[:use_filter_expression]
if params[:use_filter_expression] == 'false'
@edit[:new][:use_filter_expression] = false
@group = MiqGroup.find_by(:id => @edit[:group_id])
rbac_group_right_tree(@edit[:new][:belongsto].keys)
elsif params[:use_filter_expression] == 'true'
@edit[:use_filter_expression] = true
end
end

params[:tree_typ] ? params[:tree_typ] + "_tree" : nil
end

Expand All @@ -1153,6 +1178,7 @@ def rbac_group_set_form_vars
@edit = {
:new => {
:filters => {},
:filter_expression => {},
:belongsto => {},
:description => @group.description,
},
Expand Down Expand Up @@ -1195,29 +1221,68 @@ def rbac_group_set_form_vars
@edit[:projects_tenants].push(["Tenants", all_tenants]) unless all_tenants.blank?
@edit[:new][:group_tenant] = @group.tenant_id

rbac_group_filter_expression_vars(:filter_expression, :filter_expression_table)
@edit[:current] = copy_hash(@edit[:new])

rbac_group_right_tree(@edit[:new][:belongsto].keys)
end

def rbac_group_filter_expression_vars(field_expression, field_expression_table)
if @group && @group.entitlement && @group.entitlement[field_expression].kind_of?(MiqExpression)
@edit[:new][field_expression] = @group.entitlement[field_expression].exp
else
@edit[:new][field_expression] = nil
end
@edit[:new][:use_filter_expression] = true
# Populate exp editor fields for the expression column
@edit[field_expression] ||= ApplicationController::Filter::Expression.new
@edit[field_expression][:expression] = [] # Store exps in an array
if @edit[:new][field_expression].blank?
@edit[:new][:use_filter_expression] = false
@edit[field_expression][:expression] = {"???" => "???"} # Set as new exp element
@edit[:new][field_expression] = copy_hash(@edit[field_expression][:expression]) # Copy to new exp
else
@edit[field_expression][:expression] = copy_hash(@edit[:new][field_expression])
end
@edit[field_expression_table] = exp_build_table_or_nil(@edit[field_expression][:expression])

@expkey = field_expression # Set expression key to expression
@edit[field_expression].history.reset(@edit[field_expression][:expression])
@edit[field_expression][:exp_table] = exp_build_table(@edit[field_expression][:expression])
@edit[field_expression][:exp_model] = @group.class.to_s # Set model for the exp editor
end

# Set group record variables to new values
def rbac_group_set_record_vars(group)
role = MiqUserRole.find(@edit[:new][:role])
group.description = @edit[:new][:description]
group.miq_user_role = role
group.tenant = Tenant.find(@edit[:new][:group_tenant]) if @edit[:new][:group_tenant]
if @edit[:new][:use_filter_expression]
@edit[:new][:filters].clear
else
exp_remove_tokens(@edit[:new][:filter_expression])
@edit[:new][:filter_expression] = nil
end

rbac_group_set_filters(group) # Go set the filters for the group
end

# Set filters in the group record from the @edit[:new] hash values
def rbac_group_set_filters(group)
@set_filter_values = []
@edit[:new][:filters].each do |_key, value|
@set_filter_values.push(value)
end
rbac_group_make_subarrays # Need to have category arrays of item arrays for and/or logic
group.entitlement ||= Entitlement.new
group.entitlement.set_managed_filters(@set_filter_values)
if @edit[:new][:use_filter_expression]
group.entitlement.set_managed_filters(nil) if group.entitlement.get_managed_filters.present?
group.entitlement.filter_expression = @edit[:new][:filter_expression]["???"] ? nil : MiqExpression.new(@edit[:new][:filter_expression])
else
group.entitlement.filter_expression = nil if group.entitlement.filter_expression
@set_filter_values = []
@edit[:new][:filters].each do |_key, value|
@set_filter_values.push(value)
end
rbac_group_make_subarrays # Need to have category arrays of item arrays for and/or logic
group.entitlement.set_managed_filters(@set_filter_values)
end
group.entitlement.set_belongsto_filters(@edit[:new][:belongsto].values) # Set belongs to to hash values
end

Expand Down Expand Up @@ -1363,7 +1428,8 @@ def rbac_role_validate?

# Validate some of the role fields
def rbac_group_validate?
@assigned_filters = [] if @edit[:new][:filters].empty?
@assigned_filters = [] if @edit[:new][:filters].empty? || @edit[:new][:use_filter_expression]
@filter_expression = [] if @edit[:new][:filter_expression].empty? || @edit[:new][:use_filter_expression] == false
if @edit[:new][:role].nil? || @edit[:new][:role] == ""
add_flash(_("A User Group must be assigned a Role"), :error)
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
%label.control-label.col-md-2
= title
.col-md-8
=_("Choose an element of the expression to edit")
= _("Choose an element of the expression to edit")
= render :partial => 'layouts/exp_editor'
- else
.form-group
%label.control-label.col-md-2
= title
.col-md-8
= link_to({:action => "button_update",
:button => expression_type},
"data-miq_sparkle_on" => true,
"data-miq_sparkle_off" => true,
:remote => true,
"data-method" => :post) do
= link_to({:action => action,
:button => expression_type},
"data-miq_sparkle_on" => true,
"data-miq_sparkle_off" => true,
:remote => true,
"data-method" => :post) do
%button.btn.btn-default
- if @edit[table_key].nil?
= _("Define Expression")
Expand Down
14 changes: 14 additions & 0 deletions app/views/layouts/_group_filter_expression.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- if @edit.nil?
%h3
= _('Filter Expression')
.form-horizontal.static
- if @group && @group.entitlement && @group.entitlement.filter_expression && @group.entitlement.filter_expression.kind_of?(MiqExpression)
= h(@group.entitlement.filter_expression.to_human)
- else
= render :partial => 'layouts/info_msg', :locals => {:message => _("No Filter Expression defined")}
- else
= render :partial => 'layouts/filter_expression',
:locals => {:expression_type => :filter_expression,
:action => "rbac_group_edit",
:id => @edit[:group_id] || 'new',
:title => _('Expression')}
5 changes: 3 additions & 2 deletions app/views/layouts/_role_enablement_expression.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
= render :partial => 'layouts/custom_button_expression',
= render :partial => 'layouts/filter_expression',
:locals => {:expression_type => :enablement_expression,
:title => _('Expression')}
:action => "button_update",
:title => _('Expression')}
5 changes: 3 additions & 2 deletions app/views/layouts/_role_visibility_expression.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
= render :partial => 'layouts/custom_button_expression',
= render :partial => 'layouts/filter_expression',
:locals => {:expression_type => :visibility_expression,
:title => _('Expression')}
:action => "button_update",
:title => _('Expression')}
2 changes: 2 additions & 0 deletions app/views/layouts/exp_atom/_editor.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
- opts.push([_('Field'), 'field'])
- unless @edit[@expkey].tags_for_display_filters.empty?
- opts.push([_('Tag'), 'tag'])
- when 'MiqGroup'
- opts += [[_('Tag'), 'tag']]
- else
- opts += exptypes

Expand Down
35 changes: 30 additions & 5 deletions app/views/ops/rbac_group/_customer_tags.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
%br
= _("This user is limited to items with the selected tags.")
%br
%br
= render(:partial => 'shared/tree', :locals => {:tree => @tags_tree, :name => @tags_tree.name})
#customer_tags_div
- if @edit
- rec_id = @edit && @edit[:group_id] ? @edit[:group_id] : "new"
- url = url_for(:action => 'rbac_group_field_changed', :id => rec_id)
%br
.form-horizontal
.form-group
%label.col-md-2.control-label
= _("This user is limited to ")
.col-md-8
- if @edit
= select_tag('use_filter_expression',
options_for_select([[_("Specific Tags"), false],
[_("Tags Based On Expression"), true]],
@edit[:new][:use_filter_expression]),
:class => "selectpicker")
:javascript
miqInitSelectPicker();
miqSelectPickerEvent('use_filter_expression', "#{url}")
- if @edit[:new][:use_filter_expression]
= render(:partial => "layouts/group_filter_expression")
- else
= render(:partial => 'shared/tree', :locals => {:tree => @tags_tree, :name => @tags_tree.name})
- elsif @use_filter_expression
= render(:partial => "layouts/group_filter_expression")
- else
= _("This user is limited to items with the selected tags.")
%br
%br
= render(:partial => 'shared/tree', :locals => {:tree => @tags_tree, :name => @tags_tree.name})
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2704,7 +2704,8 @@
ls_select
ldap_entry_changed
ls_delete
)
) +
exp_post
},

:orchestration_stack => {
Expand Down
40 changes: 28 additions & 12 deletions spec/controllers/ops_controller/ops_rbac_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,38 @@
MiqUserRole.seed
MiqGroup.seed
MiqRegion.seed

stub_user(:features => :all)
@group = FactoryGirl.create(:miq_group)
@role = MiqUserRole.find_by(:name => "EvmRole-operator")
@exp = MiqExpression.new("=" => {:field => "name", :value => "Test"}, :token => 1)
end

it "does not display tenant default groups in Edit Sequence" do
tg = FactoryGirl.create(:tenant).default_miq_group
g = FactoryGirl.create(:miq_group)
it "saves the filters when use_filter_expression is false" do
@group.entitlement = Entitlement.create!
controller.instance_variable_set(:@edit, :new => {:use_filter_expression => false,
:name => 'Name',
:description => "Test",
:role => @role.id,
:filter_expression => @exp.exp,
:belongsto => {},
:filters => {'managed/application/abrt' => '/managed/application/abrt'}})
controller.send(:rbac_group_set_record_vars, @group)
expect(@group.entitlement.filter_expression).to be_nil
expect(@group.entitlement.get_managed_filters).to match([["/managed/application/abrt"]])
end

expect(MiqGroup.tenant_groups).to include(tg)
expect(MiqGroup.tenant_groups).not_to include(g)
session[:sandboxes] = {"ops" => {:active_tree => :rbac_tree}}
allow(controller).to receive(:replace_right_cell)
controller.send(:rbac_group_seq_edit)
expect(response.status).to eq(200)
edit = controller.instance_variable_get(:@edit)
expect(edit[:current][:ldap_groups].find { |lg| lg.group_type == 'tenant' }).to be(nil)
expect(edit[:current][:ldap_groups].find { |lg| lg.group_type == 'user' }).not_to be(nil)
it "saves the filter_expression when use_filter_expression true" do
controller.instance_variable_set(:@edit, :new => {:use_filter_expression => true,
:name => 'Name',
:description => "Test",
:role => @role.id,
:filter_expression => @exp.exp,
:belongsto => {},
:filters => {'managed/application/abrt' => '/managed/application/abrt'}})
controller.send(:rbac_group_set_record_vars, @group)
expect(@group.entitlement.get_managed_filters).to eq([])
expect(@group.entitlement.filter_expression.exp).to match(@exp.exp)
end
end

Expand Down

0 comments on commit bdef0ac

Please sign in to comment.