Commit c8e10867 authored by Liang Ding's avatar Liang Ding

#12518

parent 1bbd90b9
......@@ -17,12 +17,15 @@
*/
package org.b3log.solo.model;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Keys;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Set;
/**
* This class defines option model relevant keys.
*
......@@ -337,6 +340,16 @@ public final class Option {
*/
public static final String CATEGORY_C_OAUTH = "oauth";
public static String getOAuthPair(final Set<String> oauthPairs, final String openIdOrUserId) {
for (final String pair : oauthPairs) {
if (StringUtils.containsIgnoreCase(pair, openIdOrUserId)) {
return pair;
}
}
return null;
}
/**
* Private constructor.
*/
......
......@@ -28,6 +28,7 @@ import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.Role;
import org.b3log.latke.model.User;
import org.b3log.latke.repository.jdbc.JdbcRepository;
import org.b3log.latke.servlet.HTTPRequestMethod;
import org.b3log.latke.servlet.annotation.RequestProcessing;
import org.b3log.latke.servlet.annotation.RequestProcessor;
......@@ -175,7 +176,7 @@ public class OAuthGitHubProcessor {
final JSONArray github = new JSONArray(value);
final Set<String> githubAuths = CollectionUtils.jsonArrayToSet(github);
final String splitChar = ":@:";
final String oAuthPair = getOAuthPair(githubAuths, openId); // openId:@:userId
final String oAuthPair = Option.getOAuthPair(githubAuths, openId); // openId:@:userId
if (StringUtils.isBlank(oAuthPair)) {
if (!initService.isInited()) {
final JSONObject initReq = new JSONObject();
......@@ -204,6 +205,7 @@ public class OAuthGitHubProcessor {
addUserReq.put(UserExt.USER_AVATAR, userAvatar);
addUserReq.put(User.USER_ROLE, Role.VISITOR_ROLE);
userMgmtService.addUser(addUserReq);
JdbcRepository.dispose();
}
}
......@@ -282,14 +284,4 @@ public class OAuthGitHubProcessor {
return null;
}
}
private static String getOAuthPair(final Set<String> oauthPairs, final String openId) {
for (final String pair : oauthPairs) {
if (StringUtils.containsIgnoreCase(pair, openId)) {
return pair;
}
}
return null;
}
}
......@@ -33,17 +33,21 @@ import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.service.annotation.Service;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.latke.util.Crypts;
import org.b3log.latke.util.Sessions;
import org.b3log.latke.util.Strings;
import org.b3log.solo.model.Option;
import org.b3log.solo.model.UserExt;
import org.b3log.solo.repository.UserRepository;
import org.b3log.solo.util.Thumbnails;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;
/**
* User management service.
......@@ -51,7 +55,7 @@ import javax.servlet.http.HttpServletResponse;
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="mailto:385321165@qq.com">DASHU</a>
* @author <a href="https://github.com/nanolikeyou">nanolikeyou</a>
* @version 1.1.0.12, Aug 2, 2018
* @version 1.1.0.13, Sep 21, 2018
* @since 0.4.0
*/
@Service
......@@ -79,6 +83,18 @@ public class UserMgmtService {
@Inject
private LangPropsService langPropsService;
/**
* Option query service.
*/
@Inject
private OptionQueryService optionQueryService;
/**
* Option management service.
*/
@Inject
private OptionMgmtService optionMgmtService;
/**
* Tries to login with cookie.
*
......@@ -364,9 +380,26 @@ public class UserMgmtService {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Removes a user[id=" + userId + "] failed", e);
LOGGER.log(Level.ERROR, "Removes a user [id=" + userId + "] failed", e);
throw new ServiceException(e);
}
final JSONObject oauthGitHubOpt = optionQueryService.getOptionById(Option.ID_C_OAUTH_GITHUB);
if (null != oauthGitHubOpt) {
String value = oauthGitHubOpt.optString(Option.OPTION_VALUE);
try {
final Set<String> githubs = CollectionUtils.jsonArrayToSet(new JSONArray(value));
final String oAuthPair = Option.getOAuthPair(githubs, userId);
if (StringUtils.isNotBlank(oAuthPair)) {
githubs.remove(oAuthPair);
value = new JSONArray(githubs).toString();
oauthGitHubOpt.put(Option.OPTION_VALUE, value);
optionMgmtService.addOrUpdateOption(oauthGitHubOpt);
}
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Remove oauth GitHub data for user [id=" + userId + "] failed", e);
}
}
}
/**
......
Subproject commit 5008cd80069d06f8fed1d08ee6c5fa6a0b0819d1
Subproject commit f9607e2c432ec40fb8bedce05eb232e3bdb071b0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment