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 636b161
Show file tree
Hide file tree
Showing 3 changed files with 30 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"

Check failure on line 3 in app/views/api/v2/host/main.rabl

View workflow job for this annotation

GitHub Actions / rubocop

Layout/TrailingEmptyLines: Final newline missing.
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

Check failure on line 7 in app/views/api/v2/host/permissions.rabl

View workflow job for this annotation

GitHub Actions / rubocop

Layout/TrailingEmptyLines: Final newline missing.
25 changes: 21 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);

Check failure on line 42 in webpack/react_app/components/FeaturesDropdown/index.js

View workflow job for this annotation

GitHub Actions / test_js (12)

'hostResponse.response' is missing in props validation

Check failure on line 42 in webpack/react_app/components/FeaturesDropdown/index.js

View workflow job for this annotation

GitHub Actions / test_js (12)

Replace `result·=>·result.can_create_job_invocations` with `⏎········result·=>·result.can_create_job_invocations⏎······`
console.log(hostResponse?.response?.results)

Check failure on line 43 in webpack/react_app/components/FeaturesDropdown/index.js

View workflow job for this annotation

GitHub Actions / test_js (12)

Replace `····console.log(hostResponse?.response?.results)` with `··console.log(hostResponse?.response?.results);`

Check warning on line 43 in webpack/react_app/components/FeaturesDropdown/index.js

View workflow job for this annotation

GitHub Actions / test_js (12)

Unexpected console statement

Check failure on line 43 in webpack/react_app/components/FeaturesDropdown/index.js

View workflow job for this annotation

GitHub Actions / test_js (12)

'hostResponse.response' is missing in props validation
console.log(hostResponse?.results?.some(result => result.can_create_job_invocations));

Check failure on line 44 in webpack/react_app/components/FeaturesDropdown/index.js

View workflow job for this annotation

GitHub Actions / test_js (12)

Replace `····console.log(hostResponse?.results?.some(result·=>·result.can_create_job_invocations)` with `··console.log(⏎····hostResponse?.results?.some(result·=>·result.can_create_job_invocations)⏎··`

Check warning on line 44 in webpack/react_app/components/FeaturesDropdown/index.js

View workflow job for this annotation

GitHub Actions / test_js (12)

Unexpected console statement
if (!canRunJob) {
return null;
}

Expand Down Expand Up @@ -92,11 +101,19 @@ FeaturesDropdown.propTypes = {
hostId: PropTypes.number,
hostSearch: PropTypes.string,
selectedCount: PropTypes.number,
hostResponse: 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 636b161

Please sign in to comment.