Add VerificationToken for new members

This commit is contained in:
2019-12-16 14:42:55 -05:00
parent 7a4946f4f4
commit 0139d66d63
10 changed files with 130 additions and 63 deletions
+52
View File
@@ -0,0 +1,52 @@
using Hangfire;
using LeafWeb.WebCms.Services;
using MlkPwgen;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace LeafWeb.WebCms.EventHandlers
{
// https://our.umbraco.com/forum/umbraco-7/using-umbraco-7/64185-Assign-new-members-to-member-group-upon-registration#comment-254768
public class MemberEvents : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
MemberService.Created += MemberService_Created;
MemberService.Saving += MemberService_OnSaving;
}
private void MemberService_OnSaving(IMemberService sender, SaveEventArgs<IMember> e)
{
foreach (var member in e.SavedEntities)
{
// Member is not approved, don't send an email at all
if (!member.HasIdentity || !member.IsApproved)
continue;
// Pull the old approval state from the member service (this is the value before the save has updated the cache)
var alreadyApproved = ApplicationContext.Current.Services.MemberService.GetById(member.Id).IsApproved;
//Member wasn't approved before save but is now
if (!alreadyApproved)
{
//Code to send email goes here
}
}
}
private void MemberService_Created(IMemberService sender, NewEventArgs<IMember> e)
{
e.Entity.IsApproved = false;
var token = PasswordGenerator.Generate(12, allowed: "0123456789");
e.Entity.SetValue("VerificationToken", token);
sender.Save(e.Entity);
// Send Email
BackgroundJob.Enqueue<EmailNotificationService>(
email => email.SendVerifyMemberEmail(e.Entity.Email));
}
}
}
@@ -1,29 +0,0 @@
using Hangfire;
using LeafWeb.WebCms.Services;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace LeafWeb.WebCms.EventHandlers
{
// https://our.umbraco.com/forum/umbraco-7/using-umbraco-7/64185-Assign-new-members-to-member-group-upon-registration#comment-254768
public class MemberRegistrationEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
MemberService.Created += MemberService_Created;
}
private void MemberService_Created(IMemberService sender, NewEventArgs<IMember> e)
{
sender.AssignRole(e.Entity.Username, "Unverified");
sender.Save(e.Entity);
// Send Email
BackgroundJob.Enqueue<EmailNotificationService>(
email => email.SendVerifyMemberEmail(e.Entity));
}
}
}