Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Rob Watkins 369 posts 701 karma points
    Aug 09, 2023 @ 07:43
    Rob Watkins
    0

    Member notifications and IComposers

    Soooo, I'm trying to use the U12 IComposer events to automatically assign members not in a group to a group, thus allowing auto registration.

    Note: I have looked at the documentation for auto registration by subclassing the existing snippet, but I'd prefer to keep that stock and extend using events.

    So far I have this, but it's not working, is this the right way of creating a Composer? Should this handle global member creation events?

    using Umbraco.Cms.Core.Composing;
    using Umbraco.Cms.Core.Events;
    using Umbraco.Cms.Core.Security;
    using UN = Umbraco.Cms.Core.Notifications;
    
    namespace MyApp.Components
    {
        // ================================================================================================================================================ 
        public class SubscribeToMemberSavedNotificationComposer : IComposer
        {
            public void Compose(IUmbracoBuilder builder)
            {
                builder.AddNotificationHandler<UN.MemberSavedNotification, MemberSavedNotificationHandler>();
            }
        }
    
        // ================================================================================================================================================ 
        public class MemberSavedNotificationHandler : INotificationHandler<UN.MemberSavedNotification>
        {
            public const string DEFAULT_ROLE = "Unapproved";
    
            public MemberSavedNotificationHandler(IMemberManager membermanager)
            {
                _membermanager = membermanager;
            }
    
            public void Handle(UN.MemberSavedNotification notification)
        {
            var unassignedmembers = notification.SavedEntities
                .Select(x =>
                {
                    var user = _membermanager.FindByIdAsync(x.Id.ToString()).Result;
                    var roles = user != null ? _membermanager.GetRolesAsync(user).Result : null;
    
                    return new
                    {
                        User = user,
                        Roles = roles ?? new List<string>()
                    };
                })
                .Where(x => !x.Roles.Any());            
    
            foreach (var o in unassignedmembers)
            {
                if (o.User != null)
                    _membermanager.AddToRolesAsync(o.User, new string[] { DEFAULT_ROLE });
            }
        }
    
            // ------------------------------------------------------------------------------------------------------------------------------------------------ 
            private IMemberManager _membermanager;
        }
    }
    
  • Rob Watkins 369 posts 701 karma points
    Aug 09, 2023 @ 12:11
    Rob Watkins
    100

    No wait! I'm an idiot! I just needed to restart the website, my new code hadn't uiploaded because the DLLs were in use 🙄

    Leaving this question here in case anyone else finds this way of assigning users to groups useful!

    NOTE: This will assign any members not in groups to the default group, even if you have manually removed them. This is exactly what I want for my code because I always want them in a group, even if just the base Unapproved group, but you may not want this.

Please Sign in or register to post replies

Write your reply to:

Draft