Umbraco - 将成员添加到组

时间:2016-03-18 18:41:13

标签: umbraco umbraco7

当您注册会员时,我需要将其放入特定的群组中。我相信"狙击手" "注册会员"。我使用的是最新版本的Umbraco。我正在调查,但我不能让它发挥作用。

$ composer global require fxp/composer-asset-plugin:"^1.1.0" --no-plugins

3 个答案:

答案 0 :(得分:2)

您应该创建一个自定义控制器来处理您的注册逻辑,类似

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

}

并称之为:

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

答案 1 :(得分:1)

假设您与创建自己的自定义控制器的@EyesCream路径相同,在Umbraco 7中执行此操作的新方法是:

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

}

使用以下方式调用此方法:

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

这使用了Umbraco中提供的新服务,而不是旧的Umbraco 4服务。

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

在Umbraco 7.5.11中测试过。

答案 2 :(得分:0)

您也可以通过简单地将事件处理程序附加到成员创建的事件来实现此目的。这将允许您简单地使用Umbraco v7开箱即用的注册成员部分视图模板。

事件处理程序代码:

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

}