@Override
protected void doAddUser(String username, String password) throws UsersRepositoryException {
String lowerCasedUsername = username.toLowerCase();
if (contains(lowerCasedUsername)) {
throw new UsersRepositoryException(lowerCasedUsername + " already exists.");
}
try {
final Session session = login();
try {
final String name = toSafeName(lowerCasedUsername);
final String path = USERS_PATH + "/" + name;
final Node rootNode = session.getRootNode();
try {
rootNode.getNode(path);
getLogger().info("User already exists");
throw new UsersRepositoryException("User " + lowerCasedUsername + " already exists");
} catch (PathNotFoundException e) {
// user does not exist
}
Node parent;
try {
parent = rootNode.getNode(USERS_PATH);
} catch (PathNotFoundException e) {
// TODO: Need to consider whether should insist that parent
// TODO: path exists.
parent = rootNode.addNode(USERS_PATH);
}
Node node = parent.addNode(name);
node.setProperty(USERNAME_PROPERTY, lowerCasedUsername);
final String hashedPassword;
if (password == null) {
// Support easy password reset
hashedPassword = "";
} else {
hashedPassword = JCRUser.hashPassword(lowerCasedUsername, password);
}
node.setProperty(PASSWD_PROPERTY, hashedPassword);
session.save();
} finally {
session.logout();
}
} catch (RepositoryException e) {
if (getLogger().isInfoEnabled()) {
getLogger().info("Failed to add user: " + lowerCasedUsername, e);
}
throw new UsersRepositoryException("Failed to add user: " + lowerCasedUsername, e);
}
}