try {
collection = new LocalCollection(context.getSubject(), context.getBroker().getBrokerPool(), XmldbURI.ROOT_COLLECTION_URI, context.getAccessContext());
final UserManagementService ums = (UserManagementService) collection.getService("UserManagementService", "1.0");
final Account oldUser = ums.getAccount(userName);
if(oldUser == null) {
logger.error("User " + userName + " not found");
throw new XPathException(this, "User " + userName + " not found");
}
final Group oldPrimaryGroup = oldUser.getDefaultGroup();
final UserAider user;
if(oldPrimaryGroup != null) {
//dont forget to set the primary group
user = new UserAider(oldUser.getName(), oldPrimaryGroup);
} else {
user = new UserAider(oldUser.getName());
}
//copy the umask
user.setUserMask(oldUser.getUserMask());
//copy the metadata
for(final SchemaType key : oldUser.getMetadataKeys()) {
user.setMetadataValue(key, oldUser.getMetadataValue(key));
}
//copy the status
user.setEnabled(oldUser.isEnabled());
//change the password?
if(!args[1].isEmpty()) {
// set password
user.setPassword(args[1].getStringValue());
} else {
//use the old password
user.setEncodedPassword(oldUser.getPassword());
user.setPasswordDigest(oldUser.getDigestPassword());
}
//change the groups?
if(!args[2].isEmpty()) {
// set groups
for(final SequenceIterator i = args[2].iterate(); i.hasNext(); ) {
user.addGroup(i.nextItem().getStringValue());
}
} else {
user.setGroups(oldUser.getGroups());
}
ums.updateAccount(user);
} catch(final XMLDBException xe) {
logger.error("Failed to update user " + userName, xe);