Updates latest examine + api updates + fixes tests, gets different validation working (valid, failed, filtered)
This commit is contained in:
@@ -12,7 +12,7 @@ namespace Umbraco.Examine
|
||||
/// <summary>
|
||||
/// Used to validate a ValueSet for content/media - based on permissions, parent id, etc....
|
||||
/// </summary>
|
||||
public class ContentValueSetValidator : ValueSetValidator
|
||||
public class ContentValueSetValidator : ValueSetValidator, IContentValueSetValidator
|
||||
{
|
||||
private readonly IPublicAccessService _publicAccessService;
|
||||
|
||||
@@ -24,6 +24,46 @@ namespace Umbraco.Examine
|
||||
public bool SupportProtectedContent { get; }
|
||||
public int? ParentId { get; }
|
||||
|
||||
public bool ValidatePath(string path, string category)
|
||||
{
|
||||
//check if this document is a descendent of the parent
|
||||
if (ParentId.HasValue && ParentId.Value > 0)
|
||||
{
|
||||
// we cannot return FAILED here because we need the value set to get into the indexer and then deal with it from there
|
||||
// because we need to remove anything that doesn't pass by parent Id in the cases that umbraco data is moved to an illegal parent.
|
||||
if (!path.Contains(string.Concat(",", ParentId.Value, ",")))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ValidateRecycleBin(string path, string category)
|
||||
{
|
||||
var recycleBinId = category == IndexTypes.Content ? Constants.System.RecycleBinContent : Constants.System.RecycleBinMedia;
|
||||
|
||||
//check for recycle bin
|
||||
if (!SupportUnpublishedContent)
|
||||
{
|
||||
if (path.Contains(string.Concat(",", recycleBinId, ",")))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ValidateProtectedContent(string path, string category)
|
||||
{
|
||||
if (category == IndexTypes.Content
|
||||
&& !SupportProtectedContent
|
||||
//if the service is null we can't look this up so we'll return false
|
||||
&& (_publicAccessService == null || _publicAccessService.IsProtected(path)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public ContentValueSetValidator(bool supportUnpublishedContent, int? parentId = null,
|
||||
IEnumerable<string> includeItemTypes = null, IEnumerable<string> excludeItemTypes = null)
|
||||
: this(supportUnpublishedContent, true, null, parentId, includeItemTypes, excludeItemTypes)
|
||||
@@ -41,19 +81,22 @@ namespace Umbraco.Examine
|
||||
_publicAccessService = publicAccessService;
|
||||
}
|
||||
|
||||
public override bool Validate(ValueSet valueSet)
|
||||
public override ValueSetValidationResult Validate(ValueSet valueSet)
|
||||
{
|
||||
if (!base.Validate(valueSet))
|
||||
return false;
|
||||
var baseValidate = base.Validate(valueSet);
|
||||
if (baseValidate == ValueSetValidationResult.Failed)
|
||||
return ValueSetValidationResult.Failed;
|
||||
|
||||
var isFiltered = baseValidate == ValueSetValidationResult.Filtered;
|
||||
|
||||
//check for published content
|
||||
if (valueSet.Category == IndexTypes.Content && !SupportUnpublishedContent)
|
||||
{
|
||||
if (!valueSet.Values.TryGetValue(UmbracoExamineIndexer.PublishedFieldName, out var published))
|
||||
return false;
|
||||
return ValueSetValidationResult.Failed;
|
||||
|
||||
if (!published[0].Equals(1))
|
||||
return false;
|
||||
return ValueSetValidationResult.Failed;
|
||||
|
||||
//deal with variants, if there are unpublished variants than we need to remove them from the value set
|
||||
if (valueSet.Values.TryGetValue(UmbracoContentIndexer.VariesByCultureFieldName, out var variesByCulture)
|
||||
@@ -69,6 +112,7 @@ namespace Umbraco.Examine
|
||||
foreach (var cultureField in valueSet.Values.Where(x => x.Key.InvariantEndsWith(cultureSuffix)).ToList())
|
||||
{
|
||||
valueSet.Values.Remove(cultureField.Key);
|
||||
isFiltered = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,37 +120,21 @@ namespace Umbraco.Examine
|
||||
}
|
||||
|
||||
//must have a 'path'
|
||||
if (!valueSet.Values.TryGetValue(PathKey, out var pathValues)) return false;
|
||||
if (pathValues.Count == 0) return false;
|
||||
if (pathValues[0] == null) return false;
|
||||
if (pathValues[0].ToString().IsNullOrWhiteSpace()) return false;
|
||||
if (!valueSet.Values.TryGetValue(PathKey, out var pathValues)) return ValueSetValidationResult.Failed;
|
||||
if (pathValues.Count == 0) return ValueSetValidationResult.Failed;
|
||||
if (pathValues[0] == null) return ValueSetValidationResult.Failed;
|
||||
if (pathValues[0].ToString().IsNullOrWhiteSpace()) return ValueSetValidationResult.Failed;
|
||||
var path = pathValues[0].ToString();
|
||||
|
||||
// return nothing if we're not supporting protected content and it is protected, and we're not supporting unpublished content
|
||||
if (valueSet.Category == IndexTypes.Content
|
||||
&& !SupportProtectedContent
|
||||
//if the service is null we can't look this up so we'll return false
|
||||
&& (_publicAccessService == null || _publicAccessService.IsProtected(path)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// We need to validate the path of the content based on ParentId, protected content and recycle bin rules.
|
||||
// We cannot return FAILED here because we need the value set to get into the indexer and then deal with it from there
|
||||
// because we need to remove anything that doesn't pass by protected content in the cases that umbraco data is moved to an illegal parent.
|
||||
if (!ValidatePath(path, valueSet.Category)
|
||||
|| !ValidateRecycleBin(path, valueSet.Category)
|
||||
|| !ValidateProtectedContent(path, valueSet.Category))
|
||||
return ValueSetValidationResult.Filtered;
|
||||
|
||||
//check if this document is a descendent of the parent
|
||||
if (ParentId.HasValue && ParentId.Value > 0)
|
||||
{
|
||||
if (!path.Contains(string.Concat(",", ParentId.Value, ",")))
|
||||
return false;
|
||||
}
|
||||
|
||||
//check for recycle bin
|
||||
if (!SupportUnpublishedContent)
|
||||
{
|
||||
var recycleBinId = valueSet.Category == IndexTypes.Content ? Constants.System.RecycleBinContent : Constants.System.RecycleBinMedia;
|
||||
if (path.Contains(string.Concat(",", recycleBinId, ",")))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return isFiltered ? ValueSetValidationResult.Filtered: ValueSetValidationResult.Valid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user