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.
This commit is contained in:
Andrea Piovanelli
2022-06-17 08:50:43 +02:00
committed by GitHub
parent aff9b2542c
commit 520dc06b46

View File

@@ -169,6 +169,10 @@ namespace Orchard.Taxonomies.Services {
}
public IEnumerable<TermPart> GetTerms(int taxonomyId) {
// If taxonomyId isn't valid, return a empty list without executing the query.
if (taxonomyId <= 0) {
return Array.Empty<TermPart>();
}
var result = GetTermsQuery(taxonomyId)
.OrderBy(x => x.FullWeight)
.List();
@@ -177,6 +181,10 @@ namespace Orchard.Taxonomies.Services {
}
public IEnumerable<TermPart> GetRootTerms(int taxonomyId) {
// If taxonomyId isn't valid, return a empty list without executing the query.
if (taxonomyId <= 0) {
return Array.Empty<TermPart>();
}
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<TitlePartRecord>()
.Where(r => r.Title == name)