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.
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.
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:
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?
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.
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:
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.
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.
If someone could help me with this issue, I would be very grateful.
Thanks!
Also having troube with this.
Also, MemberService has these two methods:
Any ideas on this? Thank you!
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:
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:
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?
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.
Thanks for your post!
Can't get it to work though.
If I run this in a controller, it works.
But not this:
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,
@David SE
Try to call "Save" again after assigning the MemberGroup
In my code I had this and it's working correctly:
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:
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.
Was this ever resolved?
Having the same problem - can't assign a member to a group on the Created event.
@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.
is working on a reply...