Skip to content

Commit

Permalink
Refs #36887 - hide dropdown based on permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Nov 6, 2023
1 parent 31e8636 commit fb7f2eb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/views/api/v2/host/main.rabl
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
attributes :cockpit_url

extends "api/v2/host/permissions"
7 changes: 7 additions & 0 deletions app/views/api/v2/host/permissions.rabl
Original file line number Diff line number Diff line change
@@ -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
27 changes: 23 additions & 4 deletions webpack/react_app/components/FeaturesDropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ 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
? REX_FEATURES_HOST_URL(hostId)
: 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;
}

Expand Down Expand Up @@ -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;

0 comments on commit fb7f2eb

Please sign in to comment.