-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Menu Features Tree to use TreeBuilder
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1348623 https://bugzilla.redhat.com/show_bug.cgi?id=1411831 https://www.pivotaltracker.com/story/show/129518309
- Loading branch information
Showing
13 changed files
with
593 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
class TreeBuilderOpsRbacMenu < TreeBuilder | ||
include CompressedIds | ||
|
||
has_kids_for Menu::Section, [:x_get_tree_section_kids] | ||
has_kids_for Menu::Item, [:x_get_tree_item_kids] | ||
has_kids_for MiqProductFeature, [:x_get_tree_feature_kids] | ||
|
||
attr_reader :role, :features, :editable, :root_counter | ||
|
||
def initialize(name, type, sandbox, build, role:, editable: false) | ||
@role = role | ||
@editable = editable | ||
@features = @role.miq_product_features.order(:identifier).pluck(:identifier) | ||
|
||
@root_counter = [] | ||
|
||
super(name, type, sandbox, build) | ||
end | ||
|
||
private | ||
|
||
def filtered_menus | ||
menus = Menu::Manager.map do |section| | ||
next if section.id == :cons && !Settings.product.consumption | ||
next if section.name.nil? | ||
next unless Vmdb::PermissionStores.instance.can?(section.id) | ||
|
||
section | ||
end | ||
|
||
menus | ||
end | ||
|
||
def set_locals_for_render | ||
locals = { | ||
:checkboxes => true, | ||
:three_checks => true, | ||
:check_url => "/ops/rbac_role_field_changed/" | ||
} | ||
|
||
if editable | ||
locals[:oncheck] = "miqOnCheckHandler" | ||
end | ||
|
||
super.merge!(locals) | ||
end | ||
|
||
def x_get_tree_roots(count_only = false, _options) | ||
top_nodes = filtered_menus | ||
top_nodes << MiqProductFeature.find_by_identifier("all_vm_rules") | ||
|
||
count_only_or_objects(count_only, top_nodes) | ||
end | ||
|
||
def x_get_tree_hash_kids(parent, count_only = false) | ||
count_only_or_objects(count_only, parent[:data][:kids]) | ||
end | ||
|
||
def x_get_tree_section_kids(parent, count_only = false) | ||
kids = parent.items.select do |item| | ||
if item.kind_of? Menu::Item | ||
item.feature.present? && MiqProductFeature.feature_exists?(item.feature) | ||
else | ||
true | ||
end | ||
end | ||
|
||
count_only_or_objects(count_only, kids) | ||
end | ||
|
||
# FIXME: This is inefficient, but done to use MiqProductFeature objects rather than hashes | ||
def x_get_tree_item_kids(parent, count_only = false) | ||
if parent_feature = all_features.find_by(:identifier => parent.feature) | ||
details = MiqProductFeature.features[parent.feature] | ||
|
||
kids = parent_feature.children.where(:hidden => [false, nil]).sort_by do |c| | ||
details[:children].index(c.identifier) | ||
end | ||
|
||
count_only_or_objects(count_only, kids) | ||
else | ||
[] | ||
end | ||
end | ||
|
||
def x_get_tree_feature_kids(parent, count_only = false) | ||
count_only_or_objects(count_only, parent.children) | ||
end | ||
|
||
def tree_init_options(_tree_name) | ||
{ | ||
:lazy => false, | ||
:add_root => true, | ||
:role => role, | ||
:features => features, | ||
:editable => editable, | ||
:node_id_prefix => node_id_prefix | ||
} | ||
end | ||
|
||
def root_options | ||
root_node | ||
end | ||
|
||
def root_node | ||
@root_node ||= { | ||
:key => root_key, | ||
:title => root_title, | ||
:tooltip => root_tooltip, | ||
:expand => true, | ||
:cfmeNoClick => true, | ||
:select => root_select_state, | ||
:checkable => editable | ||
} | ||
end | ||
|
||
def root_details | ||
@root_details ||= MiqProductFeature.feature_details(root_feature) | ||
end | ||
|
||
def root_tooltip | ||
_(root_details[:description]) || _(root_details[:name]) | ||
end | ||
|
||
def root_title | ||
_(root_details[:name]) | ||
end | ||
|
||
def root_key | ||
"#{node_id_prefix}__#{root_feature}" | ||
end | ||
|
||
def root_select_state | ||
features.include?(root_feature) || select_state_from_counter | ||
end | ||
|
||
def select_state_from_counter | ||
return false if root_counter.empty? | ||
return true if root_counter.all?{ |n| n } | ||
return 'undefined' if root_counter.any? { |n| n || n == 'undefined' } | ||
|
||
false | ||
end | ||
|
||
def node_id_prefix | ||
role.id ? to_cid(role.id) : "new" | ||
end | ||
|
||
def root_feature | ||
@root_feature ||= MiqProductFeature.feature_root | ||
end | ||
|
||
def all_vm_options | ||
text = _("Access Rules for all Virtual Machines") | ||
checked = features.include?("all_vm_rules") || root_select_state | ||
|
||
{ | ||
:key => "#{node_id_prefix}___tab_all_vm_rules", | ||
:title => text, | ||
:tooltip => text, | ||
:select => checked | ||
} | ||
end | ||
|
||
def all_features | ||
@all_features ||= MiqProductFeature.where(:hidden => [false, nil]).load | ||
end | ||
|
||
def add_to_root_counter(node) | ||
root_counter << node[:select] | ||
end | ||
|
||
def override(node, object, pid, options) | ||
case object | ||
when Menu::Section | ||
add_to_root_counter(node) | ||
when MiqProductFeature | ||
if object.identifier == "all_vm_rules" | ||
node.merge!(all_vm_options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.