Copied to clipboard

Flag this post as spam?

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


  • Puck Holshuijsen 188 posts 751 karma points
    Dec 03, 2024 @ 12:02
    Puck Holshuijsen
    0

    Change username on save in MemberSavingNotification - currently not working properly on my end

    Hi all,

    I am having some issues with saving a new member in Umbraco 13 (13.5.2). Here's the situation.

    I have a website with Umbraco Commerce (13.1.12) and inside the backend I need to have multiple shops. Each shop is it's own entity, and each shop has their own customers. This means I can register on both shops with the same e-mailaddress as username/mail.

    At this moment I am able to do this by adding a specific shopId in front of the username, and hide it where i need to hide it.

    E.g. i register with [email protected] and in code i replace it with 00001_ [email protected]. The customer won't see the 00001_ , the shop won't see 00001_ etc. This all is working like it should.

    Here comes the issue: When i create a member (login) in the backend, I currently am using the MemberSavingNotification to add the shopId. Which kind of works, except the fact that all the properties i fill in, stay empty.

        public void Handle(MemberSavingNotification notification)
    {
    
        foreach (var member in notification.SavedEntities)
        {
            var isNew = member.Id == 0;
            var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
            string shopId = string.Empty;
    
            bool dirtyName = member.IsPropertyDirty("Name");
            bool dirtyUsername = member.IsPropertyDirty("Username");
            bool dirtyEmail = member.IsPropertyDirty("Email");
    
            if (isNew || dirtyUsername || dirtyEmail)
            {
                if (currentUser != null)
                {
                    var group = currentUser.Groups.FirstOrDefault(g => g.Alias.StartsWith("shop"));
                    if (group != null)
                    {
                        shopId = group.Name;
                    }
                }
                if (dirtyUsername || isNew)
                {
                    member.Username = $"{shopId}_{member.Username}";
                }
            }
        }
    }
    

    When i comment out this part, the saving is working fine.

          if (dirtyUsername || isNew)
                {
                    member.Username = $"{shopId}_{member.Username}";
                }
    

    I can't for the life figure out what I am doing wrong, or if I should be doing this differently :)

    Hope anyone can help me.

    Thanks in advance,

    Puck

Please Sign in or register to post replies

Write your reply to:

Draft