Skip to content

Commit

Permalink
Convert role form from haml to React and add cypress testing
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-samuel committed Oct 8, 2024
1 parent d906b9d commit 8a8e60b
Show file tree
Hide file tree
Showing 50 changed files with 2,526 additions and 95 deletions.
4 changes: 4 additions & 0 deletions app/controllers/ops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,10 @@ def handle_bottom_cell(nodetype, presenter, locals)
if ["settings_workers", "diagnostics_cu_repair"].include?(@sb[:active_tab])
presenter.hide(:form_buttons_div)
end

if @hide_bottom_bar
presenter.hide(:form_buttons_div)
end
end

def replace_explorer_trees(replace_trees, presenter)
Expand Down
109 changes: 63 additions & 46 deletions app/controllers/ops_controller/ops_rbac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,52 @@ def rbac_group_edit

def rbac_role_add
assert_privileges("rbac_role_add")
@hide_bottom_bar = true
rbac_edit_reset('new', 'role', MiqUserRole)
end

def rbac_role_add_react
assert_privileges("rbac_role_add")
@hide_bottom_bar = true
if params[:id] != 'new'
rbac_edit_reset_react('edit', 'role', MiqUserRole)
else
rbac_edit_reset_react('new', 'role', MiqUserRole)
end
end

def rbac_role_copy
assert_privileges("rbac_role_copy")
@hide_bottom_bar = true
rbac_edit_reset('copy', 'role', MiqUserRole)
end

def rbac_role_edit
assert_privileges("rbac_role_edit")
@hide_bottom_bar = true
case params[:button]
when 'cancel' then rbac_edit_cancel('role')
when 'save', 'add' then rbac_edit_save_or_add('role', 'miq_user_role')
when 'reset', nil then rbac_edit_reset(params[:typ], 'role', MiqUserRole) # Reset or form load
end
end

def rbac_role_edit_get
assert_privileges("rbac_role_edit")
unless params[:id]
obj = find_checked_items
@_params[:id] = obj[0]
end
@hide_bottom_bar = true
role = MiqUserRole.find_by(id: params[:id])
render :json => {
:name => role.name,
:vm_restriction => role[:settings] && role[:settings][:restrictions][:vms],
:service_template_restriction => role[:settings] && role[:settings][:restrictions][:service_templates],
:miqProductFeatures => role.miq_product_features,
}
end

def rbac_tenant_add
assert_privileges("rbac_tenant_add")
@_params[:typ] = "new"
Expand Down Expand Up @@ -650,12 +679,44 @@ def rbac_edit_reset(operation, what, klass)
replace_right_cell(:nodetype => x_node)
end

def rbac_edit_reset_react(operation, what, klass)
key = what.to_sym
if operation != "new"
record = MiqUserRole.find_by(id: params[:id])
record.miq_product_features = [MiqProductFeature.find_by(:identifier => MiqProductFeature.feature_root)]
end

case operation
when "new"
# create new record
@record = klass.new
if key == :role
@record.miq_product_features = [MiqProductFeature.find_by(:identifier => MiqProductFeature.feature_root)]
end
when "copy"
# copy existing record
@record = record.clone
@record.miq_product_features = record.miq_product_features
@record.read_only = false
else
# use existing record
@record = record
end
@sb[:typ] = operation

rbac_role_set_form_vars
rbac_role_get_form_vars

rbac_edit_save_or_add('role', 'miq_user_role')
end

def rbac_edit_save_or_add(what, rbac_suffix = what)
key = what.to_sym
id = params[:id] || "new"
add_pressed = params[:button] == "add"

return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")

# return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")

case key
when :user
Expand Down Expand Up @@ -1269,7 +1330,6 @@ def rbac_role_set_form_vars
@edit[:new][:vm_restriction] = vmr || :none
str = @record.settings.fetch_path(:restrictions, :service_templates) if @record.settings
@edit[:new][:service_template_restriction] = str || :none
@edit[:new][:features] = rbac_expand_features(@record.miq_product_features.map(&:identifier)).sort

@edit[:current] = copy_hash(@edit[:new])

Expand Down Expand Up @@ -1303,54 +1363,11 @@ def rbac_compact_features(selected, node = nil)
end
end

# Yield all features for given tree node a section or feature
#
# a. special case _tab_all_vm_rules
# b. section node /^_tab_/
# return all features below this section and
# recursively below any nested sections
# and nested features recursively
# c. feature node
# return nested features recursively
#
def recurse_sections_and_features(node)
if /_tab_all_vm_rules$/.match?(node)
MiqProductFeature.feature_children('all_vm_rules').each do |feature|
kids = MiqProductFeature.feature_all_children(feature)
yield feature, [feature] + kids
end
elsif /^_tab_/.match?(node)
section_id = node.split('_tab_').last.to_sym
Menu::Manager.section(section_id).features_recursive.each do |f|
kids = MiqProductFeature.feature_all_children(f)
yield f, [f] + kids
end
else
kids = MiqProductFeature.feature_all_children(node)
yield node, [node] + kids
end
end

def rbac_role_get_form_vars
@edit[:new][:name] = params[:name] if params[:name]
@edit[:new][:vm_restriction] = params[:vm_restriction].to_sym if params[:vm_restriction]
@edit[:new][:service_template_restriction] = params[:service_template_restriction].to_sym if params[:service_template_restriction]

# Add/removed features based on the node that was checked
if params[:check]
node = params[:id].split("__").last # Get the feature of the checked node
if params[:check] == "0" # Unchecked
recurse_sections_and_features(node) do |feature, all|
@edit[:new][:features] -= all # remove the feature + children
rbac_role_remove_parent(feature) # remove all parents above the unchecked tab feature
end
else # Checked
recurse_sections_and_features(node) do |feature, all|
@edit[:new][:features] += all # remove the feature + children
rbac_role_add_parent(feature) # remove all parents above the unchecked tab feature
end
end
end
@edit[:new][:features] = params[:features] if params[:features]
@edit[:new][:features].uniq!
@edit[:new][:features].sort!
end
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/ops_helper/role_rbac_details_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def rbac_role_info_view(role, rbac_menu_tree)
})
end

def rbac_role_product_features(role, rbac_menu_tree)
cells = row_data(_("Product Features (Read Only)"), {:input => 'component', :component => 'TREE_VIEW_REDUX', :props => rbac_menu_tree.locals_for_render, :name => rbac_menu_tree.name})
cells[:cells][:value][:props]
end

def select_tree_node(tree_id)
{
:remote => true,
Expand Down
Loading

0 comments on commit 8a8e60b

Please sign in to comment.