From 7bda31c7cb718a6048f25dcd0b3fc6f38ece75f1 Mon Sep 17 00:00:00 2001 From: Dean Marcussen Date: Sun, 27 Jan 2019 20:27:47 +0000 Subject: [PATCH] Added validation for AliasPart to test for duplicate aliases (#2310) (#3095) --- .../Drivers/AliasPartDisplayDriver.cs | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Alias/Drivers/AliasPartDisplayDriver.cs b/src/OrchardCore.Modules/OrchardCore.Alias/Drivers/AliasPartDisplayDriver.cs index eebc130eed7..bf3d6d413de 100644 --- a/src/OrchardCore.Modules/OrchardCore.Alias/Drivers/AliasPartDisplayDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Alias/Drivers/AliasPartDisplayDriver.cs @@ -7,16 +7,27 @@ using OrchardCore.Alias.Models; using OrchardCore.Alias.Settings; using OrchardCore.Alias.ViewModels; +using YesSql; +using OrchardCore.Alias.Indexes; +using Microsoft.Extensions.Localization; +using OrchardCore.Mvc.ModelBinding; namespace OrchardCore.Alias.Drivers { public class AliasPartDisplayDriver : ContentPartDisplayDriver { private readonly IContentDefinitionManager _contentDefinitionManager; + private readonly ISession _session; + private readonly IStringLocalizer T; - public AliasPartDisplayDriver(IContentDefinitionManager contentDefinitionManager) + public AliasPartDisplayDriver( + IContentDefinitionManager contentDefinitionManager, + ISession session, + IStringLocalizer localizer) { _contentDefinitionManager = contentDefinitionManager; + _session = session; + T = localizer; } public override IDisplayResult Edit(AliasPart aliasPart) @@ -29,7 +40,9 @@ public override async Task UpdateAsync(AliasPart model, IUpdateM var settings = GetAliasPartSettings(model); await updater.TryUpdateModelAsync(model, Prefix, t => t.Alias); - + + await ValidateAsync(model, updater); + return Edit(model); } @@ -50,5 +63,13 @@ private void BuildViewModel(AliasPartViewModel model, AliasPart part) model.AliasPart = part; model.Settings = settings; } + + private async Task ValidateAsync(AliasPart alias, IUpdateModel updater) + { + if (alias.Alias != null && (await _session.QueryIndex(o => o.Alias == alias.Alias && o.ContentItemId != alias.ContentItem.ContentItemId).CountAsync()) > 0) + { + updater.ModelState.AddModelError(Prefix, nameof(alias.Alias), T["Your alias is already in use."]); + } + } } }