Copied to clipboard

Flag this post as spam?

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


  • Ron Mast 9 posts 29 karma points
    Apr 22, 2015 @ 06:57
    Ron Mast
    0

    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

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Apr 22, 2015 @ 07:11
    Jan Skovgaard
    0

    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

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 22, 2015 @ 08:15
    Dennis Aaen
    0

    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

  • Ron Mast 9 posts 29 karma points
    Apr 22, 2015 @ 15:46
    Ron Mast
    0

    @Jan

    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 :).

    Thanks for such a quick response...gratitude.

    Ron

  • Ryan 34 posts 138 karma points
    Apr 22, 2015 @ 16:27
    Ryan
    4

    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.

     public static IMemberService MemberService
     {
         get { return ApplicationContext.Current.Services.MemberService; }
     }
    
     public void AssignGroup(IMember member)
     {        
        MemberService.AssignRole(member.Id, "Active");
    
        MemberService.Save(member);
      }
    

    If you have any questions fire away :)

  • Ron Mast 9 posts 29 karma points
    Apr 22, 2015 @ 16:56
    Ron Mast
    0

    Thanks Ryan,

    Sure I have a question :)

    Here's the RegisterMember.cshtml code:

    @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?

    Regards, Ron

  • Ryan 34 posts 138 karma points
    Apr 22, 2015 @ 17:12
    Ryan
    0

    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

     using (Html.BeginUmbracoForm<UmbRegisterController>("HandleRegisterMember"))
    

    Thanks.

  • Ron Mast 9 posts 29 karma points
    Apr 22, 2015 @ 17:16
    Ron Mast
    0

    Hi Ryan,

    Here's the UmbRegisterController code:

    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);
        }
    }
    

    Thanks!

    Ron

  • Ryan 34 posts 138 karma points
    Apr 22, 2015 @ 17:23
    Ryan
    0

    Hi Ron,

    Can you post the full HandleRegisterMember method.

    Thanks

  • Ron Mast 9 posts 29 karma points
    Apr 22, 2015 @ 17:28
    Ron Mast
    0

    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?

  • Ryan 34 posts 138 karma points
    Apr 22, 2015 @ 17:35
    Ryan
    0

    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?

  • Ron Mast 9 posts 29 karma points
    Apr 22, 2015 @ 17:40
    Ron Mast
    0

    Alrighty...back to square one...:)

    I really appreciate your patience.

  • Matthew Kirschner 323 posts 611 karma points
    Jul 09, 2015 @ 19:34
    Matthew Kirschner
    0

    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.

  • Gonzalo Matias Borghi 10 posts 92 karma points
    Mar 18, 2016 @ 13:56
    Gonzalo Matias Borghi
    0

    Someone solved this problem? I also need the solution :) Thx!

  • Glen Kelley 3 posts 73 karma points
    Apr 06, 2016 @ 09:37
    Glen Kelley
    0

    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.

  • Joseph K. 1 post 73 karma points
    Jul 04, 2016 @ 07:53
    Joseph K.
    2

    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).

  • Luke 11 posts 99 karma points
    Sep 01, 2016 @ 07:51
    Luke
    2

    You can achieve this by simply creating an event handler.

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    namespace YourApp.EventHandlers
    {
        public class MemberRegistrationEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                MemberService.Created += MemberService_Created;
            }
    
            private void MemberService_Created(IMemberService sender, NewEventArgs<IMember> e)
            {
                // Always add user to "Main Client" group
                sender.AssignRole(e.Entity.Username, "Main Client");
                sender.Save(e.Entity);
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft