Skip to content

Instantly share code, notes, and snippets.

@wgthom
Created February 20, 2016 16: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 wgthom/5807e6c2b1900e432400 to your computer and use it in GitHub Desktop.
Save wgthom/5807e6c2b1900e432400 to your computer and use it in GitHub Desktop.
attributeAssign_addAttributeAssign {
/**
* On assignment of the syncAttribute marker, create all the groups or group (if directly assigned)
* and any current memberships at the target.
*/
public void process(ChangeLogEntry changeLogEntry, ChangeLogConsumerBaseImpl consumer) {
// check if this is our syncAttribute, otherwise nothing to do.
final String attributeDefNameName = changeLogEntry.retrieveValueForLabel(ChangeLogLabels.ATTRIBUTE_ASSIGN_ADD.attributeDefNameName);
if (consumer.syncAttribute.getName().equals(attributeDefNameName)) {
// syncAttribute applied to a group, then create the group at the target
String assignType = changeLogEntry.retrieveValueForLabel(ChangeLogLabels.ATTRIBUTE_ASSIGN_ADD.assignType);
String ownerId1 = changeLogEntry.retrieveValueForLabel(ChangeLogLabels.ATTRIBUTE_ASSIGN_ADD.ownerId1);
if (GROUP.equals(assignType)){
Group markedGroup = GroupFinder.findByUuid(GrouperSession.staticGrouperSession(false), ownerId1, false );
if (markedGroup != null) {
consumer.createGroupAndMemberships(markedGroup, changeLogEntry, consumer.consumerName);
} // couldn't find group, already deleted?
} else if (FOLDER.equals(assignType)){
// syncAttribute applied to a folder, get all the groups below this folder and sub folders and create them at the target
Stem markedFolder = StemFinder.findByUuid(GrouperSession.staticGrouperSession(false), ownerId1, false);
if (markedFolder != null) {
Set<Group> markedGroups = markedFolder.getChildGroups(Stem.Scope.SUB);
for( Group group : markedGroups) {
consumer.createGroupAndMemberships(group, changeLogEntry, consumer.consumerName);
}
} // couldn't find folder, already deleted?
}
} else {
// not our syncAttribute, so nothing to do
LOG.debug("{} skipping addAttributeAssign, {} is not our syncAttribute");
}
}
},
attributeAssign_deleteAttributeAssign {
/**
* On removal of the syncAttribute marker, delete all the groups or group (if directly assigned) at the target, unless
* otherwise still marked by direct assignment or a parent folder.
*/
public void process(ChangeLogEntry changeLogEntry, ChangeLogConsumerBaseImpl consumer) {
// is this our syncAttribute? otherwise nothing to do.
final String attributeDefNameName = changeLogEntry.retrieveValueForLabel(ChangeLogLabels.ATTRIBUTE_ASSIGN_DELETE.attributeDefNameName);
if (consumer.syncAttribute.getName().equals(attributeDefNameName)) {
String assignType = changeLogEntry.retrieveValueForLabel(ChangeLogLabels.ATTRIBUTE_ASSIGN_DELETE.assignType);
String ownerId1 = changeLogEntry.retrieveValueForLabel(ChangeLogLabels.ATTRIBUTE_ASSIGN_DELETE.ownerId1);
// is it for a group? then delete the group at the target, unless a parent folder is also marked.
if (GROUP.equals(assignType)) {
// get the group and then check for a marked parent folder before deleting at target.
Group group = GroupFinder.findByUuid(GrouperSession.staticGrouperSession(false), ownerId1, false);
if (group != null){
// case when group had a direct syncAttribute marker assignment removed, does it still have a parent marker?
if (group.getAttributeDelegate().hasAttributeOrAncestorHasAttribute(consumer.syncAttribute.getName(), false)) {
LOG.debug("{} processed deleteAttributeAssign {} for group {}, but still marked by a parent folder.", new Object[]{consumer.consumerName, attributeDefNameName, group.getName()});
} else {
// marker syncAttribute removed from group and no other parent folder marked so delete at target
LOG.debug("{} processed deleteAttributeAssign {} for group {}, no other mark found so calling deleteGroup", new Object[]{consumer.consumerName, attributeDefNameName ,group.getName())};
consumer.deleteGroup(group.getName(), changeLogEntry, consumer.consumerName);
}
} else {
// case when a group which had a direct syncAttribute marker was deleted, always delete at target
PITGroup pitGroup = PITGroupFinder.findBySourceId(ownerId1, false).iterator().next();
if (pitGroup != null) {
String pitGroupName = pitGroup.getName();
// marker syncAttribute removed when deleting a group, always delete at target
LOG.debug("{} processed deleteAttributeAssign {} for deleted group {}, calling deleteGroup", new Object[]{consumer.consumerName, attributeDefNameName ,pitGroupName});
consumer.deleteGroup(pitGroupName, changeLogEntry, consumer.consumerName);
} else {
// couldn't find group anywhere? so can't determine its name.
LOG.error("{} failed to find group when processing deleteAttributeAssign so can't determine the group name, let fullSync sort it out.", consumer.consumerName);
}
}
} else if (FOLDER.equals(assignType)) {
// is it a folder, then delete all the containing groups at the target, unless they are still marked otherwise (direct or indirect)
Stem unMarkedFolder = StemFinder.findByUuid(GrouperSession.staticGrouperSession(false), ownerId1, false);
if (unMarkedFolder != null) {
// get all the groups below this folder and sub folders and to see if they are still marked, otherwise delete them at the target
Set<Group> unMarkedGroups = unMarkedFolder.getChildGroups(Stem.Scope.SUB);
for (Group group : unMarkedGroups) {
// check that the group isn't marked directly or from some other parent folder
if (consumer.isGroupMarkedForSync(group.getName())) {
LOG.debug("{} processed deleteAttributeAssign {} for folder {}, found mark for group {} so nothing to do.", new Object[]{consumer.consumerName, attributeDefNameName, unMarkedFolder.getName(), group.getName()});
} else {
LOG.debug("{} processed deleteAttributeAssign {} for folder {}, no other mark found for group {} so calling deleteGroup", new Object[]{consumer.consumerName, attributeDefNameName, unMarkedFolder.getName(), group.getName(), group.getName()});
consumer.deleteGroup(group.getName(), changeLogEntry, consumer.consumerName);
}
}
} else {
// couldn't find folder, already deleted? let fullSync sort it out.
// shouldn't get here...can't delete a folder without first deleting child objects, so there should be nothing to delete at the target
LOG.error("{} error processing deleteAttributeAssign for folder {}, let fullSync sort it out.", consumer.consumerName, unMarkedFolder.getName());
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment