public override async Task <IDisplayResult> UpdateAsync(AutoroutePart model, IUpdateModel updater) { var viewModel = new AutoroutePartViewModel(); await updater.TryUpdateModelAsync(viewModel, Prefix, t => t.Path, t => t.UpdatePath); var settings = GetSettings(model); if (settings.AllowCustomPath) { model.Path = viewModel.Path; } if (settings.AllowUpdatePath && viewModel.UpdatePath) { // Make it empty to force a regeneration model.Path = ""; } var httpContext = _httpContextAccessor.HttpContext; if (httpContext != null && await _authorizationService.AuthorizeAsync(httpContext.User, Permissions.SetHomepage)) { await updater.TryUpdateModelAsync(model, Prefix, t => t.SetHomepage); } await ValidateAsync(model, updater); return(Edit(model)); }
public override async Task <IDisplayResult> UpdateAsync(AutoroutePart model, IUpdateModel updater, UpdatePartEditorContext context) { var viewModel = new AutoroutePartViewModel(); await updater.TryUpdateModelAsync(viewModel, Prefix, t => t.Path, t => t.UpdatePath, t => t.RouteContainedItems, t => t.Absolute, t => t.Disabled); var settings = context.TypePartDefinition.GetSettings <AutoroutePartSettings>(); model.Disabled = viewModel.Disabled; model.Absolute = viewModel.Absolute; model.RouteContainedItems = viewModel.RouteContainedItems; // When disabled these values are not updated. if (!model.Disabled) { if (settings.AllowCustomPath) { model.Path = viewModel.Path; } if (settings.AllowUpdatePath && viewModel.UpdatePath) { // Make it empty to force a regeneration model.Path = ""; } var httpContext = _httpContextAccessor.HttpContext; if (httpContext != null && await _authorizationService.AuthorizeAsync(httpContext.User, Permissions.SetHomepage)) { await updater.TryUpdateModelAsync(model, Prefix, t => t.SetHomepage); } updater.ModelState.BindValidationResults(Prefix, model.ValidatePathFieldValue(S)); // This can only validate the path if the Autoroute is not managing content item routes or the path is absolute. if (!String.IsNullOrEmpty(model.Path) && (!settings.ManageContainedItemRoutes || (settings.ManageContainedItemRoutes && model.Absolute))) { var possibleConflicts = await _session.QueryIndex <AutoroutePartIndex>(o => o.Path == model.Path).ListAsync(); if (possibleConflicts.Any()) { var hasConflict = false; if (possibleConflicts.Any(x => x.ContentItemId != model.ContentItem.ContentItemId) || possibleConflicts.Any(x => !string.IsNullOrEmpty(x.ContainedContentItemId) && x.ContainedContentItemId != model.ContentItem.ContentItemId)) { hasConflict = true; } if (hasConflict) { updater.ModelState.AddModelError(Prefix, nameof(model.Path), S["Your permalink is already in use."]); } } } } return(Edit(model, context)); }
private async Task GenerateContainerPathFromPattern(AutoroutePart part) { // Compute the Path only if it's empty if (!String.IsNullOrWhiteSpace(part.Path)) { return; } var pattern = GetPattern(part); if (!String.IsNullOrEmpty(pattern)) { var model = new AutoroutePartViewModel() { Path = part.Path, AutoroutePart = part, ContentItem = part.ContentItem }; _contentManager ??= _serviceProvider.GetRequiredService <IContentManager>(); var cultureAspect = await _contentManager.PopulateAspectAsync(part.ContentItem, new CultureAspect()); using (CultureScope.Create(cultureAspect.Culture)) { part.Path = await _liquidTemplateManager.RenderAsync(pattern, NullEncoder.Default, model, scope => scope.SetValue(nameof(ContentItem), model.ContentItem)); } part.Path = part.Path.Replace("\r", String.Empty).Replace("\n", String.Empty); if (part.Path?.Length > AutoroutePart.MaxPathLength) { part.Path = part.Path.Substring(0, AutoroutePart.MaxPathLength); } if (!await IsAbsolutePathUniqueAsync(part.Path, part.ContentItem.ContentItemId)) { part.Path = await GenerateUniqueAbsolutePathAsync(part.Path, part.ContentItem.ContentItemId); } part.Apply(); } }
public override async Task <IDisplayResult> UpdateAsync(AutoroutePart model, IUpdateModel updater, UpdatePartEditorContext context) { var viewModel = new AutoroutePartViewModel(); await updater.TryUpdateModelAsync(viewModel, Prefix, t => t.Path, t => t.UpdatePath, t => t.RouteContainedItems, t => t.Absolute, t => t.Disabled); var settings = context.TypePartDefinition.GetSettings <AutoroutePartSettings>(); model.Disabled = viewModel.Disabled; model.Absolute = viewModel.Absolute; model.RouteContainedItems = viewModel.RouteContainedItems; // When disabled these values are not updated. if (!model.Disabled) { if (settings.AllowCustomPath) { model.Path = viewModel.Path; } if (settings.AllowUpdatePath && viewModel.UpdatePath) { // Make it empty to force a regeneration model.Path = ""; } var httpContext = _httpContextAccessor.HttpContext; if (httpContext != null && await _authorizationService.AuthorizeAsync(httpContext.User, Permissions.SetHomepage)) { await updater.TryUpdateModelAsync(model, Prefix, t => t.SetHomepage); } await ValidateAsync(model, updater, settings); } return(Edit(model, context)); }
public override async Task UpdatedAsync(UpdateContentContext context, AutoroutePart part) { // Compute the Path only if it's empty if (!String.IsNullOrWhiteSpace(part.Path)) { return; } var pattern = GetPattern(part); if (!String.IsNullOrEmpty(pattern)) { var model = new AutoroutePartViewModel() { Path = part.Path, AutoroutePart = part, ContentItem = part.ContentItem }; part.Path = await _liquidTemplateManager.RenderAsync(pattern, NullEncoder.Default, model, scope => scope.SetValue("ContentItem", model.ContentItem)); part.Path = part.Path.Replace("\r", String.Empty).Replace("\n", String.Empty); if (part.Path?.Length > AutoroutePartDisplay.MaxPathLength) { part.Path = part.Path.Substring(0, AutoroutePartDisplay.MaxPathLength); } if (!await IsPathUniqueAsync(part.Path, part)) { part.Path = await GenerateUniquePathAsync(part.Path, part); } part.Apply(); } }
public override async Task UpdatedAsync(UpdateContentContext context, AutoroutePart part) { // Compute the Path only if it's empty if (!String.IsNullOrWhiteSpace(part.Path)) { return; } var pattern = GetPattern(part); if (!String.IsNullOrEmpty(pattern)) { var model = new AutoroutePartViewModel() { Path = part.Path, AutoroutePart = part, ContentItem = part.ContentItem }; var templateContext = new TemplateContext(); templateContext.SetValue("ContentItem", part.ContentItem); templateContext.MemberAccessStrategy.Register <AutoroutePartViewModel>(); templateContext.SetValue("Model", model); part.Path = await _liquidTemplateManager.RenderAsync(pattern, NullEncoder.Default, templateContext); part.Path = part.Path.Replace("\r", String.Empty).Replace("\n", String.Empty); if (!await IsPathUniqueAsync(part.Path, part)) { part.Path = await GenerateUniquePathAsync(part.Path, part); } part.Apply(); } }