avechuche
avechuche

Reputation: 1560

Umbraco - Add member to group

I need that when you register a member, to put it in a specific group. I believe the "sniper" "Register Members". I'm using the latest version of Umbraco. I was investigating but I can not make it work.

@inherits Umbraco.Web.Macros.PartialViewMacroPage

@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");

}

@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()

@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)
        {
            @*
                It will only displays properties marked as "Member can edit" on the "Info" tab of the Member Type.
            *@
            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>
}

Upvotes: 2

Views: 2643

Answers (3)

lukejkw
lukejkw

Reputation: 1134

You could also achieve this by attaching an event handler to the member created event.

This will allow you to use the register member partial view template that you get out of the box with Umbraco.

Example

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

Note: You might need to add sender.Save(e.Entity) if using an older version of Umbraco

Upvotes: 1

harvzor
harvzor

Reputation: 2908

Assuming you're going the same route as @EyesCream of creating your own custom controller, the new way to do this in Umbraco 7 is:

public class YourCustomUmbController : SurfaceController
{
    [HttpPost]
    public ActionResult RegisterMember(RegisterModel model)
    {
        var memberService = ApplicationContext.Current.Services.MemberService;

        // Create member.
        var member = MemberService.CreateMemberWithIdentity(model.Email, model.Email, model.Name, "Member");

        // Save the member.
        memberService.Save(member);
        memberService.SavePassword(member, model.Password);

        // Assign member to group.
        MemberService.AssignRole(member.Id, "GroupName");
    }

}

Call this by using:

@using (Html.BeginUmbracoForm<YourCustomUmbController>("RegisterMember"))

This uses the new services available in Umbraco rather than the old Umbraco 4 service.

Reference: http://amdonnelly.blogspot.co.uk/2014/06/programmatically-add-new-member-to.html

Tested in Umbraco 7.5.11.

Upvotes: 2

Eyescream
Eyescream

Reputation: 901

You should create a custom controller to handle the logic of your registration, something like that

public class YourCustomUmbController : SurfaceController
{
    [HttpPost]
    public ActionResult RegisterMember(RegisterModel model)
    {
        MembershipCreateStatus status;
        var member = Members.RegisterMember(model, out status, model.LoginOnSuccess);
        ...
        //add roles / groups to the member (it assumes the group admin has been created)
        MemberGroup mg = MemberGroup.GetByName("admin");
        member.AddGroup(mg.Id);     
        ...
    }

}

and call it like so:

@using (Html.BeginUmbracoForm<YourCustomUmbController>("RegisterMember"))

Upvotes: 3

Related Questions