use of org.apache.syncope.common.lib.to.RelationshipTO in project syncope by apache.
the class AnyOperations method diff.
/**
* Calculate modifications needed by first in order to be equal to second.
*
* @param updated updated AnyObjectTO
* @param original original AnyObjectTO
* @param incremental perform incremental diff (without removing existing info)
* @return AnyObjectPatch containing differences
*/
public static AnyObjectPatch diff(final AnyObjectTO updated, final AnyObjectTO original, final boolean incremental) {
AnyObjectPatch result = new AnyObjectPatch();
diff(updated, original, result, incremental);
// 1. name
result.setName(replacePatchItem(updated.getName(), original.getName(), new StringReplacePatchItem()));
// 2. relationships
Map<Pair<String, String>, RelationshipTO> updatedRels = EntityTOUtils.buildRelationshipMap(updated.getRelationships());
Map<Pair<String, String>, RelationshipTO> originalRels = EntityTOUtils.buildRelationshipMap(original.getRelationships());
updatedRels.entrySet().stream().filter(entry -> (!originalRels.containsKey(entry.getKey()))).forEachOrdered(entry -> {
result.getRelationships().add(new RelationshipPatch.Builder().operation(PatchOperation.ADD_REPLACE).relationshipTO(entry.getValue()).build());
});
if (!incremental) {
originalRels.keySet().stream().filter(relationship -> !updatedRels.containsKey(relationship)).forEach(key -> {
result.getRelationships().add(new RelationshipPatch.Builder().operation(PatchOperation.DELETE).relationshipTO(originalRels.get(key)).build());
});
}
// 3. memberships
Map<String, MembershipTO> updatedMembs = EntityTOUtils.buildMembershipMap(updated.getMemberships());
Map<String, MembershipTO> originalMembs = EntityTOUtils.buildMembershipMap(original.getMemberships());
updatedMembs.entrySet().stream().filter(entry -> (!originalMembs.containsKey(entry.getKey()))).forEachOrdered(entry -> {
result.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(entry.getValue().getGroupKey()).build());
});
if (!incremental) {
originalMembs.keySet().stream().filter(membership -> !updatedMembs.containsKey(membership)).forEach(key -> {
result.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.DELETE).group(originalMembs.get(key).getGroupKey()).build());
});
}
return result;
}
use of org.apache.syncope.common.lib.to.RelationshipTO in project syncope by apache.
the class AnyOperations method diff.
/**
* Calculate modifications needed by first in order to be equal to second.
*
* @param updated updated UserTO
* @param original original UserTO
* @param incremental perform incremental diff (without removing existing info)
* @return UserPatch containing differences
*/
public static UserPatch diff(final UserTO updated, final UserTO original, final boolean incremental) {
UserPatch result = new UserPatch();
diff(updated, original, result, incremental);
// 1. password
if (updated.getPassword() != null && (original.getPassword() == null || !original.getPassword().equals(updated.getPassword()))) {
result.setPassword(new PasswordPatch.Builder().value(updated.getPassword()).resources(updated.getResources()).build());
}
// 2. username
result.setUsername(replacePatchItem(updated.getUsername(), original.getUsername(), new StringReplacePatchItem()));
// 3. security question / answer
if (updated.getSecurityQuestion() == null) {
result.setSecurityQuestion(null);
result.setSecurityAnswer(null);
} else if (!updated.getSecurityQuestion().equals(original.getSecurityQuestion()) || StringUtils.isNotBlank(updated.getSecurityAnswer())) {
result.setSecurityQuestion(new StringReplacePatchItem.Builder().value(updated.getSecurityQuestion()).build());
result.setSecurityAnswer(new StringReplacePatchItem.Builder().value(updated.getSecurityAnswer()).build());
}
result.setMustChangePassword(replacePatchItem(updated.isMustChangePassword(), original.isMustChangePassword(), new BooleanReplacePatchItem()));
// 4. roles
if (!incremental) {
original.getRoles().stream().filter(role -> !updated.getRoles().contains(role)).forEach(toRemove -> {
result.getRoles().add(new StringPatchItem.Builder().operation(PatchOperation.DELETE).value(toRemove).build());
});
}
updated.getRoles().stream().filter(role -> !original.getRoles().contains(role)).forEach(toAdd -> {
result.getRoles().add(new StringPatchItem.Builder().operation(PatchOperation.ADD_REPLACE).value(toAdd).build());
});
// 5. relationships
Map<Pair<String, String>, RelationshipTO> updatedRels = EntityTOUtils.buildRelationshipMap(updated.getRelationships());
Map<Pair<String, String>, RelationshipTO> originalRels = EntityTOUtils.buildRelationshipMap(original.getRelationships());
updatedRels.entrySet().stream().filter(entry -> (!originalRels.containsKey(entry.getKey()))).forEachOrdered(entry -> {
result.getRelationships().add(new RelationshipPatch.Builder().operation(PatchOperation.ADD_REPLACE).relationshipTO(entry.getValue()).build());
});
if (!incremental) {
originalRels.keySet().stream().filter(relationship -> !updatedRels.containsKey(relationship)).forEach(key -> {
result.getRelationships().add(new RelationshipPatch.Builder().operation(PatchOperation.DELETE).relationshipTO(originalRels.get(key)).build());
});
}
// 6. memberships
Map<String, MembershipTO> updatedMembs = EntityTOUtils.buildMembershipMap(updated.getMemberships());
Map<String, MembershipTO> originalMembs = EntityTOUtils.buildMembershipMap(original.getMemberships());
updatedMembs.entrySet().stream().map(entry -> {
MembershipPatch membershipPatch = new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(entry.getValue().getGroupKey()).build();
MembershipTO omemb;
if (originalMembs.containsKey(entry.getKey())) {
// get the original membership
omemb = originalMembs.get(entry.getKey());
} else {
// create an empty one to generate the patch
omemb = new MembershipTO.Builder().group(entry.getKey()).build();
}
diff(entry.getValue(), omemb, membershipPatch, incremental);
return membershipPatch;
}).forEachOrdered(membershipPatch -> {
result.getMemberships().add(membershipPatch);
});
if (!incremental) {
originalMembs.keySet().stream().filter(membership -> !updatedMembs.containsKey(membership)).forEach(key -> {
result.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.DELETE).group(originalMembs.get(key).getGroupKey()).build());
});
}
return result;
}
use of org.apache.syncope.common.lib.to.RelationshipTO in project syncope by apache.
the class Relationships method removeRelationships.
private void removeRelationships(final Map<String, List<RelationshipTO>> relationships, final RelationshipTO... rels) {
final List<RelationshipTO> currentRels = getCurrentRelationships();
for (RelationshipTO relationship : rels) {
currentRels.remove(relationship);
if (relationships.containsKey(relationship.getType())) {
final List<RelationshipTO> rellist = relationships.get(relationship.getType());
rellist.remove(relationship);
if (rellist.isEmpty()) {
relationships.remove(relationship.getType());
}
}
}
}
use of org.apache.syncope.common.lib.to.RelationshipTO in project syncope by apache.
the class UserReportlet method doExtract.
private void doExtract(final ContentHandler handler, final List<User> users) throws SAXException {
AttributesImpl atts = new AttributesImpl();
for (User user : users) {
atts.clear();
for (Feature feature : conf.getFeatures()) {
String type = null;
String value = null;
switch(feature) {
case key:
type = ReportXMLConst.XSD_STRING;
value = user.getKey();
break;
case username:
type = ReportXMLConst.XSD_STRING;
value = user.getUsername();
break;
case workflowId:
type = ReportXMLConst.XSD_STRING;
value = user.getWorkflowId();
break;
case status:
type = ReportXMLConst.XSD_STRING;
value = user.getStatus();
break;
case creationDate:
type = ReportXMLConst.XSD_DATETIME;
value = user.getCreationDate() == null ? "" : FormatUtils.format(user.getCreationDate());
break;
case lastLoginDate:
type = ReportXMLConst.XSD_DATETIME;
value = user.getLastLoginDate() == null ? "" : FormatUtils.format(user.getLastLoginDate());
break;
case changePwdDate:
type = ReportXMLConst.XSD_DATETIME;
value = user.getChangePwdDate() == null ? "" : FormatUtils.format(user.getChangePwdDate());
break;
case passwordHistorySize:
type = ReportXMLConst.XSD_INT;
value = String.valueOf(user.getPasswordHistory().size());
break;
case failedLoginCount:
type = ReportXMLConst.XSD_INT;
value = String.valueOf(user.getFailedLogins());
break;
default:
}
if (type != null && value != null) {
atts.addAttribute("", "", feature.name(), type, value);
}
}
handler.startElement("", "", "user", atts);
// Using UserTO for attribute values, since the conversion logic of
// values to String is already encapsulated there
UserTO userTO = userDataBinder.getUserTO(user, true);
doExtractAttributes(handler, userTO, conf.getPlainAttrs(), conf.getDerAttrs(), conf.getVirAttrs());
if (conf.getFeatures().contains(Feature.relationships)) {
handler.startElement("", "", "relationships", null);
for (RelationshipTO rel : userTO.getRelationships()) {
atts.clear();
atts.addAttribute("", "", "anyObjectKey", ReportXMLConst.XSD_STRING, rel.getOtherEndKey());
handler.startElement("", "", "relationship", atts);
if (conf.getFeatures().contains(Feature.resources)) {
for (URelationship actualRel : user.getRelationships(rel.getOtherEndKey())) {
doExtractResources(handler, anyObjectDataBinder.getAnyObjectTO(actualRel.getRightEnd(), true));
}
}
handler.endElement("", "", "relationship");
}
handler.endElement("", "", "relationships");
}
if (conf.getFeatures().contains(Feature.memberships)) {
handler.startElement("", "", "memberships", null);
for (MembershipTO memb : userTO.getMemberships()) {
atts.clear();
atts.addAttribute("", "", "groupKey", ReportXMLConst.XSD_STRING, memb.getGroupKey());
atts.addAttribute("", "", "groupName", ReportXMLConst.XSD_STRING, memb.getGroupName());
handler.startElement("", "", "membership", atts);
if (conf.getFeatures().contains(Feature.resources)) {
UMembership actualMemb = user.getMembership(memb.getGroupKey()).orElse(null);
if (actualMemb == null) {
LOG.warn("Unexpected: cannot find membership for group {} for user {}", memb.getGroupKey(), user);
} else {
doExtractResources(handler, groupDataBinder.getGroupTO(actualMemb.getRightEnd(), true));
}
}
handler.endElement("", "", "membership");
}
handler.endElement("", "", "memberships");
}
if (conf.getFeatures().contains(Feature.resources)) {
doExtractResources(handler, userTO);
}
handler.endElement("", "", "user");
}
}
use of org.apache.syncope.common.lib.to.RelationshipTO in project syncope by apache.
the class Relationships method getViewFragment.
private Fragment getViewFragment() {
final Map<String, List<RelationshipTO>> relationships = new HashMap<>();
addRelationship(relationships, getCurrentRelationships().toArray(new RelationshipTO[] {}));
final Fragment viewFragment = new Fragment("relationships", "viewFragment", this);
viewFragment.setOutputMarkupId(true);
viewFragment.add(new Accordion("relationships", relationships.keySet().stream().map(relationship -> {
return new AbstractTab(new ResourceModel("relationship", relationship)) {
private static final long serialVersionUID = 1037272333056449378L;
@Override
public Panel getPanel(final String panelId) {
return new ListViewPanel.Builder<>(RelationshipTO.class, pageRef).setItems(relationships.get(relationship)).includes("otherEndType", "otherEndKey").addAction(new ActionLink<RelationshipTO>() {
private static final long serialVersionUID = -6847033126124401556L;
@Override
public void onClick(final AjaxRequestTarget target, final RelationshipTO modelObject) {
removeRelationships(relationships, modelObject);
send(Relationships.this, Broadcast.DEPTH, new ListViewReload<>(target));
}
}, ActionType.DELETE, AnyEntitlement.UPDATE.getFor(anyTO.getType()), true).build(panelId);
}
};
}).collect(Collectors.toList())) {
private static final long serialVersionUID = 1037272333056449379L;
@Override
public void renderHead(final IHeaderResponse response) {
super.renderHead(response);
if (relationships.isEmpty()) {
response.render(OnDomReadyHeaderItem.forScript(String.format("$('#emptyPlaceholder').append(\"%s\")", getString("relationships.empty.list"))));
}
}
});
final ActionsPanel<RelationshipTO> panel = new ActionsPanel<>("actions", null);
viewFragment.add(panel);
panel.add(new ActionLink<RelationshipTO>() {
private static final long serialVersionUID = 3257738274365467945L;
@Override
public void onClick(final AjaxRequestTarget target, final RelationshipTO ignore) {
Fragment addFragment = new Fragment("relationships", "addFragment", Relationships.this);
addOrReplace(addFragment);
addFragment.add(new Specification().setRenderBodyOnly(true));
target.add(Relationships.this);
}
}, ActionType.CREATE, AnyEntitlement.UPDATE.getFor(anyTO.getType())).hideLabel();
return viewFragment;
}
Aggregations