Skip to content

Commit

Permalink
Fixes #36892 - Pass host results to REX slot;
Browse files Browse the repository at this point in the history
  allow root node permissions to be extended
Refs #36892 - improvements to regex & index layout
  • Loading branch information
jeremylenz committed Nov 20, 2023
1 parent b1883f4 commit 23dde58
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
16 changes: 15 additions & 1 deletion app/controllers/api/v2/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BaseController < Api::BaseController
layout 'api/v2/layouts/index_layout', :only => :index

helper_method :root_node_name, :metadata_total, :metadata_subtotal, :metadata_search,
:metadata_order, :metadata_by, :metadata_page, :metadata_per_page
:metadata_order, :metadata_by, :metadata_page, :metadata_per_page, :index_node_permissions_snippet
def root_node_name
@root_node_name ||= if Rabl.configuration.use_controller_name_as_json_root
controller_name.split('/').last
Expand All @@ -58,6 +58,20 @@ def root_node_name
end
end

# may be extended by plugins to add additional permissions
def index_node_permissions
{
:can_create => can_create?,
:can_edit => authorized_for(:controller => controller_name, :action => 'edit'),
}
end

def index_node_permissions_snippet
# Remove the characters '{' from the beginning and '}' from the end of the string
# The {1}'s are to only remove one character from each end.
index_node_permissions.to_json.gsub(/\A{{1}|}{1}\z/, '')
end

def metadata_total
@total ||= resource_scope.try(:size).to_i
end
Expand Down
3 changes: 1 addition & 2 deletions app/views/api/v2/layouts/index_layout.json.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"per_page": <%= metadata_per_page.to_json %>,
"search": <%= metadata_search.to_json.html_safe %>,
<% if params.has_key?(:include_permissions) %>
"can_create": <%= can_create?.to_json %>,
"can_edit": <%= can_edit?.to_json %>,
<%= index_node_permissions_snippet.html_safe %>,
<% end %>
"sort": {
"by": <%= metadata_by.to_json.html_safe %>,
Expand Down
12 changes: 12 additions & 0 deletions test/controllers/api/v2/base_controller_subclass_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,16 @@ def setup
msg << "There may be more information in the server's logs."
assert_equal JSON.parse(response.body)['error']['message'], msg
end

test "adds permissions to index node" do
@controller.stubs(:index_node_permissions).returns({:test_permission => true})
result = @controller.index_node_permissions_snippet
assert_equal result, "\"test_permission\":true"
end

test "correctly handles nested hashes in index_node_permissions" do
@controller.stubs(:index_node_permissions).returns({:test_permission => {:nested => true}})
result = @controller.index_node_permissions_snippet
assert_equal result, "\"test_permission\":{\"nested\":true}"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ const HostsIndex = () => {
<SplitItem>
<Slot
id="_all-hosts-schedule-a-job"
hostSearch={selectedCount && fetchBulkParams()}
hostSearch={selectedCount ? fetchBulkParams() : null}
hostResponse={response}
selectedCount={selectedCount}
/>
</SplitItem>
Expand Down

0 comments on commit 23dde58

Please sign in to comment.