*/
private void persistUser(String userName, Object credential, String[] roleList,
Map<String, String> claims, String profileName,
boolean requirePasswordChange) throws UserStoreException {
if (!checkUserNameValid(userName)) {
throw new UserStoreException(
"User name not valid. User name must be a non null string with following format, " +
realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG_EX));
}
if (!checkUserPasswordValid(credential)) {
throw new UserStoreException(
"Credential not valid. Credential must be a non null string with following format, " +
realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_JAVA_REG_EX));
}
boolean isExisting = checkExistingUserName(userName);
if (isExisting) {
throw new UserStoreException("User name : " + userName
+ " exists in the system. Please pick another user name");
}
Connection dbConnection = null;
String password = (String) credential;
try {
dbConnection = getDBConnection();
String sqlStmt1 = realmConfig.getUserStoreProperty(JDBCRealmConstants.ADD_USER);
String saltValue = null;
if ("true".equals(realmConfig.getUserStoreProperties().get(
JDBCRealmConstants.STORE_SALTED_PASSWORDS))) {
byte[] bytes = new byte[16];
random.nextBytes(bytes);
saltValue = Base64.encode(bytes);
}
password = this.preparePassword(password, saltValue);
// do all 4 possibilities
if (sqlStmt1.contains(UserCoreConstants.UM_TENANT_COLUMN) && (saltValue == null)) {
this.updateStringValuesToDatabase(dbConnection, sqlStmt1, userName, password, "",
requirePasswordChange, new Date(), tenantId);
} else if (sqlStmt1.contains(UserCoreConstants.UM_TENANT_COLUMN) && (saltValue != null)) {
this.updateStringValuesToDatabase(dbConnection, sqlStmt1, userName, password,
saltValue, requirePasswordChange, new Date(), tenantId);
} else if (!sqlStmt1.contains(UserCoreConstants.UM_TENANT_COLUMN)
&& (saltValue == null)) {
this.updateStringValuesToDatabase(dbConnection, sqlStmt1, userName, password,
null, requirePasswordChange, new Date());
} else {
this.updateStringValuesToDatabase(dbConnection, sqlStmt1, userName, password,
requirePasswordChange, new Date());
}
String[] roles = null;
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equals(userName)) {
roles = new String[0];
} else {
if (roleList == null || roleList.length == 0) {
roles = new String[] { this.realmConfig.getEveryOneRoleName() };
} else {
Arrays.sort(roleList);
if (Arrays.binarySearch(roleList, realmConfig.getEveryOneRoleName()) < 0) {
roles = new String[roleList.length + 1];
int i = 0;
for (i = 0; i < roleList.length; i++) {
roles[i] = roleList[i];
}
roles[i] = realmConfig.getEveryOneRoleName();
} else {
roles = roleList;
}
}
}
// add user to role.
String sqlStmt2 = null;
String type = DatabaseCreator.getDatabaseType(dbConnection);
sqlStmt2 = realmConfig.getUserStoreProperty(JDBCRealmConstants.ADD_ROLE_TO_USER
+ "-" + type);
if (sqlStmt2 == null) {
sqlStmt2 = realmConfig
.getUserStoreProperty(JDBCRealmConstants.ADD_ROLE_TO_USER);
}
if (sqlStmt2.contains(UserCoreConstants.UM_TENANT_COLUMN)) {
DatabaseUtil.udpateUserRoleMappingInBatchMode(dbConnection, sqlStmt2, roles,
tenantId, userName, tenantId, tenantId);
} else {
DatabaseUtil.udpateUserRoleMappingInBatchMode(dbConnection, sqlStmt2, roles,
tenantId, userName);
}
if (claims != null) {
// add the properties
if (profileName == null) {
profileName = UserCoreConstants.DEFAULT_PROFILE;
}
Iterator<Map.Entry<String, String>> ite = claims.entrySet().iterator();
while (ite.hasNext()) {
Map.Entry<String, String> entry = ite.next();
String claimURI = entry.getKey();
String propName = claimManager.getAttributeName(claimURI);
String propValue = entry.getValue();
addProperty(dbConnection, userName, propName, propValue, profileName);
}
}
dbConnection.commit();
} catch (Throwable e) {
try {
dbConnection.rollback();
} catch (SQLException e1) {
log.error(e.getMessage(), e1);
throw new UserStoreException(e.getMessage(), e1);
}
log.error(e.getMessage(), e);
throw new UserStoreException(e.getMessage(), e);
} finally {
DatabaseUtil.closeAllConnections(dbConnection);
}
}