Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validation for AliasPart to test for duplicate aliases (#2310) #3095

Merged
merged 1 commit into from
Jan 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."]);
}
}
}
}