diff options
author | Alexander Alexeev | 2016-11-14 05:12:02 +0700 |
---|---|---|
committer | Alexander Alexeev | 2016-11-14 05:12:02 +0700 |
commit | ecbc4907669c06c7a71ffda3bd78a163ee1287fe (patch) | |
tree | 2da5b1ff35ed9b63b9c2de01d470e63be5b39ee1 | |
parent | aaebdd4913a7e5274018675d12231df8caf227a0 (diff) |
generateHash() and checkuser() moved to Userutils
-rw-r--r-- | server-core/src/main/java/com/juick/server/UserQueries.java | 39 | ||||
-rw-r--r-- | server-core/src/main/java/com/juick/util/UserUtils.java | 27 |
2 files changed, 42 insertions, 24 deletions
diff --git a/server-core/src/main/java/com/juick/server/UserQueries.java b/server-core/src/main/java/com/juick/server/UserQueries.java index 50e3562f..bc368390 100644 --- a/server-core/src/main/java/com/juick/server/UserQueries.java +++ b/server-core/src/main/java/com/juick/server/UserQueries.java @@ -21,6 +21,7 @@ import com.juick.User; import com.juick.server.helpers.Auth; import com.juick.server.helpers.EmailOpts; import com.juick.server.helpers.UserInfo; +import com.juick.util.UserUtils; import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -36,7 +37,6 @@ import java.sql.Statement; import java.util.*; /** - * * @author Ugnich Anton */ public class UserQueries { @@ -65,6 +65,7 @@ public class UserQueries { } return hash; } + public static String getSignUpHashByTelegramID(JdbcTemplate sql, Long telegramId, String username) { try { return sql.queryForObject("SELECT loginhash FROM telegram WHERE tg_id=? AND user_id IS NULL", @@ -127,19 +128,19 @@ public class UserQueries { } public static List<User> getUsersByName(JdbcTemplate sql, List<String> unames) { - if (!unames.isEmpty()) { + if (!unames.isEmpty()) { return sql.query("SELECT id,nick,banned FROM users WHERE nick IN (\"" + StringUtils.arrayToDelimitedString(unames.toArray(), "\",\"") + "\")", new UserMapper()); } - return Collections.emptyList(); + return Collections.emptyList(); } public static List<User> getUsersByID(JdbcTemplate sql, List<Integer> uids) { - if (!uids.isEmpty()) { + if (!uids.isEmpty()) { return sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + StringUtils.arrayToCommaDelimitedString(uids.toArray()) + ")", new UserMapper()); - } - return Collections.emptyList(); + } + return Collections.emptyList(); } public static List<com.juick.User> getUsersByJID(JdbcTemplate sql, List<String> jids) { @@ -201,8 +202,8 @@ public class UserQueries { public static String getHashByUID(JdbcTemplate sql, int uid) { try { return sql.queryForObject("SELECT hash FROM logins WHERE user_id=?", String.class, uid); - } catch (EmptyResultDataAccessException e){ - String hash = generateHash(16); + } catch (EmptyResultDataAccessException e) { + String hash = UserUtils.generateHash(16); sql.update(con -> { PreparedStatement stmt = con.prepareStatement("INSERT INTO logins(user_id,hash) VALUES (?,?)"); stmt.setInt(1, uid); @@ -213,19 +214,6 @@ public class UserQueries { } } - public static String generateHash(int len) { - Random rnd = new Random(); - StringBuilder sb = new StringBuilder(len); - for (int i = 0; i < len; i++) { - sb.append(ABCDEF.charAt(rnd.nextInt(ABCDEF.length()))); - } - return sb.toString(); - } - - public static boolean checkUserNameValid(String uname) { - return uname != null && uname.length() >= 2 && uname.length() <= 16 && uname.matches("[a-zA-Z0-9\\-]+"); - } - public static int checkPassword(JdbcTemplate sql, String username, String password) { try { String realPassword = sql.queryForObject("SELECT passw FROM users WHERE nick=?", String.class, username); @@ -249,7 +237,7 @@ public class UserQueries { } public static String updateSecretEmail(JdbcTemplate sql, User user) { - String newHash = generateHash(16); + String newHash = UserUtils.generateHash(16); if (sql.update("INSERT INTO mail(user_id,hash) VALUES (?,?) ON DUPLICATE KEY UPDATE hash=?", user.getUID(), newHash, newHash) > 0) { return newHash; } @@ -285,7 +273,7 @@ public class UserQueries { public static boolean updateUserInfo(JdbcTemplate sql, User user, UserInfo info) { return sql.update("INSERT INTO usersinfo(user_id,fullname,country,url,descr) VALUES (?,?,?,?,?) " + - "ON DUPLICATE KEY UPDATE fullname=?,country=?,url=?,descr=?", user.getUID(), info.getFullName(), + "ON DUPLICATE KEY UPDATE fullname=?,country=?,url=?,descr=?", user.getUID(), info.getFullName(), info.getCountry(), info.getUrl(), info.getDescription(), info.getFullName(), info.getCountry(), info.getUrl(), info.getDescription()) > 0; } @@ -465,9 +453,11 @@ public class UserQueries { } return false; } + public static List<String> getAllJIDs(JdbcTemplate sql, User user) { return sql.queryForList("SELECT jid FROM jids WHERE user_id=?", String.class, user.getUID()); } + public static List<Auth> getAuthCodes(JdbcTemplate sql, User user) { return sql.query("SELECT account,authcode FROM auth WHERE user_id=? AND protocol='xmpp'", (rs, num) -> new Auth(rs.getString(1), rs.getString(2)), user.getUID()); @@ -480,11 +470,12 @@ public class UserQueries { public static EmailOpts getEmailOpts(JdbcTemplate sql, User user) { try { return sql.queryForObject("SELECT email,subscr_hour FROM emails WHERE user_id=? AND subscr_hour IS NOT NULL", - (rs, num) ->new EmailOpts(rs.getString(1), rs.getInt(2)), user.getUID()); + (rs, num) -> new EmailOpts(rs.getString(1), rs.getInt(2)), user.getUID()); } catch (EmptyResultDataAccessException e) { return null; } } + public static String getEmailHash(JdbcTemplate sql, User user) { try { return sql.queryForObject("SELECT hash FROM mail WHERE user_id=?", String.class, user.getUID()) diff --git a/server-core/src/main/java/com/juick/util/UserUtils.java b/server-core/src/main/java/com/juick/util/UserUtils.java new file mode 100644 index 00000000..611a3b15 --- /dev/null +++ b/server-core/src/main/java/com/juick/util/UserUtils.java @@ -0,0 +1,27 @@ +package com.juick.util; + +import java.util.Random; + +/** + * Created by aalexeev on 11/14/16. + */ +public class UserUtils { + private UserUtils() { + throw new IllegalStateException(); + } + + private static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + public static String generateHash(final int len) { + Random rnd = new Random(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + sb.append(ABCDEF.charAt(rnd.nextInt(ABCDEF.length()))); + } + return sb.toString(); + } + + public static boolean checkUserNameValid(final String uname) { + return uname != null && uname.length() >= 2 && uname.length() <= 16 && uname.matches("[a-zA-Z0-9\\-]+"); + } +} |