Skip to content

Commit

Permalink
Fixes #36751 - check permissions for schedule job button
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaAga authored and adamruzicka committed Oct 3, 2023
1 parent 0b75d16 commit 5bf78eb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def update
process_response @remote_execution_feature.update(remote_execution_feature_params)
end

api :GET, '/api/hosts/:id/available_remote_execution_features', N_('List available remote execution features for a host')
param :id, :identifier, :required => true
def available_remote_execution_features
host = Host.find(params[:id])
@remote_execution_features = resource_scope
@permissions = {:can_run_job => (authorized_for(controller: :job_invocations, action: :create) && (!host.infrastructure_host? || User.current.can?(:execute_jobs_on_infrastructure_hosts))) }
end

private

def parent_scope
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node :permissions do
@permissions
end
child @remote_execution_features do
extends 'api/v2/remote_execution_features/main'
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
resources :foreign_input_sets, :only => [:index, :show, :create, :destroy, :update]
end

get 'hosts/:id/available_remote_execution_features', to: 'remote_execution_features#available_remote_execution_features'

resources :remote_execution_features, :only => [:show, :index, :update]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/foreman_remote_execution/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Engine < ::Rails::Engine
:'api/v2/template_inputs' => [:create, :update, :destroy],
:'api/v2/foreign_input_sets' => [:create, :update, :destroy]}, :resource_type => 'JobTemplate'
permission :edit_remote_execution_features, { :remote_execution_features => [:index, :show, :update],
:'api/v2/remote_execution_features' => [:index, :show, :update]}, :resource_type => 'RemoteExecutionFeature'
:'api/v2/remote_execution_features' => [:index, :show, :update, :available_remote_execution_features]}, :resource_type => 'RemoteExecutionFeature'
permission :destroy_job_templates, { :job_templates => [:destroy],
:'api/v2/job_templates' => [:destroy] }, :resource_type => 'JobTemplate'
permission :lock_job_templates, { :job_templates => [:lock, :unlock] }, :resource_type => 'JobTemplate'
Expand Down
3 changes: 2 additions & 1 deletion webpack/react_app/components/FeaturesDropdown/constant.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const REX_FEATURES_API = '/api/remote_execution_features';
export const REX_FEATURES_API = host =>
`/api/v2/hosts/${host}/available_remote_execution_features`;
export const NEW_JOB_PAGE = '/job_invocations/new?host_ids%5B%5D';
16 changes: 11 additions & 5 deletions webpack/react_app/components/FeaturesDropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ import { runFeature } from './actions';

const FeaturesDropdown = ({ hostId }) => {
const [isOpen, setIsOpen] = useState(false);
const {
response: { results: features },
status,
} = useAPI('get', foremanUrl(REX_FEATURES_API));

const { response, status } = useAPI(
'get',
foremanUrl(REX_FEATURES_API(hostId))
);
const dispatch = useDispatch();
// eslint-disable-next-line camelcase
const canRunJob = response?.permissions?.can_run_job;
if (!canRunJob) {
return null;
}
// eslint-disable-next-line camelcase
const features = response?.remote_execution_features;
const dropdownItems = features
?.filter(feature => feature.host_action_button)
?.map(({ name, label, id, description }) => (
Expand Down

0 comments on commit 5bf78eb

Please sign in to comment.