diff options
3 files changed, 72 insertions, 21 deletions
diff --git a/juick-core/src/main/java/com/juick/User.java b/juick-core/src/main/java/com/juick/User.java index f71ec781..03805a65 100644 --- a/juick-core/src/main/java/com/juick/User.java +++ b/juick-core/src/main/java/com/juick/User.java @@ -19,6 +19,8 @@ package com.juick; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.ToStringBuilder; import javax.xml.bind.annotation.*; @@ -36,6 +38,7 @@ public class User { private int messagesCount; private String authHash; private boolean banned; + private String credentials; public User() { } @@ -57,6 +60,25 @@ public class User { (obj instanceof User && ((User) obj).getUid() == this.getUid()); } + @Override + public int hashCode() { + return new HashCodeBuilder() + .append(getUid()) + .toHashCode(); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("uid", uid) + .append("name", name) + .append("fullName", fullName) + .append("jid", jid) + .append("messagesCount", messagesCount) + .append("banned", banned) + .toString(); + } + @JsonProperty("uid") @XmlAttribute(name = "uid") public int getUid() { @@ -132,4 +154,14 @@ public class User { public void setAvatar(Object avatar) { this.avatar = avatar; } + + @XmlTransient + @JsonIgnore + public String getCredentials() { + return credentials; + } + + public void setCredentials(String credentials) { + this.credentials = credentials; + } } 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(); + } } |