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

Convert role form from ruby into React #9281

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions app/controllers/ops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,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,15 +679,45 @@ 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")

case key
when :user
return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")
record = @edit[:user_id] ? User.find_by(:id => @edit[:user_id]) : User.new
validated = rbac_user_validate?
rbac_user_set_record_vars(record)
Expand All @@ -668,6 +727,7 @@ def rbac_edit_save_or_add(what, rbac_suffix = what)
rbac_group_set_record_description_role(record) # Set new Description, Role and Tenant for a new Group
rbac_group_set_record_vars(record) if validated
when :role then
return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")
record = @edit[:role_id] ? MiqUserRole.find_by(:id => @edit[:role_id]) : MiqUserRole.new
validated = rbac_role_validate?
rbac_role_set_record_vars(record)
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
Loading