I want to stop publishing a structure groups for certain user groups. I'm able to catch the structure group and filter per the desired groups, but when I throw the exception, nothing happens and the structure group is sent to the publish queue.
private void SubScribe()
{
EventSystem.Subscribe<StructureGroup, PublishOrUnPublishEventArgs>(OnPublishTransactionPublish, EventPhases.Initiated);
}
private void OnPublishTransactionPublish(StructureGroup structureGroup, PublishOrUnPublishEventArgs e, EventPhases phase)
{
if (structureGroup == null)
return;
// get the person who created the publish transaction (= the one who published the item)
var creator = new User(structureGroup.Session.AccessToken.Id, structureGroup.Session);
var groups = creator.GroupMemberships;
var structureGroupId = structureGroup.Id.ItemId.ToString();
if (structureGroupId == tcmHomeStructureGroup)
{
if (!groups.Where(x => x.Group.Title == homeAllowedGroup).Any())
{
ResourceManager rm = new ResourceManager("MijnPostNL.Tridion.Addons", typeof(Resources).Assembly);
throw new PublisherException(new LocalizableMessage(rm, "cannotPublish", new object[] { structureGroup.Id }));
}
}
if (structureGroupId == tcmPersoneelsinfoStructureGroup)
{
if (groups.Where(x => personeelsinfoNotAllowedGroups.Contains(x.Group.Title)).Any())
{
throw new PublisherException(new LocalizableMessage(Resources.ResourceManager, "cannotPublish", new object[] { structureGroup.Id }));
}
}
}
Session.AccessToken
directly rather than loading a newUser
object by ID (in that way you loose the expanded group memberships).