Skip to content

Instantly share code, notes, and snippets.

@jamiepollock
Last active June 27, 2016 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiepollock/673c960199d5637510bb143e529b9e60 to your computer and use it in GitHub Desktop.
Save jamiepollock/673c960199d5637510bb143e529b9e60 to your computer and use it in GitHub Desktop.
ProtectedContentNotifyEventHandler an Umbraco event which will look for any members who have access to the current content node being saved then send an email to them
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace DemoApp
{
public class ProtectedContentNotifyEventHandler : ApplicationEventHandler
{
private static IEnumerable<string> _validMemberGroups = new[] {"MemberGroup1", "MemberGroup2" };
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Published += ContentService_Published;
//ContentService.Saved += ContentService_Saved;
base.ApplicationStarted(umbracoApplication, applicationContext);
}
private void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e)
{
ProcessMultipleContentItems(e.PublishedEntities);
}
private void ContentService_Saved(IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
{
ProcessMultipleContentItems(e.SavedEntities);
}
private void ProcessMultipleContentItems(IEnumerable<IContent> savedEntities)
{
var publicAccessService = ApplicationContext.Current.Services.PublicAccessService;
var memberService = ApplicationContext.Current.Services.MemberService;
foreach (var savedEntity in savedEntities)
{
if (publicAccessService.IsProtected(savedEntity))
{
var entry = publicAccessService.GetEntryForContent(savedEntity);
if (entry != null)
{
var members = new List<IMember>();
foreach (var accessRule in entry.Rules)
{
var isMemberGroupTypeRule = string.Equals(accessRule.RuleType,
Constants.Conventions.PublicAccess.MemberRoleRuleType,
StringComparison.OrdinalIgnoreCase);
var isMemberGroupValid = _validMemberGroups.Contains(accessRule.RuleValue);
if (isMemberGroupTypeRule && isMemberGroupValid)
{
members.AddRange(memberService.GetMembersByGroup(accessRule.RuleValue));
}
}
if (members.Any())
{
SendEmailToMembers(members.Distinct());
}
}
}
}
}
private void SendEmailToMembers(IEnumerable<IMember> members)
{
foreach (var member in members)
{
//use member.Email to send an email off to the member
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment