improve room check

This commit is contained in:
Sola
2015-12-15 19:30:53 +08:00
parent 72d92748f0
commit fa572b2d36
4 changed files with 124 additions and 62 deletions

View File

@@ -1,8 +1,13 @@
package love.sola.netsupport.api; package love.sola.netsupport.api;
import com.google.gson.Gson; import com.google.gson.Gson;
import love.sola.netsupport.enums.Attribute;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.sql.SQLCore; import love.sola.netsupport.sql.SQLCore;
import love.sola.netsupport.util.Checker;
import love.sola.netsupport.util.ParseUtil; import love.sola.netsupport.util.ParseUtil;
import love.sola.netsupport.wechat.Command;
import me.chanjar.weixin.common.session.WxSession;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
@@ -38,6 +43,17 @@ public class ProfileModify extends HttpServlet {
} }
private Response process(HttpServletRequest request) { private Response process(HttpServletRequest request) {
WxSession session = Checker.isAuthorized(request, Command.QUERY);
if (session == null) {
return new Response(Response.ResponseCode.UNAUTHORIZED);
}
User u = (User) session.getAttribute(Attribute.USER);
if (u == null) return new Response(Response.ResponseCode.UNAUTHORIZED);
String isp = request.getParameter("isp");
String netaccount = request.getParameter("username");
String block = request.getParameter("block");
String room = request.getParameter("room");
String phone = request.getParameter("phone");
return null; return null;
} }

View File

@@ -20,6 +20,8 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import static love.sola.netsupport.util.Checker.*;
/** /**
* *********************************************** * ***********************************************
* Created by Sola on 2015/11/29. * Created by Sola on 2015/11/29.
@@ -29,8 +31,6 @@ import java.io.PrintWriter;
@WebServlet(name = "Register", urlPatterns = "/api/register", loadOnStartup = 21) @WebServlet(name = "Register", urlPatterns = "/api/register", loadOnStartup = 21)
public class Register extends HttpServlet { public class Register extends HttpServlet {
public static final String STUDENT_ID_REGEX = "^(2010|2012|2013|2014|2015)[0-9]{9}$";
public static final String PHONE_NUMBER_REGEX = "^1[34578][0-9]{9}$";
private Gson gson = SQLCore.gson; private Gson gson = SQLCore.gson;
@@ -105,66 +105,6 @@ public class Register extends HttpServlet {
return "Register_Success"; return "Register_Success";
} }
private long checkStudentId(String studentId) {
if (studentId == null) return -1;
if (studentId.matches(STUDENT_ID_REGEX)) {
try {
return Long.parseLong(studentId);
} catch (NumberFormatException ignored) {
}
}
return -1;
}
private long checkPhoneNumber(String phone) {
if (phone == null) return -1;
if (phone.matches(PHONE_NUMBER_REGEX)) {
try {
return Long.parseLong(phone);
} catch (NumberFormatException ignored) {
}
}
return -1;
}
private ISP checkISP(String isp) {
if (isp == null) return null;
try {
return ISP.fromId(Integer.parseInt(isp));
} catch (NumberFormatException ignored) {
}
return null;
}
private String checkNetAccount(String account, ISP isp) {
if (isp == null) return null;
if (account == null) return null;
if (!account.matches(isp.accountRegex)) return null;
return account;
}
private int checkBlock(String block) {
if (block == null) return -1;
try {
return Integer.parseInt(block);
} catch (NumberFormatException ignored) {
}
return -1;
}
private int checkRoom(String room, int block) {
if (block == -1) return -1;
if (room == null) return -1;
try {
Integer i = Integer.parseInt(room);
if (i <= 100 || i >= 1300) return -1;
return i;
} catch (NumberFormatException ignored) {
}
return -1;
}
private void printAuthorizeFailed(HttpServletRequest request, PrintWriter out) { private void printAuthorizeFailed(HttpServletRequest request, PrintWriter out) {
out.println(ParseUtil.parseJsonP(request, gson.toJson(new Response(Response.ResponseCode.AUTHORIZE_FAILED)))); out.println(ParseUtil.parseJsonP(request, gson.toJson(new Response(Response.ResponseCode.AUTHORIZE_FAILED))));
out.close(); out.close();

View File

@@ -53,4 +53,45 @@ public class Block {
} }
} }
public static final int[][] AVAILABLE = new int[62][0];
static {
// -------------------------------------------- //
// THANKS DATA PROVIDED BY Lai Juncheng
// -------------------------------------------- //
AVAILABLE[FX_1] = new int[]{108, 208, 308, 408, 508};
AVAILABLE[FX_2] = new int[]{110, 210, 310, 410, 510, 610};
AVAILABLE[FX_3] = new int[]{110, 210, 310, 410, 510, 610};
AVAILABLE[FX_4] = new int[]{110, 210, 310, 410, 510, 610, 710};
AVAILABLE[FX_5] = new int[]{108, 208, 308, 408, 508, 608, 708};
AVAILABLE[BM_7] = new int[]{100, 216, 317, 417, 517, 617, 717};
AVAILABLE[BM_8] = new int[]{100, 221, 321, 421, 521, 621, 721};
AVAILABLE[BM_9] = new int[]{100, 221, 321, 421, 521, 621};
AVAILABLE[BM_10] = new int[]{111, 239, 339, 439, 558, 658, 758, 858};
AVAILABLE[BM_11] = new int[]{100, 220, 320, 420, 520, 620, 720, 820};
AVAILABLE[DM_12] = new int[]{119, 221, 321, 421, 521, 621, 720};
AVAILABLE[DM_13] = new int[]{120, 222, 322, 422, 522, 622, 722};
AVAILABLE[DM_14] = new int[]{100, 230, 330, 430, 530, 630, 730};
AVAILABLE[DM_15] = new int[]{119, 219, 319, 419, 519, 619};
AVAILABLE[QT_16] = new int[]{154, 257, 357, 457, 557, 657, 757};
AVAILABLE[QT_17] = new int[]{154, 257, 357, 457, 557, 657, 757};
AVAILABLE[QT_18] = new int[]{139, 239, 339, 439, 539, 639, 739};
AVAILABLE[QT_19] = new int[]{100, 200, 332, 432, 532, 632, 732};
AVAILABLE[DM_20] = new int[]{109, 209, 309, 409, 509, 609, 709};
AVAILABLE[DM_21] = new int[]{109, 209, 309, 409, 509, 609, 709};
AVAILABLE[XH_A] = new int[]{129, 231, 331, 431, 531, 631, 731, 831, 931, 1031, 1131, 1231};
AVAILABLE[XH_B] = new int[]{129, 229, 329, 429, 529, 629, 729, 829, 929, 1029, 1129, 1229};
AVAILABLE[XH_C] = new int[]{126, 226, 326, 426, 526, 626, 726, 826, 926, 1026, 1126, 1226};
AVAILABLE[XH_D] = new int[]{128, 228, 328, 428, 528, 628, 728, 828, 928, 1028, 1128, 1228};
AVAILABLE[FX_6] = new int[0];
}
public static boolean checkRoom(int block, int room) {
int floor = room / 100;
if (floor == 0 || room % 100 == 0) return false;
if (block < 0 || block >= AVAILABLE.length) return false;
if (AVAILABLE[block].length < floor) return false;
return room <= AVAILABLE[block][floor - 1];
}
} }

View File

@@ -1,6 +1,8 @@
package love.sola.netsupport.util; package love.sola.netsupport.util;
import love.sola.netsupport.enums.Attribute; import love.sola.netsupport.enums.Attribute;
import love.sola.netsupport.enums.Block;
import love.sola.netsupport.enums.ISP;
import love.sola.netsupport.wechat.Command; import love.sola.netsupport.wechat.Command;
import love.sola.netsupport.wechat.WechatSession; import love.sola.netsupport.wechat.WechatSession;
import me.chanjar.weixin.common.session.WxSession; import me.chanjar.weixin.common.session.WxSession;
@@ -15,6 +17,9 @@ import javax.servlet.http.HttpServletRequest;
*/ */
public class Checker { public class Checker {
public static final String STUDENT_ID_REGEX = "^(2010|2012|2013|2014|2015)[0-9]{9}$";
public static final String PHONE_NUMBER_REGEX = "^1[34578][0-9]{9}$";
public static boolean hasNull(Object... v) { public static boolean hasNull(Object... v) {
for (Object o : v) if (o == null) return true; for (Object o : v) if (o == null) return true;
return false; return false;
@@ -27,4 +32,64 @@ public class Checker {
return s == null ? null : s.getAttribute(Attribute.AUTHORIZED) == c ? s : null; return s == null ? null : s.getAttribute(Attribute.AUTHORIZED) == c ? s : null;
} }
public static long checkStudentId(String studentId) {
if (studentId == null) return -1;
if (studentId.matches(STUDENT_ID_REGEX)) {
try {
return Long.parseLong(studentId);
} catch (NumberFormatException ignored) {
}
}
return -1;
}
public static long checkPhoneNumber(String phone) {
if (phone == null) return -1;
if (!phone.matches(PHONE_NUMBER_REGEX)) return -1;
try {
return Long.parseLong(phone);
} catch (NumberFormatException ignored) { }
return -1;
}
public static ISP checkISP(String isp) {
if (isp == null) return null;
try {
return ISP.fromId(Integer.parseInt(isp));
} catch (NumberFormatException ignored) { }
return null;
}
public static String checkNetAccount(String account, ISP isp) {
if (isp == null) return null;
if (account == null) return null;
if (!account.matches(isp.accountRegex)) return null;
return account;
}
public static int checkBlock(String block) {
if (block == null) return -1;
try {
int b = Integer.parseInt(block);
if (Block.inverseMap.containsKey(b))
return b;
else
return -1;
} catch (NumberFormatException ignored) { }
return -1;
}
public static int checkRoom(String room, int block) {
if (block == -1) return -1;
if (room == null) return -1;
try {
Integer i = Integer.parseInt(room);
if (Block.checkRoom(block, i))
return i;
else
return -1;
} catch (NumberFormatException ignored) { }
return -1;
}
} }