if (!this.getMixinTypes(cache).contains(ModeShapeLexicon.ACCESS_CONTROLLABLE)) {
addMixin(cache, ModeShapeLexicon.ACCESS_CONTROLLABLE);
}
ChildReference aclNodeRef = getChildReferences(cache).getChild(ModeShapeLexicon.ACCESS_LIST_NODE_NAME);
MutableCachedNode aclNode = null;
PropertyFactory propertyFactory = cache.getContext().getPropertyFactory();
ChildReferences permissionsReferences = null;
if (aclNodeRef != null) {
aclNode = cache.mutable(aclNodeRef.getKey());
permissionsReferences = aclNode.getChildReferences(cache);
//there was a previous ACL node present so iterate it and remove all permissions which are not found in the map
for (ChildReference permissionRef : permissionsReferences) {
CachedNode permissionNode = cache.getNode(permissionRef);
String principalName = permissionNode.getProperty(ModeShapeLexicon.PERMISSION_PRINCIPAL_NAME, cache)
.getFirstValue().toString();
if (!privilegesByPrincipalName.containsKey(principalName)) {
permissionChanges().principalRemoved(principalName);
NodeKey permissionNodeKey = permissionNode.getKey();
aclNode.removeChild(cache, permissionNodeKey);
cache.destroy(permissionNodeKey);
}
}
} else {
org.modeshape.jcr.value.Property primaryType = propertyFactory.create(JcrLexicon.PRIMARY_TYPE,
ModeShapeLexicon.ACCESS_LIST_NODE_TYPE);
aclNode = this.createChild(cache, null, ModeShapeLexicon.ACCESS_LIST_NODE_NAME, primaryType);
permissionsReferences = ImmutableChildReferences.EmptyChildReferences.INSTANCE;
}
//go through the new map of permissions and update/create the internal nodes
NameFactory nameFactory = cache.getContext().getValueFactories().getNameFactory();
for (String principal : privilegesByPrincipalName.keySet()) {
Name principalName = nameFactory.create(principal);
ChildReference permissionRef = permissionsReferences.getChild(principalName);
if (permissionRef == null) {
//this is a new principal
permissionChanges().principalAdded(principal);
org.modeshape.jcr.value.Property primaryType = propertyFactory.create(
JcrLexicon.PRIMARY_TYPE, ModeShapeLexicon.PERMISSION);
Property principalProp = propertyFactory.create(ModeShapeLexicon.PERMISSION_PRINCIPAL_NAME,
principal);
Property privileges = propertyFactory.create(ModeShapeLexicon.PERMISSION_PRIVILEGES_NAME,
privilegesByPrincipalName.get(principal));
aclNode.createChild(cache, null, principalName, primaryType, principalProp, privileges);
} else {
//there already is a child node for this principal, so we just need to update its privileges
MutableCachedNode permissionNode = cache.mutable(permissionRef.getKey());
Property privileges = propertyFactory.create(ModeShapeLexicon.PERMISSION_PRIVILEGES_NAME,
privilegesByPrincipalName.get(principal));
permissionNode.setProperty(cache, privileges);
}
}
return permissionChanges();