Copied to clipboard

Flag this post as spam?

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


  • ssougnez 93 posts 319 karma points c-trib
    Nov 18, 2018 @ 16:41
    ssougnez
    0

    Can I used a back office user on my web site?

    Hi,

    I’m creating a documentation site where users will be able to add new article. At first, the site will be anonymous but I’d like to be able to give visitors the possibility to vote or add comments on the doc.

    This site will be deployed like an internal site only available for the company I’m working for. Therefore, readers and writers can basically be anyone. Therefore, I would like that a writer could also add a comment or like a documentation page and his comment would appear with his name.

    So basically, I would like to use a back office users also as a member, is it possible ? It would be a shame to be forced to have two different accounts for the same site.

  • Sven Geusens 169 posts 881 karma points c-trib
    Nov 19, 2018 @ 14:12
    Sven Geusens
    0

    I wouldn't use a (backoffice) user as a member, these are 2 separate entities with different goals.

    What you could do, is when a user is saved, create a member and keep it in sync. I have no idea how one would go about keeping the passwords in sync.

    class UmbracoAppStartup : IApplicationEventHandler
    {
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication,
            ApplicationContext applicationContext)
        {
            UserService.SavedUser += UserService_SavedUser;
        }
    
        private void UserService_SavedUser(IUserService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.Membership.IUser> e)
        {
            int outInt;
            var memberTypeToCreate = ApplicationContext.Current.Services.MemberTypeService.GetAll()
                .FirstOrDefault(t => t.Name == "member");
    
            if (memberTypeToCreate == null)
            {
                // log
                return;
            }
    
            foreach (var user in e.SavedEntities)
            {
                var existingMember =
                    ApplicationContext.Current.Services.MemberService.FindByUsername(user.Username, 0, 1, out outInt).FirstOrDefault();
    
                if (existingMember == null)
                {
                    var newMember =
                        ApplicationContext.Current.Services.MemberService.CreateMember(user.Username, user.Email,
                            user.Name, memberTypeToCreate);
                    if (newMember == null)
                    {
                        // log
                        continue;
                    }
                    ApplicationContext.Current.Services.MemberService.Save(newMember);
                }
                else
                {
                    existingMember.Name = user.Name;
                    // ... any other props you want to sync
                    ApplicationContext.Current.Services.MemberService.Save(existingMember);
                }
    
            }
        }
    }
    

    On the login controller for the front-end you could check if there is a user logged into the backoffice and then log in the member with the same username. I have no idea what the recommended way is to do this, but you can have a look in this post for more information https://our.umbraco.com/forum/developers/api-questions/50075-how-to-get-the-current-user-with-UserService

    To force login a member you will need to implement the following package https://www.nuget.org/packages/UmbracoIdentity.Core/

    Again, this is what you could do, I don't know if its the best approach.

Please Sign in or register to post replies

Write your reply to:

Draft