public static void create(DbConn cnx, String login, String password_hash, String password_salt, String... role_names) { create(cnx, login, password_hash, password_salt, null, false, role_names); }
public List<RUser> getUsers(DbConn cnx) { return RUser.select(cnx, "user_select_all_in_role", this.id); }
if (currentUser != null) renewalLimit = (Calendar) currentUser.getExpirationDate().clone(); // clone. otherwise we modify the user object next line! renewalLimit.add(Calendar.DAY_OF_YEAR, -1); if (currentUser == null || currentUser.getExpirationDate().before(Calendar.getInstance())) expiration.add(Calendar.DAY_OF_YEAR, 2); int id = RUser.create(cnx, login, hash, saltS, expiration, true, "administrator"); currentUser = RUser.select_id(cnx, id); user = currentUser;
/** * 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); } }
@Test public void testCliChangeUser() { Helpers.updateConfiguration(cnx); Main.runCommand(new String[] { "Reset-User", "--login", "myuser", "-p", "mypassword", "--roles", "administrator", "client" }); RUser u = RUser.selectlogin(cnx, "myuser"); Assert.assertEquals(2, u.getRoles(cnx).size()); boolean admin = false, client = false; for (RRole r : u.getRoles(cnx)) { if (r.getName().equals("administrator")) { admin = true; } if (r.getName().equals("client")) { client = true; } } Assert.assertTrue(client && admin); Main.runCommand(new String[] { "Reset-User", "--login", "myuser", "--password", "mypassword", "--roles", "administrator" }); Assert.assertEquals(1, u.getRoles(cnx).size()); Main.runCommand(new String[] { "Reset-User", "--login", "myuser", "-p", "mypassword", "--roles", "administrator", "config admin" }); Assert.assertEquals(2, u.getRoles(cnx).size()); }
public static List<RUser> select(DbConn cnx, String query_key, Object... args) { List<RUser> res = new ArrayList<RUser>(); try { ResultSet rs = cnx.runSelect(query_key, args); while (rs.next()) { RUser tmp = new RUser(); tmp.id = rs.getInt(1); tmp.login = rs.getString(2); tmp.password = rs.getString(3); tmp.hashSalt = rs.getString(4); tmp.locked = rs.getBoolean(5); tmp.expirationDate = cnx.getCal(rs, 6); tmp.creationDate = cnx.getCal(rs, 7); tmp.lastModified = cnx.getCal(rs, 8); tmp.email = rs.getString(9); tmp.freeText = rs.getString(10); tmp.internal = rs.getBoolean(11); res.add(tmp); } } catch (SQLException e) { throw new DatabaseException(e); } return res; }
/** * 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); } }
public static List<RUser> select(DbConn cnx, String query_key, Object... args) { List<RUser> res = new ArrayList<>(); try { ResultSet rs = cnx.runSelect(query_key, args); while (rs.next()) { RUser tmp = new RUser(); tmp.id = rs.getInt(1); tmp.login = rs.getString(2); tmp.password = rs.getString(3); tmp.hashSalt = rs.getString(4); tmp.locked = rs.getBoolean(5); tmp.expirationDate = cnx.getCal(rs, 6); tmp.creationDate = cnx.getCal(rs, 7); tmp.lastModified = cnx.getCal(rs, 8); tmp.email = rs.getString(9); tmp.freeText = rs.getString(10); tmp.internal = rs.getBoolean(11); res.add(tmp); } } catch (SQLException e) { throw new DatabaseException(e); } return res; }
public static void create(DbConn cnx, String login, String password_hash, String password_salt, String... role_names) { create(cnx, login, password_hash, password_salt, null, false, role_names); }
public List<RUser> getUsers(DbConn cnx) { return RUser.select(cnx, "user_select_all_in_role", this.id); }
public static void createUser(DbConn cnx, String login, String password, RRole... roles) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); String[] rr = new String[roles.length]; for (int i = 0; i < roles.length; i++) { rr[i] = roles[i].getName(); } RUser.create(cnx, login, new Sha512Hash(password, salt, 100000).toHex(), salt.toHex(), rr); } }
public static RUser select_id(DbConn cnx, Integer id) { List<RUser> res = select(cnx, "user_select_by_id", id); if (res.isEmpty()) { throw new DatabaseException("no result for query by key for key " + id); } if (res.size() > 1) { throw new DatabaseException("Inconsistent database! Multiple results for query by key for key " + id); } return res.get(0); }
public static void createUser(DbConn cnx, String login, String password, RRole... roles) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); String[] rr = new String[roles.length]; for (int i = 0; i < roles.length; i++) { rr[i] = roles[i].getName(); } RUser.create(cnx, login, new Sha512Hash(password, salt, 100000).toHex(), salt.toHex(), rr); } }
public static RUser select_id(DbConn cnx, Integer id) { List<RUser> res = select(cnx, "user_select_by_id", id); if (res.isEmpty()) { throw new DatabaseException("no result for query by key for key " + id); } if (res.size() > 1) { throw new DatabaseException("Inconsistent database! Multiple results for query by key for key " + id); } return res.get(0); }
public static RUser selectlogin(DbConn cnx, String id) { List<RUser> res = select(cnx, "user_select_by_key", id); if (res.isEmpty()) { throw new NoResultException("no result for query by key for key " + id); } if (res.size() > 1) { throw new NonUniqueResultException("Inconsistent database! Multiple results for query by key for key " + id); } return res.get(0); } }
public static RUser selectlogin(DbConn cnx, String id) { List<RUser> res = select(cnx, "user_select_by_key", id); if (res.isEmpty()) { throw new NoResultException("no result for query by key for key " + id); } if (res.size() > 1) { throw new NonUniqueResultException("Inconsistent database! Multiple results for query by key for key " + id); } return res.get(0); } }