From 520dc06b46a3ed3579fe645807a97c0e9dd6e592 Mon Sep 17 00:00:00 2001 From: Andrea Piovanelli <83577153+AndreaPiovanelliLaser@users.noreply.github.com> Date: Fri, 17 Jun 2022 08:50:43 +0200 Subject: [PATCH] 8557 check valid taxonomy (#8558) * Added checks on taxonomyId validity (it needs to be > 0) before executing queries that would return no result. * Added id check on GetTaxonomy(int id) functions. * Added id check on GetTerm(int id) * Removed id check on GetTaxonomy(int) function to avoid changing previous behaviour. * Returned "Array.Empty" instead of "new List" where an empty list must be returned. --- .../Services/TaxonomyService.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs index 36a4f3d24..485a050b8 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Services/TaxonomyService.cs @@ -169,6 +169,10 @@ namespace Orchard.Taxonomies.Services { } public IEnumerable GetTerms(int taxonomyId) { + // If taxonomyId isn't valid, return a empty list without executing the query. + if (taxonomyId <= 0) { + return Array.Empty(); + } var result = GetTermsQuery(taxonomyId) .OrderBy(x => x.FullWeight) .List(); @@ -177,6 +181,10 @@ namespace Orchard.Taxonomies.Services { } public IEnumerable GetRootTerms(int taxonomyId) { + // If taxonomyId isn't valid, return a empty list without executing the query. + if (taxonomyId <= 0) { + return Array.Empty(); + } var result = GetTermsQuery(taxonomyId) .Where(x => x.Path == "/") .OrderBy(x => x.FullWeight) @@ -202,11 +210,19 @@ namespace Orchard.Taxonomies.Services { } public int GetTermsCount(int taxonomyId) { + // If taxonomyId isn't valid, return 0 without executing the query. + if (taxonomyId <= 0) { + return 0; + } return GetTermsQuery(taxonomyId) .Count(); } public TermPart GetTerm(int id) { + // If term id isn't valid, return null without executing the query. + if (id <= 0) { + return null; + } return GetTermsQuery() .Where(x => x.Id == id).List().FirstOrDefault(); } @@ -231,6 +247,10 @@ namespace Orchard.Taxonomies.Services { } public TermPart GetTermByName(int taxonomyId, string name) { + // If taxonomyId isn't valid, return null without executing the query. + if (taxonomyId <= 0) { + return null; + } return GetTermsQuery(taxonomyId) .Join() .Where(r => r.Title == name)