Copied to clipboard

Flag this post as spam?

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


  • Ruben Verhaaf 4 posts 34 karma points
    May 08, 2014 @ 15:31
    Ruben Verhaaf
    0

    Add Member to MemberGroup in MemberService.Created handler

    Can anyone help me with this issue? I would like my members to be added to a default group when created, so I can safely use that group for securing content nodes. I have tried to add an event handler as follows to add the members to that group, but it does not do anything. The code is run however, as I can clearly see my breakpoints being hit when debugging.

    void MemberService_Created(IMemberService sender, Umbraco.Core.Events.NewEventArgs<Umbraco.Core.Models.IMember> e)
    {
        sender.AssignRole(e.Entity.Id, "Vineyard");
        sender.Save(e.Entity);
    }

    If someone could help me with this issue, I would be very grateful.

    Thanks!

  • Jesper 8 posts 51 karma points
    Jul 09, 2014 @ 12:50
    Jesper
    0

    Also having troube with this.

            private void MemberService_Saved(IMemberService sender, SaveEventArgs<IMember> e)
            {
                foreach (var member in e.SavedEntities)
                {
                    if (member.IsNewEntity())
                    {
                        if (member.ContentTypeAlias == "Auditor")
                        {
                            sender.AssignRole(member.Id, "AuditorGroup");
                            sender.Save(member);
                        }
                    }
                }
            }

    Also, MemberService has these two methods:

    sender.GetAllRoles(); <-- Return "AuditorGroup" as the only item of the IEnumerable (as it's the only MemberGroup thers is)
    Role == Group? 
    sender.GetMembersByGroup("AuditorGroup") <--  Gets me only the members set manually in Backoffice, not by using the MemberService.
    It doesn't seem to persist role assignment.

    Any ideas on this? Thank you!

  • Robby Cowell 20 posts 103 karma points
    Aug 18, 2014 @ 12:59
    Robby Cowell
    0

    I'm also having this issue. Everything is working as it should with the exception of the Member Service's AssignRole method, I am using it like this:

    public ActionResult CreateAgent(Agent agent){
        var memberService = Services.MemberService;
        var member = memberService.CreateMember(agent.CompanyName, agent.Email, agent.ContactName, "Agent");
    
        member.SetValue("companyName", agent.CompanyName);
        ...
        memberService.Save(member);
        memberService.AssignRole(member.Id, "Agent");
    
        return Redirect("/");
    }
    

    I then go into the 'Members' section of Umbraco, select the newly created agent, and click on the properties tab, the member is still not part of the 'Agent' group:

    enter image description here

    I've stepped through the code and it runs with no errors.

    Am I missing something here, or is this potentially a bug with the Member Service?

  • bob baty-barr 1180 posts 1294 karma points MVP
    Aug 22, 2014 @ 15:50
    bob baty-barr
    0

    in version 7.1.4 i have this code working... i just have this as a class in my app_code folder

    with the help of the amazing Casey Neehouse, i was able to add a class to app_code to assign a new member to a role when created.

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    namespace MyApplication.Events
    {
    public class MemberEvents : ApplicationEventHandler
    {
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
    Umbraco.Core.Services.MemberService.Created += MemberService_Created;
    }

    void MemberService_Created(Umbraco.Core.Services.IMemberService sender, Umbraco.Core.Events.NewEventArgs e)
    {
    sender.AssignRole(e.Entity.Id, "Site Visitors");
    }

    }

    }
  • Jesper 8 posts 51 karma points
    Oct 02, 2014 @ 00:05
    Jesper
    0

    Thanks for your post!

    Can't get it to work though. 

    If I run this in a controller, it works. 

    IMember _newMember = ApplicationContext.Current.Services.MemberService.CreateMember("username", "[email protected]", "Full Name", "Member")
    Services.MemberService.Save(_newMember);
    Services.MemberService.AssignRole(_newMember.Id, "Group Name"); 

    But not this:

     private void MemberService_Created(IMemberService sender, NewEventArgs<IMember> e)
           {
               var member = e.Entity;
               if (member.IsNewEntity())
               {
                   if (member.ContentTypeAlias == "Auditor")
                   {
                       sender.AssignRole(member.Id, "Auditors");
                  }
    }
            sender.AssignRole(member.Id, "test group");
          } 
  • David SE 6 posts 28 karma points
    Jan 28, 2015 @ 09:33
    David SE
    0

    I'm having the same issue. The code does not throw any exceptions but when you go into Umbraco and look at the member, the user has not been added to the member group. The member group is created if it does not exist but the member is not being added to it.

    Edit:

    Okay, so I've looked in the database and the role is created but in the cmsMember2MemberGroup relation table there is no entry.

    I've looked at the code in the membergroup repository but it looks ok to me. Seems to be there's a bug there. I'll try and find it.

    Thanks,

  • Rob Scott 41 posts 94 karma points
    Feb 04, 2015 @ 19:48
    Rob Scott
    0

    @David SE

    Try to call "Save" again after assigning the MemberGroup

    IMember newMember = ApplicationContext.Current.Services.MemberService.CreateMember("username", "[email protected]", "Full Name", "Member")
    Services.MemberService.Save(newMember);
    Services.MemberService.AssignRole(newMember.Id, "Group Name"); 
    Services.MemberService.Save(newMember);
    

    In my code I had this and it's working correctly:

    1. Create new member
    2. Save member
    3. Assign Role
    4. Added additional custom properties
    5. Save member
  • Ruben Verhaaf 4 posts 34 karma points
    Feb 07, 2015 @ 11:56
    Ruben Verhaaf
    0

    Well, it does work if I run the code somewhere in a SurfaceController (just like Jesper said). But performing it in a member created event still does not work:

    public class RegisterEvents : ApplicationEventHandler
    {
        private bool _hasRun = false;
    
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            if (!_hasRun)
            {
                AttachApplicationStartedEventHandlers();
                _hasRun = true;
            }
        }
    
        private void AttachApplicationStartedEventHandlers()
        {
            MemberService.Created += MemberService_Created;
        }
    
        void MemberService_Created(IMemberService sender, Umbraco.Core.Events.NewEventArgs e)
        {
            sender.Save(e.Entity);
            sender.AssignRole(e.Entity.Id, "Vineyard");
            sender.Save(e.Entity);
        }
    }

    For now I think I'll resort to regularly calling some code that checks all members are in the group they should be in. Otherwise, I don't know how to fix my issue.

  • Matt 91 posts 237 karma points
    Jun 16, 2015 @ 10:30
    Matt
    0

    Was this ever resolved?

    Having the same problem - can't assign a member to a group on the Created event.

  • Phil Gilligan 25 posts 136 karma points
    Jul 16, 2015 @ 13:56
    Phil Gilligan
    0

    @Matt, it looks like there is an issue open for this in bug tracker:

    http://issues.umbraco.org/issue/U4-6366

    If we all vote for it up, it should raise the priority of a potential fix.

Please Sign in or register to post replies

Write your reply to:

Draft