From 91a07facc6e4417810cebbfe017a4ac167ac13b1 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sat, 26 Nov 2016 04:34:57 +0700 Subject: some improvenments on user service --- .../java/com/juick/service/UserServiceImpl.java | 56 ++++++++++++++-------- .../src/main/java/com/juick/util/UserUtils.java | 5 ++ 2 files changed, 40 insertions(+), 21 deletions(-) (limited to 'juick-server') 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 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 list = getJdbcTemplate().query( - "SELECT id, nick, banned FROM users WHERE nick = ?", new UserMapper(), username); + User result = null; + + if (StringUtils.isNotBlank(username)) { + List 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 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 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 list = getJdbcTemplate().queryForList( - "SELECT passw FROM users WHERE nick = ?", String.class, username); + List 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(); + } } -- cgit v1.2.3