Copied to clipboard

Flag this post as spam?

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


  • Janus Kamp Hansen 12 posts 149 karma points
    Nov 16, 2016 @ 14:13
    Janus Kamp Hansen
    1

    Getting assigned roles on created/saved Member

    Hi guys and girls,

    I am currently working on a solution (running with Umbraco 7.3.0). I have a slighly annoyin problem.

    When I from the backend of Umbraco create a new member and I assign one, two or more membergroups to the new member and then saves the member, I have a hard time of reading the membergroups of the user in the ApplicationEventHandler

       protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
    
            umbracoApplication.Error += UmbracoApplication_Error;
    
            // Before member is saved.
            MemberService.Saving += MemberService_Saving;
    
            // After a member is saved.
            MemberService.Saved += MemberService_Saved;
    
            // Member being created
            MemberService.Created += MemberService_Created;
    
            // Before member is being deleted.
            MemberService.Deleting += MemberService_Deleting;
    
        }
    
        private void MemberService_Created(IMemberService sender, NewEventArgs<IMember> e)
        {
    
            List<string> roles = ApplicationContext.Current.Services.MemberService.GetAllRoles(e.Entity.Id).ToList();
            if (roles.Count > 0)
            {
                // Never happens?!?
            }
    
    
        }
    

    Almost same issue in "Saving"- and "Saved"-events of the Member; but here I only get the "previous" saved roles (memberGroups).

    Does anyone know of a trick to get the Role-provider updated or how to retrieve the "newly assigned" membergroups from a Member being created or saved.

    I hope you understand my issue here...

  • Janus Kamp Hansen 12 posts 149 karma points
    Nov 16, 2016 @ 18:57
    Janus Kamp Hansen
    101

    Well... I found a usefull solution myself.

    Did an event and created a delay of five seconds, which gives Umbraco plenty of time to update the membergroups on Member.

    Here is the solution:

    public delegate void MemberSavedEventHandler(object sender, IMember member);
    
    public event MemberSavedEventHandler MemberSaved;
    

    And then let the MemberService_Saved invoke a Task named "UpdateMemberAfterSaveDelayed(member)", which then waits the five seconds before running.

        /// <summary>
        /// After a member is saved.
        /// Debtor Data in e-conomic is updated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MemberService_Saved(IMemberService sender, SaveEventArgs<IMember> e)
        {
            foreach (var member in e.SavedEntities)
            {
    
                System.Threading.Tasks.Task.Run(() => UpdateMemberAfterSaveDelayed(member))
                .ContinueWith(t =>
                {
                    var ex = t.Exception.InnerException;
    
                }, TaskContinuationOptions.OnlyOnFaulted);
    
            }
        }
    
        /// <summary>
        /// Delegate that triggers RegisterEvent_MemberSaved after 5 secs.
        /// </summary>
        /// <param name="member"></param>
        private void UpdateMemberAfterSaveDelayed(IMember member)
        {
    
            System.Threading.Thread.Sleep(5000);
            if (MemberSaved != null)
            {
                MemberSaved(this, member);
            }
    
        }
    
        /// <summary>
        /// Be aware! This method is running in a thread. Not all classes can be instanced.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="member"></param>
        private void RegisterEvents_MemberSaved(object sender, IMember member)
        {
    
            if (member != null && member.Id > 0)
            {
    
                if (member.IsDirty()) {
    
                ..... doing all kinds of stuff....
    
                }
            }
        }
    

    May you also can use this...

  • Simon Dingley 1441 posts 3368 karma points c-trib
    Oct 20, 2021 @ 15:03
    Simon Dingley
    0

    Thanks for sharing this Janus. Almost 4 years after your post I have finally managed to work around the issues I've been fighting with for so long in a v7 install I still maintain! #h5yr

Please Sign in or register to post replies

Write your reply to:

Draft