From fd26565b1467408a4105ac8e19b9b14c90b9ed61 Mon Sep 17 00:00:00 2001 From: LP B Date: Mon, 23 Sep 2024 17:54:32 +0200 Subject: [PATCH] fix(app/templates): non admins cannot load templates list (#12235) --- .../app-templates/AppTemplatesView.tsx | 2 +- .../app-templates/queries/useAppTemplates.ts | 26 +++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/react/portainer/templates/app-templates/AppTemplatesView.tsx b/app/react/portainer/templates/app-templates/AppTemplatesView.tsx index 91d363ca4..18aee72a5 100644 --- a/app/react/portainer/templates/app-templates/AppTemplatesView.tsx +++ b/app/react/portainer/templates/app-templates/AppTemplatesView.tsx @@ -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 diff --git a/app/react/portainer/templates/app-templates/queries/useAppTemplates.ts b/app/react/portainer/templates/app-templates/queries/useAppTemplates.ts index 5f6fe1da9..f8337c4dd 100644 --- a/app/react/portainer/templates/app-templates/queries/useAppTemplates.ts +++ b/app/react/portainer/templates/app-templates/queries/useAppTemplates.ts @@ -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>({ + environmentId, select, enabled = true, }: { + environmentId?: EnvironmentId; select?: (templates: Array) => 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(