use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.
the class UserSelfUpdateResource method newResourceResponse.
@Override
protected ResourceResponse newResourceResponse(final IResource.Attributes attributes) {
ResourceResponse response = new AbstractResource.ResourceResponse();
response.setContentType(MediaType.TEXT_PLAIN);
try {
HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
if (!xsrfCheck(request)) {
LOG.error("XSRF TOKEN does not match");
response.setError(Response.Status.BAD_REQUEST.getStatusCode(), "XSRF TOKEN does not match");
return response;
}
if (!captchaCheck(request.getHeader("captcha"), request.getSession().getAttribute(SyncopeEnduserConstants.CAPTCHA_SESSION_KEY))) {
throw new IllegalArgumentException("Entered captcha is not matching");
}
UserTO userTO = MAPPER.readValue(request.getReader().readLine(), UserTO.class);
Map<String, CustomAttributesInfo> customForm = SyncopeEnduserApplication.get().getCustomForm();
// check if request is compliant with customization form rules
if (UserRequestValidator.compliant(userTO, customForm, false)) {
// 1. membership attributes management
Set<AttrTO> membAttrs = new HashSet<>();
userTO.getPlainAttrs().stream().filter(attr -> (attr.getSchema().contains(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR))).forEachOrdered((attr) -> {
String[] simpleAttrs = attr.getSchema().split(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR);
MembershipTO membership = userTO.getMemberships().stream().filter(item -> simpleAttrs[0].equals(item.getGroupName())).findFirst().orElse(null);
if (membership == null) {
membership = new MembershipTO.Builder().group(null, simpleAttrs[0]).build();
userTO.getMemberships().add(membership);
}
AttrTO clone = SerializationUtils.clone(attr);
clone.setSchema(simpleAttrs[1]);
membership.getPlainAttrs().add(clone);
membAttrs.add(attr);
});
userTO.getPlainAttrs().removeAll(membAttrs);
// 2. millis -> Date conversion for PLAIN attributes of USER and its MEMBERSHIPS
SyncopeEnduserSession.get().getDatePlainSchemas().stream().map(plainSchema -> {
millisToDate(userTO.getPlainAttrs(), plainSchema);
return plainSchema;
}).forEachOrdered(plainSchema -> {
userTO.getMemberships().forEach(membership -> {
millisToDate(membership.getPlainAttrs(), plainSchema);
});
});
membAttrs.clear();
userTO.getDerAttrs().stream().filter(attr -> (attr.getSchema().contains(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR))).forEachOrdered(attr -> {
String[] simpleAttrs = attr.getSchema().split(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR);
MembershipTO membership = userTO.getMemberships().stream().filter(item -> simpleAttrs[0].equals(item.getGroupName())).findFirst().orElse(null);
if (membership == null) {
membership = new MembershipTO.Builder().group(null, simpleAttrs[0]).build();
userTO.getMemberships().add(membership);
}
AttrTO clone = SerializationUtils.clone(attr);
clone.setSchema(simpleAttrs[1]);
membership.getDerAttrs().add(clone);
membAttrs.add(attr);
});
userTO.getDerAttrs().removeAll(membAttrs);
membAttrs.clear();
userTO.getVirAttrs().stream().filter(attr -> (attr.getSchema().contains(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR))).forEachOrdered((attr) -> {
String[] simpleAttrs = attr.getSchema().split(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR);
MembershipTO membership = userTO.getMemberships().stream().filter(item -> simpleAttrs[0].equals(item.getGroupName())).findFirst().orElse(null);
if (membership == null) {
membership = new MembershipTO.Builder().group(null, simpleAttrs[0]).build();
userTO.getMemberships().add(membership);
}
AttrTO clone = SerializationUtils.clone(attr);
clone.setSchema(simpleAttrs[1]);
membership.getVirAttrs().add(clone);
membAttrs.add(attr);
});
userTO.getVirAttrs().removeAll(membAttrs);
// get old user object from session
UserTO selfTO = SyncopeEnduserSession.get().getSelfTO();
// align "userTO" and "selfTO" objects
if (customForm != null && !customForm.isEmpty()) {
completeUserObject(userTO, selfTO);
}
// create diff patch
UserPatch userPatch = AnyOperations.diff(userTO, selfTO, false);
// update user by patch
Response res = SyncopeEnduserSession.get().getService(userTO.getETagValue(), UserSelfService.class).update(userPatch);
buildResponse(response, res.getStatus(), res.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL) ? "User [" + userTO.getUsername() + "] successfully updated" : "ErrorMessage{{ " + res.getStatusInfo().getReasonPhrase() + " }}");
} else {
LOG.warn("Incoming update request [{}] is not compliant with form customization rules." + " Update NOT allowed", userTO.getUsername());
buildResponse(response, Response.Status.OK.getStatusCode(), "User: " + userTO.getUsername() + " successfully created");
}
} catch (final Exception e) {
LOG.error("Error while updating user", e);
response.setError(Response.Status.BAD_REQUEST.getStatusCode(), new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
}
return response;
}
use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.
the class AnyOperations method patch.
private static Collection<AttrTO> patch(final Map<String, AttrTO> attrs, final Set<AttrPatch> attrPatches) {
Map<String, AttrTO> rwattrs = new HashMap<>(attrs);
attrPatches.forEach(patch -> {
if (patch.getAttrTO() == null) {
LOG.warn("Invalid {} specified: {}", AttrPatch.class.getName(), patch);
} else {
AttrTO removed = rwattrs.remove(patch.getAttrTO().getSchema());
if (removed != null && removed.getSchemaInfo() != null) {
patch.getAttrTO().setSchemaInfo(removed.getSchemaInfo());
}
if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
rwattrs.put(patch.getAttrTO().getSchema(), patch.getAttrTO());
}
}
});
return rwattrs.values();
}
use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.
the class VirAttrs method setAttrs.
@Override
protected void setAttrs(final MembershipTO membershipTO) {
List<AttrTO> attrs = new ArrayList<>();
Map<String, AttrTO> attrMap = EntityTOUtils.buildAttrMap(anyTO.getVirAttrs());
membershipSchemas.get(membershipTO.getGroupKey()).values().stream().map(schema -> {
AttrTO attrTO = new AttrTO();
attrTO.setSchema(schema.getKey());
if (attrMap.containsKey(schema.getKey())) {
attrTO.getValues().addAll(attrMap.get(schema.getKey()).getValues());
} else {
attrTO.getValues().add(StringUtils.EMPTY);
}
return attrTO;
}).forEachOrdered(attrTO -> {
attrs.add(attrTO);
});
membershipTO.getVirAttrs().clear();
membershipTO.getVirAttrs().addAll(attrs);
}
use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.
the class VirAttrs method setAttrs.
@Override
protected void setAttrs() {
List<AttrTO> attrs = new ArrayList<>();
Map<String, AttrTO> attrMap = EntityTOUtils.buildAttrMap(anyTO.getVirAttrs());
schemas.values().stream().map(schema -> {
AttrTO attrTO = new AttrTO();
attrTO.setSchema(schema.getKey());
if (attrMap.containsKey(schema.getKey())) {
attrTO.getValues().addAll(attrMap.get(schema.getKey()).getValues());
} else {
attrTO.getValues().add(StringUtils.EMPTY);
}
return attrTO;
}).forEachOrdered(attrTO -> {
attrs.add(attrTO);
});
anyTO.getVirAttrs().clear();
anyTO.getVirAttrs().addAll(attrs);
}
use of org.apache.syncope.common.lib.to.AttrTO in project syncope by apache.
the class PlainAttrs method setAttrs.
@Override
protected void setAttrs() {
List<AttrTO> attrs = new ArrayList<>();
Map<String, AttrTO> attrMap = EntityTOUtils.buildAttrMap(anyTO.getPlainAttrs());
schemas.values().stream().map(schema -> {
AttrTO attrTO = new AttrTO();
attrTO.setSchema(schema.getKey());
if (attrMap.get(schema.getKey()) == null || attrMap.get(schema.getKey()).getValues().isEmpty()) {
attrTO.getValues().add("");
// is important to set the schema info only after values setting
attrTO.setSchemaInfo(schema);
} else {
attrTO = attrMap.get(schema.getKey());
}
return attrTO;
}).forEachOrdered(attrTO -> {
attrs.add(attrTO);
});
anyTO.getPlainAttrs().clear();
anyTO.getPlainAttrs().addAll(attrs);
}
Aggregations