Skip to content

Commit

Permalink
Added validation for AliasPart to test for duplicate aliases (#2310) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
deanmarcussen authored and sebastienros committed Jan 27, 2019
1 parent fe78d13 commit 7bda31c
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AliasPart>
{
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly ISession _session;
private readonly IStringLocalizer<AliasPartDisplayDriver> T;

public AliasPartDisplayDriver(IContentDefinitionManager contentDefinitionManager)
public AliasPartDisplayDriver(
IContentDefinitionManager contentDefinitionManager,
ISession session,
IStringLocalizer<AliasPartDisplayDriver> localizer)
{
_contentDefinitionManager = contentDefinitionManager;
_session = session;
T = localizer;
}

public override IDisplayResult Edit(AliasPart aliasPart)
Expand All @@ -29,7 +40,9 @@ public override async Task<IDisplayResult> UpdateAsync(AliasPart model, IUpdateM
var settings = GetAliasPartSettings(model);

await updater.TryUpdateModelAsync(model, Prefix, t => t.Alias);


await ValidateAsync(model, updater);

return Edit(model);
}

Expand All @@ -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<AliasPartIndex>(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."]);
}
}
}
}

0 comments on commit 7bda31c

Please sign in to comment.