in reference/src/main/java/org/apache/sling/cms/reference/forms/impl/actions/UpdateProfileAction.java [53:103]
public FormActionResult handleForm(Resource actionResource, FormRequest request) throws FormException {
ResourceResolver resolver = request.getOriginalRequest().getResourceResolver();
String userId = resolver.getUserID();
try {
JackrabbitSession session = Optional.ofNullable((JackrabbitSession) resolver.adaptTo(Session.class))
.orElseThrow(() -> new RepositoryException("Unable to get Jackrabbit Session"));
final UserManager userManager = session.getUserManager();
if (userManager.getAuthorizable(userId) == null) {
log.warn("No profile found for {}", userId);
return FormActionResult.failure("No profile found for " + userId);
}
User user = (User) userManager.getAuthorizable(userId);
log.debug("Updating profile for {}", userId);
String subpath = actionResource.getValueMap().get(FormConstants.PN_SUBPATH, FormConstants.PATH_PROFILE);
ValueFactory valueFactory = session.getValueFactory();
for (Entry<String, Object> e : request.getFormData().entrySet()) {
Value value = null;
if (e.getValue() instanceof String[]) {
Value[] values = Arrays.stream(((String[]) e.getValue())).map(valueFactory::createValue)
.collect(Collectors.toList()).toArray(new Value[0]);
user.setProperty(subpath + "/" + e.getKey(), values);
} else {
if (e.getValue() instanceof Calendar) {
value = valueFactory.createValue((Calendar) e.getValue());
} else if (e.getValue() instanceof Double) {
value = valueFactory.createValue((Double) e.getValue());
} else if (e.getValue() instanceof Integer) {
value = valueFactory.createValue((Integer) e.getValue());
} else {
value = valueFactory.createValue((String) e.getValue());
}
user.setProperty(subpath + "/" + e.getKey(), value);
}
}
log.debug("Saving changes!");
resolver.commit();
return FormActionResult.success("Profile Updated");
} catch (RepositoryException | PersistenceException e) {
log.warn("Failed to update profile for {}", userId, e);
return FormActionResult.failure("Failed to update profile for " + userId);
}
}