-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow configuring Azure Search AI from UI or appsettings. (#15004)
- Loading branch information
1 parent
5a90a24
commit c0ed728
Showing
33 changed files
with
718 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
...e.Modules/OrchardCore.Search.AzureAI/Drivers/AzureAISearchDefaultSettingsDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.DataProtection; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.Extensions.Localization; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.DisplayManagement.Entities; | ||
using OrchardCore.DisplayManagement.Handlers; | ||
using OrchardCore.DisplayManagement.Views; | ||
using OrchardCore.Environment.Shell; | ||
using OrchardCore.Modules; | ||
using OrchardCore.Mvc.ModelBinding; | ||
using OrchardCore.Search.AzureAI.Models; | ||
using OrchardCore.Search.AzureAI.Services; | ||
using OrchardCore.Search.AzureAI.ViewModels; | ||
using OrchardCore.Settings; | ||
|
||
namespace OrchardCore.Search.AzureAI.Drivers; | ||
|
||
public class AzureAISearchDefaultSettingsDisplayDriver : SectionDisplayDriver<ISite, AzureAISearchDefaultSettings> | ||
{ | ||
public const string GroupId = "azureAISearch"; | ||
|
||
private static readonly char[] _separator = [',', ' ']; | ||
|
||
private readonly AzureAISearchIndexSettingsService _indexSettingsService; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IAuthorizationService _authorizationService; | ||
private readonly ShellSettings _shellSettings; | ||
private readonly IShellHost _shellHost; | ||
private readonly AzureAISearchDefaultOptions _searchOptions; | ||
private readonly IDataProtectionProvider _dataProtectionProvider; | ||
|
||
protected readonly IStringLocalizer S; | ||
|
||
public AzureAISearchDefaultSettingsDisplayDriver( | ||
AzureAISearchIndexSettingsService indexSettingsService, | ||
IHttpContextAccessor httpContextAccessor, | ||
IAuthorizationService authorizationService, | ||
IOptions<AzureAISearchDefaultOptions> searchOptions, | ||
ShellSettings shellSettings, | ||
IShellHost shellHost, | ||
IDataProtectionProvider dataProtectionProvider, | ||
IStringLocalizer<AzureAISearchDefaultSettingsDisplayDriver> stringLocalizer | ||
) | ||
{ | ||
_indexSettingsService = indexSettingsService; | ||
_httpContextAccessor = httpContextAccessor; | ||
_authorizationService = authorizationService; | ||
_shellSettings = shellSettings; | ||
_shellHost = shellHost; | ||
_searchOptions = searchOptions.Value; | ||
_dataProtectionProvider = dataProtectionProvider; | ||
S = stringLocalizer; | ||
} | ||
|
||
public override IDisplayResult Edit(AzureAISearchDefaultSettings settings) | ||
{ | ||
if (_searchOptions.DisableUIConfiguration) | ||
{ | ||
return null; | ||
} | ||
|
||
return Initialize<AzureAISearchDefaultSettingsViewModel>("AzureAISearchDefaultSettings_Edit", model => | ||
{ | ||
model.AuthenticationTypes = new[] | ||
{ | ||
new SelectListItem(S["Default"], nameof(AzureAIAuthenticationType.Default)), | ||
new SelectListItem(S["Managed Identity"], nameof(AzureAIAuthenticationType.ManagedIdentity)), | ||
new SelectListItem(S["API Key"], nameof(AzureAIAuthenticationType.ApiKey)), | ||
}; | ||
model.ConfigurationsAreOptional = _searchOptions.IsFileConfigurationExists(); | ||
model.AuthenticationType = settings.AuthenticationType; | ||
model.UseCustomConfiguration = settings.UseCustomConfiguration; | ||
model.Endpoint = settings.Endpoint; | ||
model.IdentityClientId = settings.IdentityClientId; | ||
model.ApiKeyExists = !string.IsNullOrEmpty(settings.ApiKey); | ||
}).Location("Content") | ||
.RenderWhen(() => _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, AzureAISearchIndexPermissionHelper.ManageAzureAISearchIndexes)) | ||
.OnGroup(GroupId); | ||
} | ||
|
||
public override async Task<IDisplayResult> UpdateAsync(AzureAISearchDefaultSettings settings, BuildEditorContext context) | ||
{ | ||
if (!GroupId.EqualsOrdinalIgnoreCase(context.GroupId) || _searchOptions.DisableUIConfiguration) | ||
{ | ||
return null; | ||
} | ||
|
||
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, AzureAISearchIndexPermissionHelper.ManageAzureAISearchIndexes)) | ||
{ | ||
return null; | ||
} | ||
|
||
var model = new AzureAISearchDefaultSettingsViewModel(); | ||
|
||
if (await context.Updater.TryUpdateModelAsync(model, Prefix)) | ||
{ | ||
if (!_searchOptions.IsFileConfigurationExists()) | ||
{ | ||
model.UseCustomConfiguration = true; | ||
} | ||
|
||
var useCustomConfigurationChanged = settings.UseCustomConfiguration != model.UseCustomConfiguration; | ||
|
||
if (model.UseCustomConfiguration) | ||
{ | ||
settings.AuthenticationType = model.AuthenticationType.Value; | ||
settings.Endpoint = model.Endpoint; | ||
settings.IdentityClientId = model.IdentityClientId?.Trim(); | ||
|
||
if (string.IsNullOrWhiteSpace(model.Endpoint)) | ||
{ | ||
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Endpoint), S["Endpoint is required."]); | ||
} | ||
else if (!Uri.TryCreate(model.Endpoint, UriKind.Absolute, out var _)) | ||
{ | ||
context.Updater.ModelState.AddModelError(Prefix, nameof(model.Endpoint), S["Endpoint must be a valid url."]); | ||
} | ||
|
||
if (model.AuthenticationType == AzureAIAuthenticationType.ApiKey) | ||
{ | ||
var hasNewKey = !string.IsNullOrWhiteSpace(model.ApiKey); | ||
|
||
if (!hasNewKey && string.IsNullOrEmpty(settings.ApiKey)) | ||
{ | ||
context.Updater.ModelState.AddModelError(Prefix, nameof(model.ApiKey), S["API Key is required when using API Key authentication type."]); | ||
} | ||
else if (hasNewKey) | ||
{ | ||
var protector = _dataProtectionProvider.CreateProtector(AzureAISearchDefaultOptionsConfigurations.ProtectorName); | ||
|
||
settings.ApiKey = protector.Protect(model.ApiKey); | ||
} | ||
} | ||
} | ||
|
||
settings.UseCustomConfiguration = model.UseCustomConfiguration; | ||
|
||
if (context.Updater.ModelState.IsValid && | ||
(_searchOptions.Credential?.Key != model.ApiKey | ||
|| _searchOptions.Endpoint != settings.Endpoint | ||
|| _searchOptions.AuthenticationType != settings.AuthenticationType | ||
|| _searchOptions.IdentityClientId != settings.IdentityClientId | ||
|| useCustomConfigurationChanged)) | ||
{ | ||
await _shellHost.ReleaseShellContextAsync(_shellSettings); | ||
} | ||
} | ||
|
||
return Edit(settings); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.