Last active
September 12, 2022 10:56
-
-
Save nul800sebastiaan/d3c18ab4e238efa405916b88d9807ead to your computer and use it in GitHub Desktop.
Add member groups to Umbraco's internal member index
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
using Examine; | |
using Examine.Lucene; | |
using Microsoft.Extensions.Options; | |
using Umbraco.Cms.Core; | |
using Umbraco.Cms.Core.Composing; | |
using Umbraco.Cms.Core.Security; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Core.Web; | |
namespace MyProject; | |
public class MemberGroupIndexerComposer : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
builder.Services.ConfigureOptions<ConfigureMemberGroupsIndexOptions>(); | |
} | |
public sealed class ConfigureMemberGroupsIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions> | |
{ | |
public void Configure(string name, LuceneDirectoryIndexOptions options) | |
{ | |
switch (name) | |
{ | |
// Technically it's not necessary to explicitly add a field definition, | |
// but it allows you to control the field definition type | |
case Constants.UmbracoIndexes.MembersIndexName: | |
options.FieldDefinitions.TryAdd(new FieldDefinition("memberGroupNames", FieldDefinitionTypes.FullText)); | |
options.FieldDefinitions.TryAdd(new FieldDefinition("memberGroupIds", FieldDefinitionTypes.FullText)); | |
break; | |
} | |
} | |
public void Configure(LuceneDirectoryIndexOptions options) | |
=> Configure(string.Empty, options); | |
} | |
public class SubscribeToExamineSetupComposer : ComponentComposer<MemberGroupIndexerComponent> { } | |
public class MemberGroupIndexerComponent : IComponent | |
{ | |
private readonly IServiceProvider _serviceProvider; | |
private readonly IUmbracoContextFactory _contextFactory; | |
private readonly IExamineManager _examineManager; | |
public MemberGroupIndexerComponent( | |
IUmbracoContextFactory contextFactory, | |
IExamineManager examineManager, | |
IServiceProvider serviceProvider) | |
{ | |
_contextFactory = contextFactory; | |
_examineManager = examineManager; | |
_serviceProvider = serviceProvider; | |
} | |
public void Initialize() | |
{ | |
if (!_examineManager.TryGetIndex(Constants.UmbracoIndexes.MembersIndexName, out var index)) | |
{ | |
throw new InvalidOperationException( | |
$"No index found by the name {Constants.UmbracoIndexes.MembersIndexName}"); | |
} | |
if (index is not BaseIndexProvider indexProvider) | |
{ | |
throw new InvalidOperationException("Could not cast)"); | |
} | |
indexProvider.TransformingIndexValues += IndexProviderTransformingIndexValues; | |
} | |
private void IndexProviderTransformingIndexValues(object? sender, IndexingItemEventArgs e) | |
{ | |
if (e.ValueSet.ItemType != "Member" || !int.TryParse(e.ValueSet.Id, out var memberId)) | |
{ | |
return; | |
} | |
using var _ = _contextFactory.EnsureUmbracoContext(); | |
using var serviceScope = _serviceProvider.CreateScope(); | |
var memberManager = serviceScope.ServiceProvider.GetRequiredService<IMemberManager>(); | |
var memberGroupService = serviceScope.ServiceProvider.GetRequiredService<IMemberGroupService>(); | |
var memberGroups = memberGroupService.GetAll().ToList(); | |
var member = memberManager.FindByIdAsync(memberId.ToString()).Result; | |
var roles = memberManager.GetRolesAsync(member).Result; | |
var memberGroupForMember = memberGroups.Where(x => x.Name != null && x.Name.ContainsAny(roles)); | |
var memberGroupIds = memberGroupForMember.Select(x => x.Id); | |
// get all the values we currently have | |
var updatedValues = e.ValueSet.Values.ToDictionary(x => x.Key, x => x.Value.ToList()); | |
// add our own values to the new field | |
// note: comma separated doesn't work well for Examine so we're using spaces | |
updatedValues.Add("memberGroupNames", new List<object> { string.Join(" ", roles) }); | |
updatedValues.Add("memberGroupIds", new List<object> { string.Join(" ", memberGroupIds) }); | |
// set all the values for persisting them | |
e.SetValues(updatedValues.ToDictionary(x => x.Key, x => (IEnumerable<object>)x.Value)); | |
} | |
public void Terminate() { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment