Manager secMgr = ManagerFactory.getManager();
// loop over users to be edited:
for (Identity identity : selIdentities) {
DB db = DBFactory.getInstance();
//reload identity from cache, to prevent stale object
identity = (Identity) db.loadObject(identity);
User user = identity.getUser();
String errorDesc = "";
boolean updateError = false;
// change pwd
if (attributeChangeMap.containsKey(PWD_IDENTIFYER)) {
String newPwd = attributeChangeMap.get(PWD_IDENTIFYER);
if (StringHelper.containsNonWhitespace(newPwd)) {
if (!UserManager.getInstance().syntaxCheckOlatPassword(newPwd)) {
errorDesc = transWithFallback.translate("error.password");
updateError = true;
}
} else newPwd = null;
OLATAuthManager.changePasswordAsAdmin(identity, newPwd);
}
// set language
String userLanguage = user.getPreferences().getLanguage();
if (attributeChangeMap.containsKey(LANG_IDENTIFYER)) {
String inputLanguage = attributeChangeMap.get(LANG_IDENTIFYER);
if (!userLanguage.equals(inputLanguage)) {
Preferences preferences = user.getPreferences();
preferences.setLanguage(inputLanguage);
user.setPreferences(preferences);
}
}
Context vcContext = new VelocityContext();
// set all properties as context
setUserContext(identity, vcContext, isAdministrativeUser);
// loop for each property configured in
// src/serviceconfig/org/olat/_spring/olat_userconfig.xml -> Key:
// org.olat.admin.user.bulkChange.UserBulkChangeStep00
for (int k = 0; k < userPropertyHandlers.size(); k++) {
UserPropertyHandler propHandler = userPropertyHandlers.get(k);
String propertyName = propHandler.getName();
String userValue = identity.getUser().getProperty(propertyName, null);
String inputFieldValue = "";
if (attributeChangeMap.containsKey(propertyName)) {
inputFieldValue = attributeChangeMap.get(propertyName);
inputFieldValue = inputFieldValue.replace("$", "$!");
String evaluatedInputFieldValue = evaluateValueWithUserContext(inputFieldValue, vcContext);
// validate evaluated property-value
ValidationError validationError = new ValidationError();
// do validation checks with users current locale!
Locale locale = transWithFallback.getLocale();
if (!propHandler.isValidValue(evaluatedInputFieldValue, validationError, locale)) {
errorDesc = transWithFallback.translate(validationError.getErrorKey()) + " (" + evaluatedInputFieldValue + ")";
updateError = true;
break;
}
if (!evaluatedInputFieldValue.equals(userValue)) {
String stringValue = propHandler.getStringValue(evaluatedInputFieldValue, locale);
propHandler.setUserProperty(user, stringValue);
}
}
} // for (propertyHandlers)
// set roles for identity
// loop over securityGroups defined above
for (String securityGroup : securityGroups) {
SecurityGroup secGroup = secMgr.findSecurityGroupByName(securityGroup);
Boolean isInGroup = secMgr.isIdentityInSecurityGroup(identity, secGroup);
String thisRoleAction = "";
if (roleChangeMap.containsKey(securityGroup)) {
thisRoleAction = roleChangeMap.get(securityGroup);
// user not anymore in security group, remove him
if (isInGroup && thisRoleAction.equals("remove")) secMgr.removeIdentityFromSecurityGroup(identity, secGroup);
// user not yet in security group, add him
if (!isInGroup && thisRoleAction.equals("add")) secMgr.addIdentityToSecurityGroup(identity, secGroup);
}
}
// set status
if (roleChangeMap.containsKey("Status")) {
Integer status = Integer.parseInt(roleChangeMap.get("Status"));
secMgr.saveIdentityStatus(identity, status);
identity = (Identity) db.loadObject(identity);
}
// persist changes:
if (updateError) {
String errorOutput = identity.getName() + ": " + errorDesc;
log.debug("error during bulkChange of users, following user could not be updated: " + errorOutput);
notUpdatedIdentities.add(errorOutput);
} else {
um.updateUserFromIdentity(identity);
changedIdentities.add(identity);
log.audit("user successfully changed during bulk-change: " + identity.getName());
}
// commit changes for this user
db.intermediateCommit();
}
}