/** * Creates a new user if does not exist. If it exists, it is unlocked and roles are reset (password is untouched). * * @param cnx * @param login * @param password * the raw password. it will be hashed. * @param description * @param roles */ static void createUserIfMissing(DbConn cnx, String login, String password, String description, String... roles) { try { int userId = cnx.runSelectSingle("user_select_id_by_key", Integer.class, login); cnx.runUpdate("user_update_enable_by_id", userId); RUser.set_roles(cnx, userId, roles); } catch (NoResultException e) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); String hash = new Sha512Hash(password, salt, 100000).toHex(); String saltS = salt.toHex(); RUser.create(cnx, login, hash, saltS, roles); } }
/** * Creates a new user if does not exist. If it exists, it is unlocked and roles are reset (password is untouched). * * @param cnx * @param login * @param password * the raw password. it will be hashed. * @param description * @param roles */ static void createUserIfMissing(DbConn cnx, String login, String password, String description, String... roles) { try { int userId = cnx.runSelectSingle("user_select_id_by_key", Integer.class, login); cnx.runUpdate("user_update_enable_by_id", userId); RUser.set_roles(cnx, userId, roles); } catch (NoResultException e) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); String hash = new Sha512Hash(password, salt, 100000).toHex(); String saltS = salt.toHex(); RUser.create(cnx, login, hash, saltS, roles); } }