From fdd71feb3b12cf17dc86f35d3afc6ebfcbf2daaa Mon Sep 17 00:00:00 2001 From: Jeremy Lenz Date: Mon, 6 Nov 2023 17:43:16 -0500 Subject: [PATCH] Refs #36887 - hide dropdown based on permissions --- app/views/api/v2/host/main.rabl | 2 ++ app/views/api/v2/host/permissions.rabl | 7 +++++ .../components/FeaturesDropdown/index.js | 27 ++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 app/views/api/v2/host/permissions.rabl diff --git a/app/views/api/v2/host/main.rabl b/app/views/api/v2/host/main.rabl index a6db41c11..c364f0df3 100644 --- a/app/views/api/v2/host/main.rabl +++ b/app/views/api/v2/host/main.rabl @@ -1 +1,3 @@ attributes :cockpit_url + +extends "api/v2/host/permissions" diff --git a/app/views/api/v2/host/permissions.rabl b/app/views/api/v2/host/permissions.rabl new file mode 100644 index 000000000..6dd18a1f9 --- /dev/null +++ b/app/views/api/v2/host/permissions.rabl @@ -0,0 +1,7 @@ +if params.has_key?(:include_permissions) + node do |resource| + if resource&.class&.try(:include?, Authorizable) + node(:can_create_job_invocations) { authorized_for(:auth_object => resource, :authorizer => authorizer, :permission => "create_job_invocations") } + end + end +end diff --git a/webpack/react_app/components/FeaturesDropdown/index.js b/webpack/react_app/components/FeaturesDropdown/index.js index 82f0cc490..ee1a16404 100644 --- a/webpack/react_app/components/FeaturesDropdown/index.js +++ b/webpack/react_app/components/FeaturesDropdown/index.js @@ -23,7 +23,12 @@ import { import { runFeature } from './actions'; import './index.scss'; -const FeaturesDropdown = ({ hostId, hostSearch, selectedCount }) => { +const FeaturesDropdown = ({ + hostId, + hostSearch, + hostResponse, + selectedCount, +}) => { const [isOpen, setIsOpen] = useState(false); const isSingleHost = !!hostId; // identifies whether we're on the host details or host overview page const rexFeaturesUrl = isSingleHost @@ -31,9 +36,13 @@ const FeaturesDropdown = ({ hostId, hostSearch, selectedCount }) => { : ALL_REX_FEATURES_URL; const { response, status } = useAPI('get', foremanUrl(rexFeaturesUrl)); const dispatch = useDispatch(); - // eslint-disable-next-line camelcase - const canRunJob = response?.permissions?.can_run_job; - if (isSingleHost && !canRunJob) { + const canRunJob = isSingleHost + ? // eslint-disable-next-line camelcase + response?.permissions?.can_run_job + : hostResponse?.response?.results?.some( + result => result.can_create_job_invocations + ); + if (!canRunJob) { return null; } @@ -92,11 +101,21 @@ FeaturesDropdown.propTypes = { hostId: PropTypes.number, hostSearch: PropTypes.string, selectedCount: PropTypes.number, + hostResponse: PropTypes.shape({ + response: PropTypes.shape({ + results: PropTypes.arrayOf( + PropTypes.shape({ + can_create_job_invocations: PropTypes.bool, + }) + ), + }), + }), }; FeaturesDropdown.defaultProps = { hostId: undefined, hostSearch: undefined, selectedCount: 0, + hostResponse: undefined, }; export default FeaturesDropdown;