fix(app/templates): non admins cannot load templates list (#12235)

pull/12251/head
LP B 2024-09-23 17:54:32 +02:00 committed by GitHub
parent e0b6f2283a
commit fd26565b14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 9 deletions

View File

@ -22,7 +22,7 @@ export function AppTemplatesView() {
'template',
(param) => (param ? parseInt(param, 10) : 0)
);
const templatesQuery = useAppTemplates();
const templatesQuery = useAppTemplates({ environmentId: envId });
const selectedTemplate = selectedTemplateId
? templatesQuery.data?.find(
(template) => template.Id === selectedTemplateId

View File

@ -4,6 +4,8 @@ import axios, { parseAxiosError } from '@/portainer/services/axios';
import { useRegistries } from '@/react/portainer/registries/queries/useRegistries';
import { DockerHubViewModel } from '@/portainer/models/dockerhub';
import { Registry } from '@/react/portainer/registries/types/registry';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { useEnvironmentRegistries } from '@/react/portainer/environments/queries/useEnvironmentRegistries';
import { AppTemplate } from '../types';
import { TemplateViewModel } from '../view-model';
@ -11,22 +13,30 @@ import { TemplateViewModel } from '../view-model';
import { buildUrl } from './build-url';
export function useAppTemplates<T = Array<TemplateViewModel>>({
environmentId,
select,
enabled = true,
}: {
environmentId?: EnvironmentId;
select?: (templates: Array<TemplateViewModel>) => T;
enabled?: boolean;
} = {}) {
const registriesQuery = useRegistries({ enabled });
const hasEnvParam = !!environmentId;
return useQuery(
['templates'],
() => getTemplatesWithRegistry(registriesQuery.data),
{
enabled: !!registriesQuery.data && enabled,
select,
}
const environmentRegistriesQuery = useEnvironmentRegistries(
environmentId || 0,
{ enabled: enabled && hasEnvParam }
);
const registriesQuery = useRegistries({ enabled: enabled && !hasEnvParam });
const data = hasEnvParam
? environmentRegistriesQuery.data
: registriesQuery.data;
return useQuery(['templates'], () => getTemplatesWithRegistry(data), {
enabled: !!data && enabled,
select,
});
}
export function useAppTemplate(