diff options
author | 2016-11-26 04:34:57 +0700 | |
---|---|---|
committer | 2016-11-26 04:34:57 +0700 | |
commit | 91a07facc6e4417810cebbfe017a4ac167ac13b1 (patch) | |
tree | 741b055031d63ea5ac0d4b33bc399d97e456b197 /juick-server/src | |
parent | b0d89589ae42ba9c099ef934c39bebc6b0a75f7f (diff) |
some improvenments on user service
Diffstat (limited to 'juick-server/src')
-rw-r--r-- | juick-server/src/main/java/com/juick/service/UserServiceImpl.java | 56 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/util/UserUtils.java | 5 |
2 files changed, 40 insertions, 21 deletions
diff --git a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java index a84f2fc3..2bdc4d41 100644 --- a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java @@ -1,7 +1,6 @@ package com.juick.service; import com.juick.User; -import com.juick.server.UserQueries; import com.juick.server.helpers.Auth; import com.juick.server.helpers.EmailOpts; import com.juick.server.helpers.UserInfo; @@ -112,30 +111,39 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { List<User> list = getJdbcTemplate().query( "SELECT id, nick,banned FROM users WHERE id = ?", new UserMapper(), uid); - return list.isEmpty() ? - Optional.empty() : Optional.of(list.get(0)); + return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); } @Transactional(readOnly = true) @Override public User getUserByName(final String username) { - List<User> list = getJdbcTemplate().query( - "SELECT id, nick, banned FROM users WHERE nick = ?", new UserMapper(), username); + User result = null; + + if (StringUtils.isNotBlank(username)) { + List<User> list = getJdbcTemplate().query( + "SELECT id, nick, banned FROM users WHERE nick = ?", new UserMapper(), username); - return list.isEmpty() ? - null : list.get(0); + if (!list.isEmpty()) + result = list.get(0); + } + return result; } @Transactional(readOnly = true) @Override public User getUserByJID(final String jid) { - List<User> list = getJdbcTemplate().query( - "SELECT id, nick, banned FROM users WHERE id = (SELECT user_id FROM jids WHERE jid = ?)", - new UserMapper(), - jid); + User result = null; + + if (StringUtils.isNotBlank(jid)) { + List<User> list = getJdbcTemplate().query( + "SELECT id, nick, banned FROM users WHERE id = (SELECT user_id FROM jids WHERE jid = ?)", + new UserMapper(), + jid); - return list.isEmpty() ? - null : list.get(0); + if (!list.isEmpty()) + result = list.get(0); + } + return result; } @Transactional(readOnly = true) @@ -276,16 +284,22 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { @Override public int checkPassword(final String username, final String password) { if (StringUtils.isNotBlank(username)) { - List<String> list = getJdbcTemplate().queryForList( - "SELECT passw FROM users WHERE nick = ?", String.class, username); + List<User> list = getJdbcTemplate().query( + "SELECT id, nick, banned, passw FROM users WHERE nick = ?", + (rs, rowNum) -> { + User user = new User(); + user.setUid(rs.getInt(1)); + user.setName(rs.getString(2)); + user.setBanned(rs.getBoolean(3)); + user.setCredentials(rs.getString(4)); + return user; + }, + username); if (!list.isEmpty()) { - String realPassword = list.get(0); - if (realPassword.equals(password)) { - User user = getUserByName(username); - if (user != null) - return user.getUid(); - } + User user = list.get(0); + if (Objects.equals(password, user.getCredentials())) + return user.getUid(); } } return -1; diff --git a/juick-server/src/main/java/com/juick/util/UserUtils.java b/juick-server/src/main/java/com/juick/util/UserUtils.java index 611a3b15..372dd502 100644 --- a/juick-server/src/main/java/com/juick/util/UserUtils.java +++ b/juick-server/src/main/java/com/juick/util/UserUtils.java @@ -1,5 +1,6 @@ package com.juick.util; +import java.security.Principal; import java.util.Random; /** @@ -24,4 +25,8 @@ public class UserUtils { public static boolean checkUserNameValid(final String uname) { return uname != null && uname.length() >= 2 && uname.length() <= 16 && uname.matches("[a-zA-Z0-9\\-]+"); } + + public static String getUsername(final Principal principal, final String defaultUsername) { + return principal == null ? defaultUsername : principal.getName(); + } } |