Assign new members to member group upon registration
Hello Everyone,
The registration page works awesome, but I would like to assign the newly registered member to a member group upon registration. How do I do this? Thanks in advance.
If you're using Umbraco 7 then you can create a pre-defined Razor snippet in the "Developer" - "Partial View macro" and when choosing "Create" you can choose from a list of predefined snippets. Try choosing the "Registration Member" snippet.
In here it's described in the comments for the code how the file works and how you can add a member to a certain member group up on creation.
I think to assign new members to member group upon registration, you need to use the API to do that. There is a member service API which has different methods that you can use on members e.g .AssignRole() and so on. You can find the documentation here: https://our.umbraco.org/documentation/Reference/Management-v6/Services/MemberService
Went through the snippet and I'm thinking this is what you meant:
@*
You can specify a custom member type alias in the constructor, the default is 'Member'
for example, to use 'Custom Member' you'd use this syntax:
var registerModel = Members.CreateRegistrationModel("Custom Member");
*@
var registerModel = Members.CreateRegistrationModel();
Only issue is this is for the Member Type...not Member Group :(. Perhaps i'm missing something. I'm already using this snippet for registration :). Basically I want to set the Member's Member Group property to something...in my case I have created an "Active" member group :).
You should be able to adapt the below for your needs, I pass my newly created member object into the AssignGroup method, and then set the member group.
public static IMemberService MemberService
{
get { return ApplicationContext.Current.Services.MemberService; }
}
public void AssignGroup(IMember member)
{
MemberService.AssignRole(member.Id, "Active");
MemberService.Save(member);
}
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Web.Mvc.Html
@using ClientDependency.Core.Mvc
@using Umbraco.Web
@using Umbraco.Web.Controllers
@{
@*
You can specify a custom member type alias in the constructor, the default is 'Member'
for example, to use 'Custom Member' you'd use this syntax:
var registerModel = Members.CreateRegistrationModel("Custom Member");
*@
var registerModel = Members.CreateRegistrationModel();
@*
Configurable here:
registerModel.RedirectUrl - Optional. What path to redirect to if registration is successful.
By default the member will be redirected to the current umbraco page
unless this is specified.
registerModel.UsernameIsEmail - the default is true
if you want the username to be different from the email
address, set this to true and add a new Username field in
the form below
@Html.LabelFor(m => registerModel.Username)
@Html.TextBoxFor(m => registerModel.Username)
@Html.ValidationMessageFor(m => registerModel.Username)
*@
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
Html.RequiresJs("/umbraco_client/ui/jquery.js");
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
var success = TempData["FormSuccess"] != null;
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()
@if (success)
{
@* This message will show if RedirectOnSucces is set to false (default) *@
<p>Registration succeeeded.</p>
}
else
{
using (Html.BeginUmbracoForm<UmbRegisterController>("HandleRegisterMember"))
{
<fieldset>
<legend>Register Member</legend>
@Html.ValidationSummary("registerModel", true)
@Html.LabelFor(m => registerModel.Name)
@Html.TextBoxFor(m => registerModel.Name)
@Html.ValidationMessageFor(m => registerModel.Name)
<br />
@Html.LabelFor(m => registerModel.Email)
@Html.TextBoxFor(m => registerModel.Email)
@Html.ValidationMessageFor(m => registerModel.Email)
<br />
@Html.LabelFor(m => registerModel.Password)
@Html.PasswordFor(m => registerModel.Password)
@Html.ValidationMessageFor(m => registerModel.Password)
<br />
@if (registerModel.MemberProperties != null)
{
for (var i = 0; i < registerModel.MemberProperties.Count; i++)
{
@Html.LabelFor(m => registerModel.MemberProperties[i].Value, registerModel.MemberProperties[i].Name)
@*
By default this will render a textbox but if you want to change the editor template for this property you can
easily change it. For example, if you wanted to render a custom editor for this field called "MyEditor" you would
create a file at ~/Views/Shared/EditorTemplates/MyEditor.cshtml", then you will change the next line of code to
render your specific editor template like:
@Html.EditorFor(m => profileModel.MemberProperties[i].Value, "MyEditor")
*@
@Html.EditorFor(m => registerModel.MemberProperties[i].Value)
@Html.HiddenFor(m => registerModel.MemberProperties[i].Alias)
<br />
}
}
@Html.HiddenFor(m => registerModel.MemberTypeAlias)
@Html.HiddenFor(m => registerModel.RedirectUrl)
@Html.HiddenFor(m => registerModel.UsernameIsEmail)
<button>Register</button>
</fieldset>
}
}
How would I bake your code into this to make it work?
Unfortunately the code I provided can't live inside the view - Instead we need to put it inside a surface controller or class. Could you paste your code found inside 'UmbRegisterController', specifically 'HandleRegisterMember'.
We know that's where your handling the registration because of
using (Html.BeginUmbracoForm<UmbRegisterController>("HandleRegisterMember"))
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
namespace Umbraco.Web.Controllers
{
public class UmbRegisterController : SurfaceController
{
public UmbRegisterController();
[HttpPost]
public ActionResult HandleRegisterMember(RegisterModel model);
}
}
I've never used Umbraco through WebMatrix and instead always install it via nuget in Visual Studio. I think Umbraco via Visual Studio is probably the best way for you to go, although I still don't think you'll have access to the controller.It wouldn't take much work to create a custom registration page and controller to handle the logic.
It's worth pointing out this won't happen out of the box and will require coding experience and some time :P.
I'd love to use the default code and snippets provided by Umbraco, but I can't see how to add the extra functionality (adding a group to a member, etc...) without rolling my own controller actions.
I couldn't find a way of doing this built in to umbraco, which was a shame as it was required for a project where different email addresses had to be assigned to different groups based on domain.
I first tried putting code on the template/view that would assign the user to a group on login or after signup, but this just seemed wrong (actual c# on templates and partials tends to annoy me in that it's not very MVC) and came with it's own problems.
In the end I wrote a set of classes for the membership service to add this functionality to the model and surface controller as well as an extension class for MembershipHelper to generate it.
I've uploaded the dll to the following location - this was compiled against 7.3.8 but has worked on earlier and later versions, and here's the reworked Register Member partial:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Web.Mvc.Html
@using ClientDependency.Core.Mvc
@using Umbraco.Web
@using BlueprintUMEx.Web.Models
@using BlueprintUMEx.Web.Controllers
@using BlueprintUMEx.Web.Security.Extensions
@{
@*
You can specify a custom member type alias in the constructor, the default is 'Member'
for example, to use 'Custom Member' you'd use this syntax:
var registerExtendedModel = Members.CreateExtendedRegistrationModel("Custom Member");
You can specify a custom member group in the constructor, the default is None
for example, to use 'Custom Group' you'd use this syntax:
(leaving first parameter empty is the same as supplying the default type)
var registerExtendedModel = Members.CreateExtendedRegistrationModel("", "Custom Group");
*@
var registerExtendedModel = Members.CreateExtendedRegistrationModel("Custom Member", "Custom Group");
@*
Configurable here:
registerExtendedModel.RedirectUrl - Optional. What path to redirect to if registration is successful.
By default the member will be redirected to the current umbraco page
unless this is specified.
registerExtendedModel.UsernameIsEmail - the default is true
if you want the username to be different from the email
address, set this to true and add a new Username field in
the form below
@Html.LabelFor(m => registerExtendedModel.Username)
@Html.TextBoxFor(m => registerExtendedModel.Username)
@Html.ValidationMessageFor(m => registerExtendedModel.Username)
*@
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
Html.RequiresJs("/umbraco_client/ui/jquery.js");
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
var success = TempData["FormSuccess"] != null;
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()
@if (success)
{
@* This message will show if RedirectOnSucces is set to false (default) *@
<p>Registration succeeeded.</p>
}
else
{
using (Html.BeginUmbracoForm<UmbExtendedRegisterController>("HandleExtendedRegisterMember"))
{
<fieldset>
<legend>Register Member</legend>
@Html.ValidationSummary("registerModel", true)
@Html.LabelFor(m => registerExtendedModel.Name)
@Html.TextBoxFor(m => registerExtendedModel.Name)
@Html.ValidationMessageFor(m => registerExtendedModel.Name)
<br />
@Html.LabelFor(m => registerExtendedModel.Email)
@Html.TextBoxFor(m => registerExtendedModel.Email)
@Html.ValidationMessageFor(m => registerExtendedModel.Email)
<br />
@Html.LabelFor(m => registerExtendedModel.Password)
@Html.PasswordFor(m => registerExtendedModel.Password)
@Html.ValidationMessageFor(m => registerExtendedModel.Password)
<br />
@if (registerExtendedModel.MemberProperties != null)
{
@*
It will only displays properties marked as "Member can edit" on the "Info" tab of the Member Type.
*@
for (var i = 0; i < registerExtendedModel.MemberProperties.Count; i++)
{
@Html.LabelFor(m => registerExtendedModel.MemberProperties[i].Value, registerExtendedModel.MemberProperties[i].Name)
@*
By default this will render a textbox but if you want to change the editor template for this property you can
easily change it. For example, if you wanted to render a custom editor for this field called "MyEditor" you would
create a file at ~/Views/Shared/EditorTemplates/MyEditor.cshtml", then you will change the next line of code to
render your specific editor template like:
@Html.EditorFor(m => profileModel.MemberProperties[i].Value, "MyEditor")
*@
@Html.EditorFor(m => registerExtendedModel.MemberProperties[i].Value)
@Html.HiddenFor(m => registerExtendedModel.MemberProperties[i].Alias)
<br />
}
}
@Html.HiddenFor(m => registerExtendedModel.MemberTypeAlias)
@Html.HiddenFor(m => registerExtendedModel.MemberGroupAlias) @* We now need to supply the group parameter *@
@Html.HiddenFor(m => registerExtendedModel.RedirectUrl)
@Html.HiddenFor(m => registerExtendedModel.UsernameIsEmail)
<button>Register</button>
</fieldset>
}
}
I apologise for not having the source available for people to learn from, if enough people want/use it I might package it up but I do need to create it as a stand alone project (it's references and such are tied in to the big project) before I can put it on github or something similar.
My solution works for me but I'd like to know if anyone can think of a reason not to solve this problem this way.
Register Member snippet code begins...
@if (success)
{
var m = Membership.GetUser();
ApplicationContext.Current.Services.MemberService.AssignRole((int)m.ProviderUserKey, "Unregistered");
Response.Redirect(redirectNode.Url);
}
else... The snippet continues.
As you can see I'm using the success block of the Register Member snippet to get the current user. Then, using the static MemberService.AssignRole() I add the user to my desired role(s). Next I use the redirectNode.Url to send the user to the protected area where I want them to complete registration (the registrant can't get into the protected area without being added to the desired role first).
Assign new members to member group upon registration
Hello Everyone,
The registration page works awesome, but I would like to assign the newly registered member to a member group upon registration. How do I do this? Thanks in advance.
Regards, Ron
Hi Ron
If you're using Umbraco 7 then you can create a pre-defined Razor snippet in the "Developer" - "Partial View macro" and when choosing "Create" you can choose from a list of predefined snippets. Try choosing the "Registration Member" snippet.
In here it's described in the comments for the code how the file works and how you can add a member to a certain member group up on creation.
Hope this helps
/Jan
Hi Ron,
I think to assign new members to member group upon registration, you need to use the API to do that. There is a member service API which has different methods that you can use on members e.g .AssignRole() and so on. You can find the documentation here: https://our.umbraco.org/documentation/Reference/Management-v6/Services/MemberService
Hope this helps,
/Dennis
@Jan
Went through the snippet and I'm thinking this is what you meant:
Only issue is this is for the Member Type...not Member Group :(. Perhaps i'm missing something. I'm already using this snippet for registration :). Basically I want to set the Member's Member Group property to something...in my case I have created an "Active" member group :).
Thanks for such a quick response...gratitude.
Ron
Hi,
You should be able to adapt the below for your needs, I pass my newly created member object into the AssignGroup method, and then set the member group.
If you have any questions fire away :)
Thanks Ryan,
Sure I have a question :)
Here's the RegisterMember.cshtml code:
How would I bake your code into this to make it work?
Regards, Ron
Hi Ron,
Unfortunately the code I provided can't live inside the view - Instead we need to put it inside a surface controller or class. Could you paste your code found inside 'UmbRegisterController', specifically 'HandleRegisterMember'.
We know that's where your handling the registration because of
Thanks.
Hi Ryan,
Here's the UmbRegisterController code:
Thanks!
Ron
Hi Ron,
Can you post the full HandleRegisterMember method.
Thanks
Perhaps I should explain how I have Umbraco installed and opened in VS.
I used WebMatrix to install an instance of Umbraco and opened as web site in VS.
Right now when I right click and attempt to go to definition for HandleRegisterMember method, it doesn't do anything.
Do I have to install Umbraco a different way? By that I mean, create new MVC project and bring in UmbracoCMS through nuget.
Is that my problem?
That would explain it!
I've never used Umbraco through WebMatrix and instead always install it via nuget in Visual Studio. I think Umbraco via Visual Studio is probably the best way for you to go, although I still don't think you'll have access to the controller.It wouldn't take much work to create a custom registration page and controller to handle the logic.
It's worth pointing out this won't happen out of the box and will require coding experience and some time :P.
Sound OK?
Alrighty...back to square one...:)
I really appreciate your patience.
Hi, Ron. Did you ever find a solution for this?
I'd love to use the default code and snippets provided by Umbraco, but I can't see how to add the extra functionality (adding a group to a member, etc...) without rolling my own controller actions.
Someone solved this problem? I also need the solution :) Thx!
I couldn't find a way of doing this built in to umbraco, which was a shame as it was required for a project where different email addresses had to be assigned to different groups based on domain.
I first tried putting code on the template/view that would assign the user to a group on login or after signup, but this just seemed wrong (actual c# on templates and partials tends to annoy me in that it's not very MVC) and came with it's own problems.
In the end I wrote a set of classes for the membership service to add this functionality to the model and surface controller as well as an extension class for MembershipHelper to generate it.
I've uploaded the dll to the following location - this was compiled against 7.3.8 but has worked on earlier and later versions, and here's the reworked Register Member partial:
I apologise for not having the source available for people to learn from, if enough people want/use it I might package it up but I do need to create it as a stand alone project (it's references and such are tied in to the big project) before I can put it on github or something similar.
My solution works for me but I'd like to know if anyone can think of a reason not to solve this problem this way.
As you can see I'm using the success block of the Register Member snippet to get the current user. Then, using the static MemberService.AssignRole() I add the user to my desired role(s). Next I use the redirectNode.Url to send the user to the protected area where I want them to complete registration (the registrant can't get into the protected area without being added to the desired role first).
You can achieve this by simply creating an event handler.
is working on a reply...