Merge branch 'netcore/members-passwordchange-temp' into netcore/members-roles-save

This commit is contained in:
Emma Garland
2021-03-12 14:39:24 +00:00
363 changed files with 4820 additions and 2115 deletions

View File

@@ -121,10 +121,12 @@ namespace Umbraco.Extensions
if (!app.UmbracoCanBoot())
{
app.UseStaticFiles(); // We need static files to show the nice error page.
app.UseMiddleware<BootFailedMiddleware>();
}
else
{
app.UseMiddleware<PreviewAuthenticationMiddleware>();
app.UseMiddleware<UmbracoRequestMiddleware>();
app.UseMiddleware<MiniProfilerMiddleware>();
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http;
using Umbraco.Cms.Core;
namespace Umbraco.Extensions
{
@@ -23,7 +24,7 @@ namespace Umbraco.Extensions
var builder = new StringBuilder();
foreach (var i in items.Where(i => keysToIgnore.InvariantContains(i.Key) == false))
builder.Append(string.Format("{0}={1}&", i.Key, i.Value));
return builder.ToString().TrimEnd('&');
return builder.ToString().TrimEnd(Constants.CharArrays.Ampersand);
}
/// <summary>

View File

@@ -0,0 +1,537 @@
using System;
using System.Collections.Generic;
using System.Data;
using Examine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.DependencyInjection;
namespace Umbraco.Extensions
{
public static class FriendlyPublishedContentExtensions
{
private static IVariationContextAccessor VariationContextAccessor { get; } =
StaticServiceProvider.Instance.GetRequiredService<IVariationContextAccessor>();
private static IPublishedModelFactory PublishedModelFactory { get; } =
StaticServiceProvider.Instance.GetRequiredService<IPublishedModelFactory>();
private static IPublishedUrlProvider PublishedUrlProvider { get; } =
StaticServiceProvider.Instance.GetRequiredService<IPublishedUrlProvider>();
private static IUserService UserService { get; } =
StaticServiceProvider.Instance.GetRequiredService<IUserService>();
private static IUmbracoContextAccessor UmbracoContextAccessor { get; } =
StaticServiceProvider.Instance.GetRequiredService<IUmbracoContextAccessor>();
private static ISiteDomainHelper SiteDomainHelper { get; } =
StaticServiceProvider.Instance.GetRequiredService<ISiteDomainHelper>();
private static IExamineManager ExamineManager { get; } =
StaticServiceProvider.Instance.GetRequiredService<IExamineManager>();
private static IFileService FileService { get; } =
StaticServiceProvider.Instance.GetRequiredService<IFileService>();
private static IOptions<WebRoutingSettings> WebRoutingSettings { get; } =
StaticServiceProvider.Instance.GetRequiredService<IOptions<WebRoutingSettings>>();
private static IContentTypeService ContentTypeService { get; } =
StaticServiceProvider.Instance.GetRequiredService<IContentTypeService>();
private static IPublishedValueFallback PublishedValueFallback { get; } =
StaticServiceProvider.Instance.GetRequiredService<IPublishedValueFallback>();
private static IPublishedSnapshot PublishedSnapshot => UmbracoContextAccessor.UmbracoContext?.PublishedSnapshot;
private static IMediaTypeService MediaTypeService { get; } =
StaticServiceProvider.Instance.GetRequiredService<IMediaTypeService>();
private static IMemberTypeService MemberTypeService { get; } =
StaticServiceProvider.Instance.GetRequiredService<IMemberTypeService>();
/// <summary>
/// Creates a strongly typed published content model for an internal published content.
/// </summary>
/// <param name="content">The internal published content.</param>
/// <returns>The strongly typed published content model.</returns>
public static IPublishedContent CreateModel(
this IPublishedContent content)
=> content.CreateModel(PublishedModelFactory);
/// <summary>
/// Gets the name of the content item.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="culture">The specific culture to get the name for. If null is used the current culture is used (Default is null).</param>
public static string Name(
this IPublishedContent content,
string culture = null)
=> content.Name(VariationContextAccessor, culture);
/// <summary>
/// Gets the URL segment of the content item.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="culture">The specific culture to get the URL segment for. If null is used the current culture is used (Default is null).</param>
public static string UrlSegment(
this IPublishedContent content,
string culture = null)
=> content.UrlSegment(VariationContextAccessor, culture);
/// <summary>
/// Gets the culture date of the content item.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="culture">The specific culture to get the name for. If null is used the current culture is used (Default is null).</param>
public static DateTime CultureDate(
this IPublishedContent content,
string culture = null)
=> content.CultureDate(VariationContextAccessor, culture);
/// <summary>
/// Returns the current template Alias
/// </summary>
/// <returns>Empty string if none is set.</returns>
public static string GetTemplateAlias(this IPublishedContent content)
=> content.GetTemplateAlias(FileService);
public static bool IsAllowedTemplate(this IPublishedContent content, int templateId)
=> content.IsAllowedTemplate(ContentTypeService, WebRoutingSettings.Value, templateId);
public static bool IsAllowedTemplate(
this IPublishedContent content,
bool disableAlternativeTemplates,
bool validateAlternativeTemplates,
int templateId)
=> content.IsAllowedTemplate(
ContentTypeService,
disableAlternativeTemplates,
validateAlternativeTemplates,
templateId);
public static bool IsAllowedTemplate(
this IPublishedContent content,
bool disableAlternativeTemplates,
bool validateAlternativeTemplates,
string templateAlias)
=> content.IsAllowedTemplate(
FileService,
ContentTypeService,
disableAlternativeTemplates,
validateAlternativeTemplates,
templateAlias);
/// <summary>
/// Gets a value indicating whether the content has a value for a property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="culture">The variation language.</param>
/// <param name="segment">The variation segment.</param>
/// <param name="fallback">Optional fallback strategy.</param>
/// <returns>A value indicating whether the content has a value for the property identified by the alias.</returns>
/// <remarks>Returns true if HasValue is true, or a fallback strategy can provide a value.</remarks>
public static bool HasValue(
this IPublishedContent content,
string alias,
string culture = null,
string segment = null,
Fallback fallback = default)
=>
content.HasValue(PublishedValueFallback, alias, culture, segment, fallback);
/// <summary>
/// Gets the value of a content's property identified by its alias, if it exists, otherwise a default value.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="culture">The variation language.</param>
/// <param name="segment">The variation segment.</param>
/// <param name="fallback">Optional fallback strategy.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, if it exists, otherwise a default value.</returns>
public static object Value(this IPublishedContent content, string alias, string culture = null, string segment = null, Fallback fallback = default, object defaultValue = default)
=> content.Value(PublishedValueFallback, alias, culture, segment, fallback, defaultValue);
/// <summary>
/// Gets the value of a content's property identified by its alias, converted to a specified type.
/// </summary>
/// <typeparam name="T">The target property type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="culture">The variation language.</param>
/// <param name="segment">The variation segment.</param>
/// <param name="fallback">Optional fallback strategy.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, converted to the specified type.</returns>
public static T Value<T>(this IPublishedContent content, string alias, string culture = null, string segment = null, Fallback fallback = default, T defaultValue = default)
=> content.Value<T>(PublishedValueFallback, alias, culture, segment, fallback, defaultValue);
/// <summary>
/// Returns all DescendantsOrSelf of all content referenced
/// </summary>
/// <param name="parentNodes"></param>
/// <param name="docTypeAlias"></param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns></returns>
/// <remarks>
/// This can be useful in order to return all nodes in an entire site by a type when combined with TypedContentAtRoot
/// </remarks>
public static IEnumerable<IPublishedContent> DescendantsOrSelfOfType(
this IEnumerable<IPublishedContent> parentNodes, string docTypeAlias, string culture = null)
=> parentNodes.DescendantsOrSelfOfType(VariationContextAccessor, docTypeAlias, culture);
/// <summary>
/// Returns all DescendantsOrSelf of all content referenced
/// </summary>
/// <param name="parentNodes"></param>
/// <param name="variationContextAccessor">Variation context accessor.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns></returns>
/// <remarks>
/// This can be useful in order to return all nodes in an entire site by a type when combined with TypedContentAtRoot
/// </remarks>
public static IEnumerable<T> DescendantsOrSelf<T>(
this IEnumerable<IPublishedContent> parentNodes,
string culture = null)
where T : class, IPublishedContent
=> parentNodes.DescendantsOrSelf<T>(VariationContextAccessor, culture);
public static IEnumerable<IPublishedContent> Descendants(this IPublishedContent content, string culture = null)
=> content.Descendants(VariationContextAccessor, culture);
public static IEnumerable<IPublishedContent> Descendants(this IPublishedContent content, int level, string culture = null)
=> content.Descendants(VariationContextAccessor, level, culture);
public static IEnumerable<IPublishedContent> DescendantsOfType(this IPublishedContent content,
string contentTypeAlias, string culture = null)
=> content.DescendantsOfType(VariationContextAccessor, contentTypeAlias, culture);
public static IEnumerable<T> Descendants<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
=> content.Descendants<T>(VariationContextAccessor, culture);
public static IEnumerable<T> Descendants<T>(this IPublishedContent content, int level, string culture = null)
where T : class, IPublishedContent
=> content.Descendants<T>(VariationContextAccessor, level, culture);
public static IEnumerable<IPublishedContent> DescendantsOrSelf(this IPublishedContent content, string culture = null)
=> content.DescendantsOrSelf(VariationContextAccessor, culture);
public static IEnumerable<IPublishedContent> DescendantsOrSelf(this IPublishedContent content, int level, string culture = null)
=> content.DescendantsOrSelf(VariationContextAccessor, level, culture);
public static IEnumerable<IPublishedContent> DescendantsOrSelfOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
=> content.DescendantsOrSelfOfType(VariationContextAccessor, contentTypeAlias, culture);
public static IEnumerable<T> DescendantsOrSelf<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
=> content.DescendantsOrSelf<T>(VariationContextAccessor, culture);
public static IEnumerable<T> DescendantsOrSelf<T>(this IPublishedContent content, int level, string culture = null)
where T : class, IPublishedContent
=> content.DescendantsOrSelf<T>(VariationContextAccessor, level, culture);
public static IPublishedContent Descendant(this IPublishedContent content, string culture = null)
=> content.Descendant(VariationContextAccessor, culture);
public static IPublishedContent Descendant(this IPublishedContent content, int level, string culture = null)
=> content.Descendant(VariationContextAccessor, level, culture);
public static IPublishedContent DescendantOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
=> content.DescendantOfType(VariationContextAccessor, contentTypeAlias, culture);
public static T Descendant<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
=> content.Descendant<T>(VariationContextAccessor, culture);
public static T Descendant<T>(this IPublishedContent content, int level, string culture = null)
where T : class, IPublishedContent
=> content.Descendant<T>(VariationContextAccessor, level, culture);
public static IPublishedContent DescendantOrSelf(this IPublishedContent content, string culture = null)
=> content.DescendantOrSelf(VariationContextAccessor, culture);
public static IPublishedContent DescendantOrSelf(this IPublishedContent content, int level, string culture = null)
=> content.DescendantOrSelf(VariationContextAccessor, level, culture);
public static IPublishedContent DescendantOrSelfOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
=> content.DescendantOrSelfOfType(VariationContextAccessor, contentTypeAlias, culture);
public static T DescendantOrSelf<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
=> content.DescendantOrSelf<T>(VariationContextAccessor, culture);
public static T DescendantOrSelf<T>(this IPublishedContent content, int level, string culture = null)
where T : class, IPublishedContent
=> content.DescendantOrSelf<T>(VariationContextAccessor, level, culture);
/// <summary>
/// Gets the children of the content item.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="culture">
/// The specific culture to get the URL children for. Default is null which will use the current culture in <see cref="VariationContext"/>
/// </param>
/// <remarks>
/// <para>Gets children that are available for the specified culture.</para>
/// <para>Children are sorted by their sortOrder.</para>
/// <para>
/// For culture,
/// if null is used the current culture is used.
/// If an empty string is used only invariant children are returned.
/// If "*" is used all children are returned.
/// </para>
/// <para>
/// If a variant culture is specified or there is a current culture in the <see cref="VariationContext"/> then the Children returned
/// will include both the variant children matching the culture AND the invariant children because the invariant children flow with the current culture.
/// However, if an empty string is specified only invariant children are returned.
/// </para>
/// </remarks>
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, string culture = null)
=> content.Children(VariationContextAccessor, culture);
/// <summary>
/// Gets the children of the content, filtered by a predicate.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="predicate">The predicate.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The children of the content, filtered by the predicate.</returns>
/// <remarks>
/// <para>Children are sorted by their sortOrder.</para>
/// </remarks>
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, Func<IPublishedContent, bool> predicate, string culture = null)
=> content.Children(VariationContextAccessor, predicate, culture);
/// <summary>
/// Gets the children of the content, of any of the specified types.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <param name="contentTypeAlias">The content type alias.</param>
/// <returns>The children of the content, of any of the specified types.</returns>
public static IEnumerable<IPublishedContent> ChildrenOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
=> content.ChildrenOfType(VariationContextAccessor, contentTypeAlias, culture);
/// <summary>
/// Gets the children of the content, of a given content type.
/// </summary>
/// <typeparam name="T">The content type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The children of content, of the given content type.</returns>
/// <remarks>
/// <para>Children are sorted by their sortOrder.</para>
/// </remarks>
public static IEnumerable<T> Children<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
=> content.Children<T>(VariationContextAccessor, culture);
public static IPublishedContent FirstChild(this IPublishedContent content, string culture = null)
=> content.FirstChild(VariationContextAccessor, culture);
/// <summary>
/// Gets the first child of the content, of a given content type.
/// </summary>
public static IPublishedContent FirstChildOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
=> content.FirstChildOfType(VariationContextAccessor, contentTypeAlias, culture);
public static IPublishedContent FirstChild(this IPublishedContent content, Func<IPublishedContent, bool> predicate, string culture = null)
=> content.FirstChild(VariationContextAccessor, predicate, culture);
public static IPublishedContent FirstChild(this IPublishedContent content, Guid uniqueId, string culture = null)
=> content.FirstChild(VariationContextAccessor, uniqueId, culture);
public static T FirstChild<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
=> content.FirstChild<T>(VariationContextAccessor, culture);
public static T FirstChild<T>(this IPublishedContent content, Func<T, bool> predicate, string culture = null)
where T : class, IPublishedContent
=> content.FirstChild<T>(VariationContextAccessor, predicate, culture);
/// <summary>
/// Gets the siblings of the content.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The siblings of the content.</returns>
/// <remarks>
/// <para>Note that in V7 this method also return the content node self.</para>
/// </remarks>
public static IEnumerable<IPublishedContent> Siblings(this IPublishedContent content, string culture = null)
=> content.Siblings(PublishedSnapshot, VariationContextAccessor, culture);
/// <summary>
/// Gets the siblings of the content, of a given content type.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="contentTypeAlias">The content type alias.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The siblings of the content, of the given content type.</returns>
/// <remarks>
/// <para>Note that in V7 this method also return the content node self.</para>
/// </remarks>
public static IEnumerable<IPublishedContent> SiblingsOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
=> content.SiblingsOfType(PublishedSnapshot, VariationContextAccessor, contentTypeAlias, culture);
/// <summary>
/// Gets the siblings of the content, of a given content type.
/// </summary>
/// <typeparam name="T">The content type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The siblings of the content, of the given content type.</returns>
/// <remarks>
/// <para>Note that in V7 this method also return the content node self.</para>
/// </remarks>
public static IEnumerable<T> Siblings<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
=> content.Siblings<T>(PublishedSnapshot, VariationContextAccessor, culture);
/// <summary>
/// Gets the siblings of the content including the node itself to indicate the position.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The siblings of the content including the node itself.</returns>
public static IEnumerable<IPublishedContent> SiblingsAndSelf(this IPublishedContent content, string culture = null)
=> content.SiblingsAndSelf(PublishedSnapshot, VariationContextAccessor, culture);
/// <summary>
/// Gets the siblings of the content including the node itself to indicate the position, of a given content type.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <param name="contentTypeAlias">The content type alias.</param>
/// <returns>The siblings of the content including the node itself, of the given content type.</returns>
public static IEnumerable<IPublishedContent> SiblingsAndSelfOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
=> content.SiblingsAndSelfOfType(PublishedSnapshot, VariationContextAccessor, contentTypeAlias, culture);
/// <summary>
/// Gets the siblings of the content including the node itself to indicate the position, of a given content type.
/// </summary>
/// <typeparam name="T">The content type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The siblings of the content including the node itself, of the given content type.</returns>
public static IEnumerable<T> SiblingsAndSelf<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
=> content.SiblingsAndSelf<T>(PublishedSnapshot, VariationContextAccessor, culture);
/// <summary>
/// Gets the url of the content item.
/// </summary>
/// <remarks>
/// <para>If the content item is a document, then this method returns the url of the
/// document. If it is a media, then this methods return the media url for the
/// 'umbracoFile' property. Use the MediaUrl() method to get the media url for other
/// properties.</para>
/// <para>The value of this property is contextual. It depends on the 'current' request uri,
/// if any. In addition, when the content type is multi-lingual, this is the url for the
/// specified culture. Otherwise, it is the invariant url.</para>
/// </remarks>
public static string Url(this IPublishedContent content, string culture = null, UrlMode mode = UrlMode.Default)
=> content.Url(PublishedUrlProvider, culture, mode);
/// <summary>
/// Gets the children of the content in a DataTable.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="variationContextAccessor">Variation context accessor.</param>
/// <param name="contentTypeService">The content type service.</param>
/// <param name="mediaTypeService">The media type service.</param>
/// <param name="memberTypeService">The member type service.</param>
/// <param name="publishedUrlProvider">The published url provider.</param>
/// <param name="contentTypeAliasFilter">An optional content type alias.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The children of the content.</returns>
public static DataTable ChildrenAsTable(this IPublishedContent content, string contentTypeAliasFilter = "", string culture = null)
=> content.ChildrenAsTable(VariationContextAccessor, ContentTypeService, MediaTypeService, MemberTypeService, PublishedUrlProvider, contentTypeAliasFilter, culture);
/// <summary>
/// Gets the url for a media.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="culture">The culture (use current culture by default).</param>
/// <param name="mode">The url mode (use site configuration by default).</param>
/// <param name="propertyAlias">The alias of the property (use 'umbracoFile' by default).</param>
/// <returns>The url for the media.</returns>
/// <remarks>
/// <para>The value of this property is contextual. It depends on the 'current' request uri,
/// if any. In addition, when the content type is multi-lingual, this is the url for the
/// specified culture. Otherwise, it is the invariant url.</para>
/// </remarks>
public static string MediaUrl(
this IPublishedContent content,
string culture = null,
UrlMode mode = UrlMode.Default,
string propertyAlias = Constants.Conventions.Media.File)
=> content.MediaUrl(PublishedUrlProvider, culture, mode, propertyAlias);
/// <summary>
/// Gets the name of the content item creator.
/// </summary>
/// <param name="content">The content item.</param>
public static string CreatorName(this IPublishedContent content) =>
content.CreatorName(UserService);
/// <summary>
/// Gets the name of the content item writer.
/// </summary>
/// <param name="content">The content item.</param>
public static string WriterName(this IPublishedContent content) =>
content.WriterName(UserService);
/// <summary>
/// Gets the culture assigned to a document by domains, in the context of a current Uri.
/// </summary>
/// <param name="content">The document.</param>
/// <param name="current">An optional current Uri.</param>
/// <returns>The culture assigned to the document by domains.</returns>
/// <remarks>
/// <para>In 1:1 multilingual setup, a document contains several cultures (there is not
/// one document per culture), and domains, withing the context of a current Uri, assign
/// a culture to that document.</para>
/// </remarks>
public static string GetCultureFromDomains(
this IPublishedContent content,
Uri current = null)
=> content.GetCultureFromDomains(UmbracoContextAccessor, SiteDomainHelper, current);
public static IEnumerable<PublishedSearchResult> SearchDescendants(
this IPublishedContent content,
string term,
string indexName = null)
=> content.SearchDescendants(ExamineManager, UmbracoContextAccessor, term, indexName);
public static IEnumerable<PublishedSearchResult> SearchChildren(
this IPublishedContent content,
string term,
string indexName = null)
=> content.SearchChildren(ExamineManager, UmbracoContextAccessor, term, indexName);
}
}

View File

@@ -0,0 +1,74 @@
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Web.Common.DependencyInjection;
namespace Umbraco.Extensions
{
public static class FriendlyPublishedElementExtensions
{
private static IPublishedValueFallback PublishedValueFallback { get; } =
StaticServiceProvider.Instance.GetRequiredService<IPublishedValueFallback>();
/// <summary>
/// Gets the value of a content's property identified by its alias.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="culture">The variation language.</param>
/// <param name="segment">The variation segment.</param>
/// <param name="fallback">Optional fallback strategy.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, if it exists, otherwise a default value.</returns>
/// <remarks>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, returns <paramref name="defaultValue"/>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static object Value(
this IPublishedElement content,
string alias,
string culture = null,
string segment = null,
Fallback fallback = default,
object defaultValue = default)
=> content.Value(PublishedValueFallback, alias, culture, segment, fallback, defaultValue);
/// <summary>
/// Gets the value of a content's property identified by its alias, converted to a specified type.
/// </summary>
/// <typeparam name="T">The target property type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="alias">The property alias.</param>
/// <param name="culture">The variation language.</param>
/// <param name="segment">The variation segment.</param>
/// <param name="fallback">Optional fallback strategy.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the content's property identified by the alias, converted to the specified type.</returns>
/// <remarks>
/// <para>The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering content.</para>
/// <para>If no property with the specified alias exists, or if the property has no value, or if it could not be converted, returns <c>default(T)</c>.</para>
/// <para>If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter.</para>
/// <para>The alias is case-insensitive.</para>
/// </remarks>
public static T Value<T>(
this IPublishedElement content,
string alias,
string culture = null,
string segment = null,
Fallback fallback = default,
T defaultValue = default)
=> content.Value<T>(PublishedValueFallback, alias, culture, segment, fallback, defaultValue);
/// <summary>
/// Gets a value indicating whether the content is visible.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>A value indicating whether the content is visible.</returns>
/// <remarks>A content is not visible if it has an umbracoNaviHide property with a value of "1". Otherwise,
/// the content is visible.</remarks>
public static bool IsVisible(this IPublishedElement content) => content.IsVisible(PublishedValueFallback);
}
}

View File

@@ -12,18 +12,22 @@ namespace Umbraco.Extensions
/// </summary>
public static class ImageCropperTemplateExtensions
{
private static readonly JsonSerializerSettings s_imageCropperValueJsonSerializerSettings = new JsonSerializerSettings
{
Culture = CultureInfo.InvariantCulture,
FloatParseHandling = FloatParseHandling.Decimal
};
internal static ImageCropperValue DeserializeImageCropperValue(this string json)
{
var imageCrops = new ImageCropperValue();
ImageCropperValue imageCrops = null;
if (json.DetectIsJson())
{
try
{
imageCrops = JsonConvert.DeserializeObject<ImageCropperValue>(json, new JsonSerializerSettings
{
Culture = CultureInfo.InvariantCulture,
FloatParseHandling = FloatParseHandling.Decimal
});
imageCrops = JsonConvert.DeserializeObject<ImageCropperValue>(json, s_imageCropperValueJsonSerializerSettings);
}
catch (Exception ex)
{
@@ -31,7 +35,9 @@ namespace Umbraco.Extensions
}
}
imageCrops ??= new ImageCropperValue();
return imageCrops;
}
}
}

View File

@@ -0,0 +1,221 @@
using System;
using System.Collections.Generic;
using System.Web;
using Examine;
using Microsoft.AspNetCore.Html;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Examine;
namespace Umbraco.Extensions
{
public static class PublishedContentExtensions
{
#region Creator/Writer Names
/// <summary>
/// Gets the name of the content item creator.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="userService"></param>
public static string CreatorName(this IPublishedContent content, IUserService userService) => userService.GetProfileById(content.CreatorId)?.Name;
/// <summary>
/// Gets the name of the content item writer.
/// </summary>
/// <param name="content">The content item.</param>
/// <param name="userService"></param>
public static string WriterName(this IPublishedContent content, IUserService userService) => userService.GetProfileById(content.WriterId)?.Name;
#endregion
#region Variations
/// <summary>
/// Gets the culture assigned to a document by domains, in the context of a current Uri.
/// </summary>
/// <param name="content">The document.</param>
/// <param name="umbracoContextAccessor"></param>
/// <param name="siteDomainHelper"></param>
/// <param name="current">An optional current Uri.</param>
/// <returns>The culture assigned to the document by domains.</returns>
/// <remarks>
/// <para>In 1:1 multilingual setup, a document contains several cultures (there is not
/// one document per culture), and domains, withing the context of a current Uri, assign
/// a culture to that document.</para>
/// </remarks>
public static string GetCultureFromDomains(this IPublishedContent content, IUmbracoContextAccessor umbracoContextAccessor, ISiteDomainHelper siteDomainHelper, Uri current = null)
=> DomainUtilities.GetCultureFromDomains(content.Id, content.Path, current, umbracoContextAccessor.UmbracoContext, siteDomainHelper);
#endregion
#region Search
public static IEnumerable<PublishedSearchResult> SearchDescendants(this IPublishedContent content, IExamineManager examineManager, IUmbracoContextAccessor umbracoContextAccessor, string term, string indexName = null)
{
indexName = string.IsNullOrEmpty(indexName) ? Constants.UmbracoIndexes.ExternalIndexName : indexName;
if (!examineManager.TryGetIndex(indexName, out var index))
{
throw new InvalidOperationException("No index found with name " + indexName);
}
var searcher = index.GetSearcher();
//var t = term.Escape().Value;
//var luceneQuery = "+__Path:(" + content.Path.Replace("-", "\\-") + "*) +" + t;
var query = searcher.CreateQuery()
.Field(UmbracoExamineFieldNames.IndexPathFieldName, (content.Path + ",").MultipleCharacterWildcard())
.And()
.ManagedQuery(term);
return query.Execute().ToPublishedSearchResults(umbracoContextAccessor.UmbracoContext.Content);
}
public static IEnumerable<PublishedSearchResult> SearchChildren(this IPublishedContent content, IExamineManager examineManager, IUmbracoContextAccessor umbracoContextAccessor, string term, string indexName = null)
{
indexName = string.IsNullOrEmpty(indexName) ? Constants.UmbracoIndexes.ExternalIndexName : indexName;
if (!examineManager.TryGetIndex(indexName, out var index))
{
throw new InvalidOperationException("No index found with name " + indexName);
}
var searcher = index.GetSearcher();
//var t = term.Escape().Value;
//var luceneQuery = "+parentID:" + content.Id + " +" + t;
var query = searcher.CreateQuery()
.Field("parentID", content.Id)
.And()
.ManagedQuery(term);
return query.Execute().ToPublishedSearchResults(umbracoContextAccessor.UmbracoContext.Content);
}
#endregion
#region IsSomething: equality
/// <summary>
/// If the specified <paramref name="content" /> is equal to <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsEqual(other, valueIfTrue, string.Empty);
/// <summary>
/// If the specified <paramref name="content" /> is equal to <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsEqual(other) ? valueIfTrue : valueIfFalse));
/// <summary>
/// If the specified <paramref name="content" /> is not equal to <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsNotEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsNotEqual(other, valueIfTrue, string.Empty);
/// <summary>
/// If the specified <paramref name="content" /> is not equal to <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsNotEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsNotEqual(other) ? valueIfTrue : valueIfFalse));
#endregion
#region IsSomething: ancestors and descendants
/// <summary>
/// If the specified <paramref name="content" /> is a decendant of <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <see cref="string.Empty" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsDescendant(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsDescendant(other, valueIfTrue, string.Empty);
/// <summary>
/// If the specified <paramref name="content" /> is a decendant of <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsDescendant(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsDescendant(other) ? valueIfTrue : valueIfFalse));
public static IHtmlContent IsDescendantOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsDescendantOrSelf(other, valueIfTrue, string.Empty);
/// <summary>
/// If the specified <paramref name="content" /> is a decendant of <paramref name="other" /> or are the same, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsDescendantOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsDescendantOrSelf(other) ? valueIfTrue : valueIfFalse));
public static IHtmlContent IsAncestor(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsAncestor(other, valueIfTrue, string.Empty);
/// <summary>
/// If the specified <paramref name="content" /> is an ancestor of <paramref name="other" />, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsAncestor(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsAncestor(other) ? valueIfTrue : valueIfFalse));
public static IHtmlContent IsAncestorOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue) => content.IsAncestorOrSelf(other, valueIfTrue, string.Empty);
/// <summary>
/// If the specified <paramref name="content" /> is an ancestor of <paramref name="other" /> or are the same, the HTML encoded <paramref name="valueIfTrue" /> will be returned; otherwise, <paramref name="valueIfFalse" />.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="other">The other content.</param>
/// <param name="valueIfTrue">The value if <c>true</c>.</param>
/// <param name="valueIfFalse">The value if <c>false</c>.</param>
/// <returns>
/// The HTML encoded value.
/// </returns>
public static IHtmlContent IsAncestorOrSelf(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) => new HtmlString(HttpUtility.HtmlEncode(content.IsAncestorOrSelf(other) ? valueIfTrue : valueIfFalse));
#endregion
}
}