Skip to content

Commit

Permalink
♻️ 重构请求路由 #5
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 11, 2020
1 parent a4957ea commit 512cf8d
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import org.b3log.symphony.event.ArticleBaiduSender;
import org.b3log.symphony.model.*;
import org.b3log.symphony.processor.middleware.PermissionMidware;
import org.b3log.symphony.processor.middleware.validate.UserRegister2Validation;
import org.b3log.symphony.processor.middleware.validate.UserRegisterValidation;
import org.b3log.symphony.processor.middleware.validate.UserRegister2ValidationMidware;
import org.b3log.symphony.processor.middleware.validate.UserRegisterValidationMidware;
import org.b3log.symphony.service.*;
import org.b3log.symphony.util.Escapes;
import org.b3log.symphony.util.Sessions;
Expand Down Expand Up @@ -1357,9 +1357,9 @@ public void addUser(final RequestContext context) {
final String password = context.param(User.USER_PASSWORD);
final String appRole = context.param(UserExt.USER_APP_ROLE);

final boolean nameInvalid = UserRegisterValidation.invalidUserName(userName);
final boolean nameInvalid = UserRegisterValidationMidware.invalidUserName(userName);
final boolean emailInvalid = !Strings.isEmail(email);
final boolean passwordInvalid = UserRegister2Validation.invalidUserPassword(password);
final boolean passwordInvalid = UserRegister2ValidationMidware.invalidUserPassword(password);

if (nameInvalid || emailInvalid || passwordInvalid) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(context, "admin/error.ftl");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.b3log.symphony.processor.middleware.LoginCheckMidware;
import org.b3log.symphony.processor.middleware.PermissionMidware;
import org.b3log.symphony.processor.middleware.validate.ArticlePostValidationMidware;
import org.b3log.symphony.processor.middleware.validate.UserRegisterValidation;
import org.b3log.symphony.processor.middleware.validate.UserRegisterValidationMidware;
import org.b3log.symphony.service.*;
import org.b3log.symphony.util.*;
import org.json.JSONObject;
Expand Down Expand Up @@ -836,7 +836,7 @@ public void showArticle(final RequestContext context) {

// Referral statistic
final String referralUserName = context.param("r");
if (!UserRegisterValidation.invalidUserName(referralUserName)) {
if (!UserRegisterValidationMidware.invalidUserName(referralUserName)) {
final JSONObject referralUser = userQueryService.getUserByName(referralUserName);
if (null == referralUser) {
return;
Expand Down
81 changes: 39 additions & 42 deletions src/main/java/org/b3log/symphony/processor/LoginProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,26 @@
import org.apache.logging.log4j.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.http.HttpMethod;
import org.b3log.latke.http.Dispatcher;
import org.b3log.latke.http.Request;
import org.b3log.latke.http.RequestContext;
import org.b3log.latke.http.Response;
import org.b3log.latke.http.annotation.After;
import org.b3log.latke.http.annotation.Before;
import org.b3log.latke.http.annotation.RequestProcessing;
import org.b3log.latke.http.annotation.RequestProcessor;
import org.b3log.latke.http.renderer.AbstractFreeMarkerRenderer;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.ioc.Singleton;
import org.b3log.latke.model.User;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.util.Locales;
import org.b3log.latke.util.Requests;
import org.b3log.symphony.model.*;
import org.b3log.symphony.processor.middleware.CSRFMidware;
import org.b3log.symphony.processor.middleware.LoginCheckMidware;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchEndAdvice;
import org.b3log.symphony.processor.middleware.stopwatch.StopwatchStartAdvice;
import org.b3log.symphony.processor.middleware.validate.UserForgetPwdValidation;
import org.b3log.symphony.processor.middleware.validate.UserRegister2Validation;
import org.b3log.symphony.processor.middleware.validate.UserRegisterValidation;
import org.b3log.symphony.processor.middleware.PermissionMidware;
import org.b3log.symphony.processor.middleware.validate.UserForgetPwdValidationMidware;
import org.b3log.symphony.processor.middleware.validate.UserRegister2ValidationMidware;
import org.b3log.symphony.processor.middleware.validate.UserRegisterValidationMidware;
import org.b3log.symphony.service.*;
import org.b3log.symphony.util.Sessions;
import org.json.JSONObject;
Expand All @@ -65,10 +63,10 @@
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
* @version 1.13.12.8, Sep 6, 2019
* @version 2.0.0.0, Feb 11, 2020
* @since 0.2.0
*/
@RequestProcessor
@Singleton
public class LoginProcessor {

/**
Expand Down Expand Up @@ -162,13 +160,38 @@ public class LoginProcessor {
@Inject
private TagQueryService tagQueryService;

/**
* Register request handlers.
*/
public static void register() {
final BeanManager beanManager = BeanManager.getInstance();
final LoginCheckMidware loginCheck = beanManager.getReference(LoginCheckMidware.class);
final PermissionMidware permissionMidware = beanManager.getReference(PermissionMidware.class);
final CSRFMidware csrfMidware = beanManager.getReference(CSRFMidware.class);
final UserForgetPwdValidationMidware userForgetPwdValidationMidware = beanManager.getReference(UserForgetPwdValidationMidware.class);
final UserRegisterValidationMidware userRegisterValidationMidware = beanManager.getReference(UserRegisterValidationMidware.class);
final UserRegister2ValidationMidware userRegister2ValidationMidware = beanManager.getReference(UserRegister2ValidationMidware.class);

final LoginProcessor loginProcessor = beanManager.getReference(LoginProcessor.class);
Dispatcher.post("/guide/next", loginProcessor::nextGuideStep, loginCheck::handle);
Dispatcher.get("/guide", loginProcessor::showGuide, loginCheck::handle, csrfMidware::fill, permissionMidware::grant);
Dispatcher.get("/login", loginProcessor::showLogin, permissionMidware::grant);
Dispatcher.get("/forget-pwd", loginProcessor::showForgetPwd, permissionMidware::grant);
Dispatcher.post("/forget-pwd", loginProcessor::forgetPwd, userForgetPwdValidationMidware::handle);
Dispatcher.get("/reset-pwd", loginProcessor::showResetPwd, permissionMidware::grant);
Dispatcher.post("/reset-pwd", loginProcessor::resetPwd);
Dispatcher.get("/register", loginProcessor::showRegister, permissionMidware::grant);
Dispatcher.post("/register", loginProcessor::register, userRegisterValidationMidware::handle);
Dispatcher.post("/register2", loginProcessor::register2, userRegister2ValidationMidware::handle);
Dispatcher.post("/login", loginProcessor::login);
Dispatcher.get("/logout", loginProcessor::logout, permissionMidware::grant);
}

/**
* Next guide step.
*
* @param context the specified context
*/
@RequestProcessing(value = "/guide/next", method = HttpMethod.POST)
@Before({LoginCheckMidware.class})
public void nextGuideStep(final RequestContext context) {
context.renderJSON();

Expand Down Expand Up @@ -208,9 +231,6 @@ public void nextGuideStep(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/guide", method = HttpMethod.GET)
@Before({StopwatchStartAdvice.class, LoginCheckMidware.class})
@After({CSRFToken.class, PermissionGrant.class, StopwatchEndAdvice.class})
public void showGuide(final RequestContext context) {
final JSONObject currentUser = Sessions.getUser();
final int step = currentUser.optInt(UserExt.USER_GUIDE_STEP);
Expand Down Expand Up @@ -247,9 +267,6 @@ public void showGuide(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/login", method = HttpMethod.GET)
@Before(StopwatchStartAdvice.class)
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showLogin(final RequestContext context) {
if (Sessions.isLoggedIn()) {
context.sendRedirect(Latkes.getServePath());
Expand Down Expand Up @@ -278,9 +295,6 @@ public void showLogin(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/forget-pwd", method = HttpMethod.GET)
@Before(StopwatchStartAdvice.class)
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showForgetPwd(final RequestContext context) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(context, "verify/forget-pwd.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
Expand All @@ -292,8 +306,6 @@ public void showForgetPwd(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/forget-pwd", method = HttpMethod.POST)
@Before(UserForgetPwdValidation.class)
public void forgetPwd(final RequestContext context) {
context.renderJSON();

Expand Down Expand Up @@ -335,9 +347,6 @@ public void forgetPwd(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/reset-pwd", method = HttpMethod.GET)
@Before(StopwatchStartAdvice.class)
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showResetPwd(final RequestContext context) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(context, null);
context.setRenderer(renderer);
Expand Down Expand Up @@ -365,7 +374,6 @@ public void showResetPwd(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/reset-pwd", method = HttpMethod.POST)
public void resetPwd(final RequestContext context) {
context.renderJSON();

Expand Down Expand Up @@ -411,9 +419,6 @@ public void resetPwd(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/register", method = HttpMethod.GET)
@Before(StopwatchStartAdvice.class)
@After({PermissionGrant.class, StopwatchEndAdvice.class})
public void showRegister(final RequestContext context) {
if (Sessions.isLoggedIn()) {
context.sendRedirect(Latkes.getServePath());
Expand All @@ -428,7 +433,7 @@ public void showRegister(final RequestContext context) {
boolean useInvitationLink = false;

String referral = context.param("r");
if (!UserRegisterValidation.invalidUserName(referral)) {
if (!UserRegisterValidationMidware.invalidUserName(referral)) {
final JSONObject referralUser = userQueryService.getUserByName(referral);
if (null != referralUser) {
dataModel.put(Common.REFERRAL, referral);
Expand Down Expand Up @@ -483,8 +488,6 @@ public void showRegister(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/register", method = HttpMethod.POST)
@Before(UserRegisterValidation.class)
public void register(final RequestContext context) {
context.renderJSON();
final JSONObject requestJSONObject = (JSONObject) context.attr(Keys.REQUEST);
Expand Down Expand Up @@ -541,8 +544,6 @@ public void register(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/register2", method = HttpMethod.POST)
@Before(UserRegister2Validation.class)
public void register2(final RequestContext context) {
context.renderJSON();

Expand Down Expand Up @@ -579,7 +580,7 @@ public void register2(final RequestContext context) {
final String ip = Requests.getRemoteAddr(request);
userMgmtService.updateOnlineStatus(user.optString(Keys.OBJECT_ID), ip, true, true);

if (StringUtils.isNotBlank(referral) && !UserRegisterValidation.invalidUserName(referral)) {
if (StringUtils.isNotBlank(referral) && !UserRegisterValidationMidware.invalidUserName(referral)) {
final JSONObject referralUser = userQueryService.getUserByName(referral);
if (null != referralUser) {
final String referralId = referralUser.optString(Keys.OBJECT_ID);
Expand Down Expand Up @@ -636,7 +637,6 @@ public void register2(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/login", method = HttpMethod.POST)
public void login(final RequestContext context) {
final Request request = context.getRequest();
final Response response = context.getResponse();
Expand Down Expand Up @@ -738,10 +738,7 @@ public void login(final RequestContext context) {
*
* @param context the specified context
*/
@RequestProcessing(value = "/logout", method = HttpMethod.GET)
public void logout(final RequestContext context) {
final Request request = context.getRequest();

final JSONObject user = Sessions.getUser();
if (null != user) {
Sessions.logout(user.optString(Keys.OBJECT_ID), context.getResponse());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void doAdvice(final RequestContext context) throws RequestProcessAdviceEx
}

final String pwd = requestJSONObject.optString(User.USER_PASSWORD);
if (UserRegisterValidation.invalidUserPassword(pwd)) {
if (UserRegisterValidationMidware.invalidUserPassword(pwd)) {
throw new RequestProcessAdviceException(new JSONObject().put(Keys.MSG, langPropsService.get("invalidPasswordLabel")));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
package org.b3log.symphony.processor.middleware.validate;

import org.b3log.latke.Keys;
import org.b3log.latke.http.Request;
import org.b3log.latke.http.RequestContext;
import org.b3log.latke.http.advice.ProcessAdvice;
import org.b3log.latke.http.advice.RequestProcessAdviceException;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.ioc.Singleton;
import org.b3log.latke.model.User;
Expand All @@ -34,50 +31,37 @@
* User forget password form validation.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.1.0, Mar 10, 2016
* @version 2.0.0.0, Feb 11, 2020
* @since 1.4.0
*/
@Singleton
public class UserForgetPwdValidation extends ProcessAdvice {
public class UserForgetPwdValidationMidware {

/**
* Language service.
*/
@Inject
private LangPropsService langPropsService;

@Override
public void doAdvice(final RequestContext context) throws RequestProcessAdviceException {
final Request request = context.getRequest();

JSONObject requestJSONObject;
try {
requestJSONObject = context.requestJSON();
request.setAttribute(Keys.REQUEST, requestJSONObject);
} catch (final Exception e) {
throw new RequestProcessAdviceException(new JSONObject().put(Keys.MSG, e.getMessage()));
}

public void handle(final RequestContext context) {
final JSONObject requestJSONObject = context.requestJSON();
final String email = requestJSONObject.optString(User.USER_EMAIL);
final String captcha = requestJSONObject.optString(CaptchaProcessor.CAPTCHA);

checkField(CaptchaProcessor.invalidCaptcha(captcha), "submitFailedLabel", "captchaErrorLabel");
checkField(!Strings.isEmail(email), "submitFailedLabel", "invalidEmailLabel");
}
if (CaptchaProcessor.invalidCaptcha(captcha)) {
context.renderJSON(new JSONObject().put(Keys.MSG, langPropsService.get("submitFailedLabel") + " - " + langPropsService.get("captchaErrorLabel")));
context.abort();

/**
* Checks field.
*
* @param invalid the specified invalid flag
* @param failLabel the specified fail label
* @param fieldLabel the specified field label
* @throws RequestProcessAdviceException request process advice exception
*/
private void checkField(final boolean invalid, final String failLabel, final String fieldLabel)
throws RequestProcessAdviceException {
if (invalid) {
throw new RequestProcessAdviceException(new JSONObject().put(Keys.MSG, langPropsService.get(failLabel)
+ " - " + langPropsService.get(fieldLabel)));
return;
}

if (!Strings.isEmail(email)) {
context.renderJSON(new JSONObject().put(Keys.MSG, langPropsService.get("submitFailedLabel") + " - " + langPropsService.get("invalidEmailLabel")));
context.abort();

return;
}

context.handle();
}
}
Loading

0 comments on commit 512cf8d

Please sign in to comment.