Copied to clipboard

Flag this post as spam?

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


  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Mar 26, 2015 @ 13:30
    Søren Kottal
    0

    AssignRole not saving

    I am trying to automatically assign new members to a specific membergroup.

     

    I have written this code, to hook into MemberService.Created, but the new group isn't saved to the member (when looking in the back office). Am I doing something wrong?

     

    The Log.Add is fired, and I can see in my db that both Id and Name of the new member is correct.

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using umbraco.BusinessLogic;
    
    namespace Umbraco_Site_Extensions.memberExtensions
    {
        public class RegisterEvents : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                MemberService.Created += MemberService_Created;
            }
    
            void MemberService_Created(Umbraco.Core.Services.IMemberService sender, Umbraco.Core.Events.NewEventArgs e)
            {
                sender.AssignRole(e.Entity.Id, "Group name");
                Log.Add(LogTypes.Notify, 1, "Added " + e.Entity.Id + " - " + e.Entity.Name + " to Group name.");
            }
        }
    }
    

     

  • David SE 6 posts 28 karma points
    Mar 30, 2015 @ 10:15
    David SE
    0

    This behavior is known, I've noticed as well and have found older posts confirming it. To me it seems like a bug, it's certainly not the expected behavior.

    I noticed that if I debug my code, and look in the database for every line of code, I can see that the member and group is added to the relationship table. But once the callback method reaches its end, that database entry is removed. Why? I have no idea. I've been wanting to inspect Umbraco's source and see what happens but havent had the time. Perhaps this coming weekend :)

  • FriendlyWebGuy 3 posts 23 karma points
    May 06, 2015 @ 13:01
    FriendlyWebGuy
    0

    @David SE - Hi David, did you ever manage to find out what was causing the issue? I've been trying to use .AssignRole this morning and found it wasn't saving, after a bit of searching I eventually found this tread which shows its not just me getting stuck.

    Cheers,
    Dave G 

  • Matt 91 posts 237 karma points
    Jun 17, 2015 @ 09:11
    Matt
    0

    Was this ever solved?

  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Jun 17, 2015 @ 09:26
    Søren Kottal
    0

    Unfortunately not :(

  • Matt 91 posts 237 karma points
    Jun 17, 2015 @ 09:28
    Matt
    0

    Did you manage to find a workaround Søren?

    After looking on the issue tracker it seems many people have had the problem for over a year - not specifically assignrole, it is any member property.

  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Jun 17, 2015 @ 09:37
    Søren Kottal
    0

    No, instructed the client to remember to add new members to the specific group.

  • Alejandro 23 posts 45 karma points
    Jul 08, 2015 @ 14:05
    Alejandro
    0

    I made a post yesterday showing the same behavior while trying to save a property: https://our.umbraco.org/forum/developers/extending-umbraco//67380-cannot-update-member-property-on-created-event

    Is there a workaround for this? Thanks!

  • Matt 91 posts 237 karma points
    Jul 08, 2015 @ 14:07
    Matt
    0

    I never found a solution Alejandro, unfortunately.

  • Alejandro 23 posts 45 karma points
    Jul 08, 2015 @ 14:36
    Alejandro
    0

    Thanks Matt.

    I checked the tracker and the issue was reported on March 05. http://issues.umbraco.org/issue/U4-6366

  • Mark 1 post 71 karma points
    Aug 09, 2015 @ 17:26
    Mark
    0

    I'm on 7.2.1 and it looks like this is an issue for OnSaving/OnSaved as well.

    Looking at the umbraco source code, it looks like in Umbraco.Web.Editors.MemberController.PostSave, the code Create/Updates the member, which I'm pretty sure triggers the event handlers. After that completes, it then removes and adds membership groups based on what is passed in, presumably what's coming from the page; so that's overwriting any Roles/membership groups are being done in the event handler.

    I can't find any work around to it.

Please Sign in or register to post replies

Write your reply to:

Draft