Skip to content

Commit b4c8f8d

Browse files
Fix button form change field validation
1 parent ab2ae8c commit b4c8f8d

File tree

1 file changed

+83
-2
lines changed

1 file changed

+83
-2
lines changed

app/controllers/application_controller/buttons.rb

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ def group_update
100100
# we need a "Provider" so adding an "ExtManagementSystem" to the list.
101101
MODEL_WITH_OPEN_URL = %w[ExtManagementSystem Service User MiqGroup Tenant CloudTenant GenericObject Vm].freeze
102102

103+
# Types of visibility for the custom buttons
104+
VISIBILITY_TYPES = {'role' => 'role', 'all' => 'all'}.freeze
105+
106+
def button_visibility_box_edit
107+
typ_changed = params[:visibility_typ].present?
108+
@edit[:new][:visibility_typ] = VISIBILITY_TYPES[params[:visibility_typ]] if typ_changed
109+
110+
visibility_typ = @edit[:new][:visibility_typ]
111+
if %w[role].include?(visibility_typ)
112+
plural = visibility_typ.pluralize
113+
key = plural.to_sym
114+
prefix = "#{plural}_"
115+
116+
@edit[:new][key] = [] if typ_changed
117+
params.each do |var, value|
118+
next unless var.starts_with?(prefix)
119+
120+
name = var.split(prefix).last.to_i
121+
if value == "1"
122+
@edit[:new][key] |= [name] # union
123+
elsif value.downcase == "null"
124+
@edit[:new][key].delete(name)
125+
end
126+
end
127+
else
128+
@edit[:new][:roles] ||= []
129+
@edit[:new][:roles] |= ["_ALL_"]
130+
end
131+
end
132+
103133
def automate_button_field_changed
104134
assert_privileges(feature_by_action)
105135

@@ -125,7 +155,7 @@ def automate_button_field_changed
125155
@edit[:new][:dialog_id] = nil if params[:display_for].present? && params[:display_for] != 'single'
126156

127157
@edit[:new][:dialog_id] = params[:dialog_id] == "" ? nil : params[:dialog_id] if params.key?("dialog_id")
128-
visibility_box_edit
158+
button_visibility_box_edit
129159

130160
if params[:button_type] == 'default'
131161
clear_playbook_variables
@@ -147,7 +177,58 @@ def automate_button_field_changed
147177
page.replace("form_role_visibility", :partial => "layouts/role_visibility", :locals => {:rec_id => (@custom_button.id || "new").to_s, :action => "automate_button_field_changed"})
148178
end
149179
unless params[:target_class]
150-
@changed = session[:changed] = (@edit[:new] != @edit[:current])
180+
@changed = session[:changed] = false
181+
182+
# This block checks if any of the fields have changed
183+
# Broken into different branches for specific fields
184+
# If no fields are changed it will not set @changed or session[:changed] to true
185+
@edit[:new].each_key do |key|
186+
if @edit[:new][key] != @edit[:current][key]
187+
if @edit[:new][key] == "" && @edit[:current][key].nil? # check empty string / nil case
188+
next
189+
elsif @edit[:new][key] == @edit[:current][key].to_s # check string / integer case
190+
next
191+
elsif key == :roles # check role values
192+
if (@edit[:new][key] == ["_ALL_"] || @edit[:new][key] == []) && (@edit[:current][key].nil? || @edit[:current][key] == ["all"] || @edit[:current][key] == ["_ALL_"]) # if visibility is set to "To All"
193+
next
194+
elsif @edit[:new][key].sort == @edit[:current][key].sort # check if new roles array and current roles array are equal after sorting the ids
195+
next
196+
else # if new roles array and current roles array are not equal
197+
@changed = session[:changed] = true
198+
break
199+
200+
end
201+
elsif key == :attrs # check attribute values
202+
@edit[:new][key].each_with_index do |_item, index|
203+
if (@edit[:new][:attrs][index] == [""] || @edit[:new][:attrs][index] == ["", ""]) && @edit[:current][:attrs][index] == [] # check if attribute and value field is empty
204+
next
205+
elsif @edit[:new][:attrs][index] == [] && @edit[:current][:attrs][index] == [] # check if attribute or value field is empty
206+
next
207+
else # if new attribute array and current attribute array are not equal
208+
@changed = session[:changed] = true
209+
break
210+
211+
end
212+
end
213+
214+
elsif key == :visibility_typ # check visibility type
215+
if @edit[:new][key] == "all" && @edit[:current][key].nil? # if visibility is set to "To All"
216+
next
217+
elsif @edit[:new][key] == "role" && @edit[:new][:roles] == [] # if visibility type is set to "To Role" and no roles are selected
218+
next
219+
else # if new visibility type value and current visibility type value are not equal
220+
@changed = session[:changed] = true
221+
break
222+
223+
end
224+
else # if new value and current value are not equal
225+
@changed = session[:changed] = true
226+
break
227+
228+
end
229+
end
230+
end
231+
151232
page << javascript_for_miq_button_visibility(@changed)
152233
end
153234
page << "miqSparkle(false);"

0 commit comments

Comments
 (0)