Copied to clipboard

Flag this post as spam?

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


  • Edgaras 3 posts 83 karma points
    Oct 01, 2018 @ 11:44
    Edgaras
    0

    Listening to User Creation

    Hello,

    My goal is to create a member with identical login and password, whenever a backoffice user is created, but I can't find any event listener or something to listen to when user created? Can anyone point me in the right direction?

  • Marc Goodson 2146 posts 14350 karma points MVP 8x c-trib
    Oct 01, 2018 @ 22:45
    Marc Goodson
    100

    Hi Edgaras

    Could you perhaps make use of the UserServices' SavingUser event?

    I'm not sure what you would use for the password for the 'twinned' member?

    public class AppStart : ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            UserService.SavingUser += UserService_SavingUser;
        }
    
        private void UserService_SavingUser(IUserService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.Membership.IUser> e)
        {
            var memberService = ApplicationContext.Current.Services.MemberService;
            // do something with each saved IUser
            foreach (var savedUser in e.SavedEntities)
            {
                //check if member exists
               var existingMember = memberService.GetByEmail(savedUser.Email);
                if (existingMember != null)
                {
                    var newMember = memberService.CreateMemberWithIdentity(savedUser.Username, savedUser.Email, savedUser.Name, "Member");
                    memberService.Save(newMember);      
                }
            }
        }
    }
    
  • Edgaras 3 posts 83 karma points
    Oct 01, 2018 @ 23:14
    Edgaras
    0

    Thank you!

    I have a problem where I need backoffice users to have aditional properties to go along with content they create(e.g. job title, number, etc.) and I was going to make a member type with those properties for them to use, but I don't want content creators to have two different accounts on the same site.

    Since I guess keeping passwords the same would be a problem, I am thinking about creating twinned member for each user with a random generated password that they will never use, and authenticate as said member whenever coresponding user is logged in inside backoffice. Would this be a terrible idea?

Please Sign in or register to post replies

Write your reply to:

Draft