| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9d27af3 commit 7fd6a60
16 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,19 +27,18 @@ | |||
| 27 | 27 | ||
| 28 | 28 | import apijson.demo.server.model.BaseModel; | |
| 29 | 29 | import apijson.demo.server.model.Comment; | |
| 30 | - import apijson.demo.server.model.Login; | ||
| 31 | 30 | import apijson.demo.server.model.Moment; | |
| 32 | - import apijson.demo.server.model.Password; | ||
| 33 | - import apijson.demo.server.model.User; | ||
| 34 | 31 | import apijson.demo.server.model.Privacy; | |
| 32 | + import apijson.demo.server.model.User; | ||
| 35 | 33 | import apijson.demo.server.model.Verify; | |
| 36 | - import apijson.demo.server.model.Wallet; | ||
| 37 | 34 | import zuo.biao.apijson.JSON; | |
| 38 | - import zuo.biao.apijson.JSONRequest; | ||
| 39 | 35 | import zuo.biao.apijson.Log; | |
| 40 | 36 | import zuo.biao.apijson.MethodAccess; | |
| 41 | 37 | import zuo.biao.apijson.RequestMethod; | |
| 42 | 38 | import zuo.biao.apijson.RequestRole; | |
| 39 | + import zuo.biao.apijson.model.Column; | ||
| 40 | + import zuo.biao.apijson.model.Table; | ||
| 41 | + import zuo.biao.apijson.model.Test; | ||
| 43 | 42 | import zuo.biao.apijson.server.exception.NotLoggedInException; | |
| 44 | 43 | import zuo.biao.apijson.server.sql.SQLConfig; | |
| 45 | 44 | ||
@@ -57,18 +56,19 @@ public class AccessVerifier { | |||
| 57 | 56 | ||
| 58 | 57 | // <TableName, <METHOD, allowRoles>> | |
| 59 | 58 | // <User, <GET, [OWNER, ADMIN]>> | |
| 60 | - public static final Map<String, Map<RequestMethod, RequestRole[]>> accessMap; | ||
| 59 | + public static final Map<String, Map<RequestMethod, RequestRole[]>> ACCESS_MAP; | ||
| 61 | 60 | static { | |
| 62 | - accessMap = new HashMap<String, Map<RequestMethod, RequestRole[]>>(); | ||
| 63 | - | ||
| 64 | - accessMap.put(User.class.getSimpleName(), getAccessMap(User.class.getAnnotation(MethodAccess.class))); | ||
| 65 | - accessMap.put(Privacy.class.getSimpleName(), getAccessMap(Privacy.class.getAnnotation(MethodAccess.class))); | ||
| 66 | - accessMap.put(Moment.class.getSimpleName(), getAccessMap(Moment.class.getAnnotation(MethodAccess.class))); | ||
| 67 | - accessMap.put(Comment.class.getSimpleName(), getAccessMap(Comment.class.getAnnotation(MethodAccess.class))); | ||
| 68 | - accessMap.put(Verify.class.getSimpleName(), getAccessMap(Verify.class.getAnnotation(MethodAccess.class))); | ||
| 69 | - accessMap.put(Login.class.getSimpleName(), getAccessMap(Login.class.getAnnotation(MethodAccess.class))); | ||
| 70 | - accessMap.put(Password.class.getSimpleName(), getAccessMap(Password.class.getAnnotation(MethodAccess.class))); | ||
| 71 | - accessMap.put(Wallet.class.getSimpleName(), getAccessMap(Wallet.class.getAnnotation(MethodAccess.class))); | ||
| 61 | + ACCESS_MAP = new HashMap<String, Map<RequestMethod, RequestRole[]>>(); | ||
| 62 | + | ||
| 63 | + ACCESS_MAP.put(Table.class.getSimpleName(), getAccessMap(Table.class.getAnnotation(MethodAccess.class))); | ||
| 64 | + ACCESS_MAP.put(Column.class.getSimpleName(), getAccessMap(Column.class.getAnnotation(MethodAccess.class))); | ||
| 65 | + ACCESS_MAP.put(Test.class.getSimpleName(), getAccessMap(Test.class.getAnnotation(MethodAccess.class))); | ||
| 66 | + | ||
| 67 | + ACCESS_MAP.put(User.class.getSimpleName(), getAccessMap(User.class.getAnnotation(MethodAccess.class))); | ||
| 68 | + ACCESS_MAP.put(Privacy.class.getSimpleName(), getAccessMap(Privacy.class.getAnnotation(MethodAccess.class))); | ||
| 69 | + ACCESS_MAP.put(Moment.class.getSimpleName(), getAccessMap(Moment.class.getAnnotation(MethodAccess.class))); | ||
| 70 | + ACCESS_MAP.put(Comment.class.getSimpleName(), getAccessMap(Comment.class.getAnnotation(MethodAccess.class))); | ||
| 71 | + ACCESS_MAP.put(Verify.class.getSimpleName(), getAccessMap(Verify.class.getAnnotation(MethodAccess.class))); | ||
| 72 | 72 | } | |
| 73 | 73 | ||
| 74 | 74 | /**获取权限Map,每种操作都只允许对应的角色 | |
@@ -119,7 +119,7 @@ public static boolean verify(SQLConfig config, User visitor) throws Exception { | |||
| 119 | 119 | ||
| 120 | 120 | //验证角色,假定真实强制匹配<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
| 121 | 121 | ||
| 122 | - String userIdkey = Controller.USER_.equals(config.getTable()) || Controller.USER_PRIVACY_.equals(config.getTable()) | ||
| 122 | + String userIdkey = Controller.USER_.equals(config.getTable()) || Controller.PRIVACY_.equals(config.getTable()) | ||
| 123 | 123 | ? Controller.ID : Controller.USER_ID; | |
| 124 | 124 | ||
| 125 | 125 | if (role == null) { | |
@@ -202,7 +202,7 @@ public static void verifyRole(String table, RequestMethod method, RequestRole ro | |||
| 202 | 202 | if (role == null) { | |
| 203 | 203 | role = RequestRole.UNKNOWN; | |
| 204 | 204 | } | |
| 205 | - Map<RequestMethod, RequestRole[]> map = accessMap.get(table); | ||
| 205 | + Map<RequestMethod, RequestRole[]> map = ACCESS_MAP.get(table); | ||
| 206 | 206 | ||
| 207 | 207 | if (map == null || BaseModel.isContain(map.get(method), role) == false) { | |
| 208 | 208 | throw new IllegalAccessException(table + " 不允许 " + role.name() + " 用户的 " + method.name() + " 请求!"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,16 +36,12 @@ | |||
| 36 | 36 | ||
| 37 | 37 | import apijson.demo.server.model.BaseModel; | |
| 38 | 38 | import apijson.demo.server.model.Comment; | |
| 39 | - import apijson.demo.server.model.Login; | ||
| 40 | 39 | import apijson.demo.server.model.Moment; | |
| 41 | - import apijson.demo.server.model.Password; | ||
| 42 | - import apijson.demo.server.model.User; | ||
| 43 | 40 | import apijson.demo.server.model.Privacy; | |
| 41 | + import apijson.demo.server.model.User; | ||
| 44 | 42 | import apijson.demo.server.model.Verify; | |
| 45 | - import apijson.demo.server.model.Wallet; | ||
| 46 | 43 | import zuo.biao.apijson.JSON; | |
| 47 | 44 | import zuo.biao.apijson.JSONResponse; | |
| 48 | - import zuo.biao.apijson.Log; | ||
| 49 | 45 | import zuo.biao.apijson.RequestMethod; | |
| 50 | 46 | import zuo.biao.apijson.StringUtil; | |
| 51 | 47 | import zuo.biao.apijson.server.JSONRequest; | |
@@ -160,19 +156,15 @@ public String delete(@RequestBody String request, HttpSession session) { | |||
| 160 | 156 | ||
| 161 | 157 | ||
| 162 | 158 | public static final String USER_; | |
| 159 | + public static final String PRIVACY_; | ||
| 163 | 160 | public static final String MOMENT_; | |
| 164 | 161 | public static final String COMMENT_; | |
| 165 | - public static final String WALLET_; | ||
| 166 | - public static final String PASSWORD_; | ||
| 167 | - public static final String USER_PRIVACY_; | ||
| 168 | - public static final String VERIFY_; | ||
| 162 | + public static final String VERIFY_; //加下划线后缀是为了避免 Verify 和 verify 都叫VERIFY,分不清 | ||
| 169 | 163 | static { | |
| 170 | 164 | USER_ = User.class.getSimpleName(); | |
| 165 | + PRIVACY_ = Privacy.class.getSimpleName(); | ||
| 171 | 166 | MOMENT_ = Moment.class.getSimpleName(); | |
| 172 | 167 | COMMENT_ = Comment.class.getSimpleName(); | |
| 173 | - WALLET_ = Wallet.class.getSimpleName(); | ||
| 174 | - PASSWORD_ = Password.class.getSimpleName(); | ||
| 175 | - USER_PRIVACY_ = Privacy.class.getSimpleName(); | ||
| 176 | 168 | VERIFY_ = Verify.class.getSimpleName(); | |
| 177 | 169 | } | |
| 178 | 170 | ||
@@ -187,8 +179,8 @@ public String delete(@RequestBody String request, HttpSession session) { | |||
| 187 | 179 | public static final String NAME = "name"; | |
| 188 | 180 | public static final String PHONE = "phone"; | |
| 189 | 181 | public static final String PASSWORD = "password"; | |
| 190 | - public static final String LOGIN_PASSWORD = "loginPassword"; | ||
| 191 | - public static final String PAY_PASSWORD = "payPassword"; | ||
| 182 | + public static final String _PASSWORD = "_password"; | ||
| 183 | + public static final String _PAY_PASSWORD = "_payPassword"; | ||
| 192 | 184 | public static final String OLD_PASSWORD = "oldPassword"; | |
| 193 | 185 | public static final String VERIFY = "verify"; | |
| 194 | 186 | ||
@@ -339,7 +331,8 @@ private JSONObject newVerifyRequest(String phone, String verify) { | |||
| 339 | 331 | } | |
| 340 | 332 | ||
| 341 | 333 | ||
| 342 | - | ||
| 334 | + public static final int LOGIN_TYPE_PASSWORD = 0;//密码登录 | ||
| 335 | + public static final int LOGIN_TYPE_VERIFY = 1;//验证码登录 | ||
| 343 | 336 | /**用户登录 | |
| 344 | 337 | * @param request 只用String,避免encode后未decode | |
| 345 | 338 | * @return | |
@@ -379,7 +372,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) { | |||
| 379 | 372 | new Privacy().setPhone(phone) | |
| 380 | 373 | ) | |
| 381 | 374 | ); | |
| 382 | - JSONResponse response = new JSONResponse(phoneResponse).getJSONResponse(USER_PRIVACY_); | ||
| 375 | + JSONResponse response = new JSONResponse(phoneResponse).getJSONResponse(PRIVACY_); | ||
| 383 | 376 | if (JSONResponse.isSucceed(response) == false) { | |
| 384 | 377 | return response; | |
| 385 | 378 | } | |
@@ -403,7 +396,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) { | |||
| 403 | 396 | ||
| 404 | 397 | //校验凭证 | |
| 405 | 398 | int type = Integer.valueOf(0 + StringUtil.getNumber(typeString)); | |
| 406 | - if (type == Login.TYPE_PASSWORD) {//password密码登录 | ||
| 399 | + if (type == LOGIN_TYPE_PASSWORD) {//password密码登录 | ||
| 407 | 400 | response = new JSONResponse( | |
| 408 | 401 | new Parser(POST_HEAD, true).parseResponse( | |
| 409 | 402 | new JSONRequest(new Privacy(userId).setPassword(password)) | |
@@ -415,7 +408,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) { | |||
| 415 | 408 | if (JSONResponse.isSucceed(response) == false) { | |
| 416 | 409 | return response; | |
| 417 | 410 | } | |
| 418 | - response = response.getJSONResponse(type == Login.TYPE_PASSWORD ? USER_PRIVACY_ : VERIFY_); | ||
| 411 | + response = response.getJSONResponse(type == LOGIN_TYPE_PASSWORD ? PRIVACY_ : VERIFY_); | ||
| 419 | 412 | if (JSONResponse.isExist(response) == false) { | |
| 420 | 413 | return Parser.newErrorResult(new ConditionErrorException("账号或密码错误")); | |
| 421 | 414 | } | |
@@ -434,7 +427,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) { | |||
| 434 | 427 | session.setAttribute(USER_ID, userId);//用户id | |
| 435 | 428 | session.setAttribute(TYPE, type);//登录方式 | |
| 436 | 429 | session.setAttribute(USER_, user);//用户 | |
| 437 | - session.setAttribute(USER_PRIVACY_, privacy);//用户隐私信息 | ||
| 430 | + session.setAttribute(PRIVACY_, privacy);//用户隐私信息 | ||
| 438 | 431 | // session.setMaxInactiveInterval(1*60);//设置session过期时间 | |
| 439 | 432 | ||
| 440 | 433 | return response; | |
@@ -508,7 +501,7 @@ public JSONObject register(@RequestBody String request) { | |||
| 508 | 501 | new Privacy().setPhone(phone) | |
| 509 | 502 | ) | |
| 510 | 503 | ); | |
| 511 | - JSONObject checkUser = check == null ? null : check.getJSONObject(USER_PRIVACY_); | ||
| 504 | + JSONObject checkUser = check == null ? null : check.getJSONObject(PRIVACY_); | ||
| 512 | 505 | if (checkUser == null || checkUser.getIntValue(JSONResponse.KEY_COUNT) > 0) { | |
| 513 | 506 | return Parser.newErrorResult(new ConflictException("手机号" + phone + "已经注册")); | |
| 514 | 507 | } | |
@@ -531,7 +524,7 @@ public JSONObject register(@RequestBody String request) { | |||
| 531 | 524 | ) | |
| 532 | 525 | ) | |
| 533 | 526 | ); | |
| 534 | - if (JSONResponse.isSucceed(response.getJSONResponse(USER_PRIVACY_)) == false) {//创建失败,删除新增的无效User和userPrivacy | ||
| 527 | + if (JSONResponse.isSucceed(response.getJSONResponse(PRIVACY_)) == false) {//创建失败,删除新增的无效User和userPrivacy | ||
| 535 | 528 | ||
| 536 | 529 | new Parser(DELETE, true).parseResponse( | |
| 537 | 530 | new JSONRequest( | |
@@ -624,26 +617,39 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) { | |||
| 624 | 617 | } catch (Exception e) { | |
| 625 | 618 | return Parser.newErrorResult(e); | |
| 626 | 619 | } | |
| 620 | + JSONObject privacyObj; | ||
| 621 | + long userId; | ||
| 622 | + String payPassword; | ||
| 623 | + double change; | ||
| 624 | + try { | ||
| 625 | + privacyObj = requestObject.getJSONObject(PRIVACY_); | ||
| 626 | + if (privacyObj == null) { | ||
| 627 | + throw new NullPointerException("请设置 " + PRIVACY_ + "!"); | ||
| 628 | + } | ||
| 629 | + userId = privacyObj.getLongValue(ID); | ||
| 630 | + payPassword = privacyObj.getString(_PAY_PASSWORD); | ||
| 631 | + change = privacyObj.getDoubleValue("balance+"); | ||
| 632 | + | ||
| 633 | + if (userId <= 0) { | ||
| 634 | + throw new IllegalArgumentException(PRIVACY_ + "." + ID + ":value 中value不合法!"); | ||
| 635 | + } | ||
| 636 | + if (StringUtil.isPassword(payPassword) == false) { | ||
| 637 | + throw new IllegalArgumentException(PRIVACY_ + "." + _PAY_PASSWORD + ":value 中value不合法!"); | ||
| 638 | + } | ||
| 639 | + } catch (Exception e) { | ||
| 640 | + return Parser.extendErrorResult(requestObject, e); | ||
| 641 | + } | ||
| 627 | 642 | ||
| 628 | 643 | //验证密码<<<<<<<<<<<<<<<<<<<<<<< | |
| 629 | 644 | ||
| 630 | - JSONObject pwdObj = requestObject.getJSONObject(PASSWORD_); | ||
| 631 | - requestObject.remove(PASSWORD_); | ||
| 632 | - if (pwdObj == null) { | ||
| 633 | - pwdObj = new JSONRequest(); | ||
| 634 | - } | ||
| 635 | - if (pwdObj.getIntValue(TYPE) != Password.TYPE_PAY) { | ||
| 636 | - // return Parser.extendErrorResult(requestObject, new ConditionErrorException("Password type必须是支付类型!")); | ||
| 637 | - pwdObj.put(TYPE, Password.TYPE_PAY); | ||
| 638 | - } | ||
| 639 | - | ||
| 645 | + privacyObj.remove("balance+"); | ||
| 640 | 646 | JSONResponse response = new JSONResponse( | |
| 641 | 647 | new Parser(POST_HEAD, true).setSession(session).parseResponse( | |
| 642 | - new JSONRequest(PASSWORD_, pwdObj) | ||
| 648 | + new JSONRequest(PRIVACY_, privacyObj) | ||
| 643 | 649 | ) | |
| 644 | 650 | ); | |
| 645 | - response = response.getJSONResponse(PASSWORD_); | ||
| 646 | - if (response == null || response.isExist() == false) { | ||
| 651 | + response = response.getJSONResponse(PRIVACY_); | ||
| 652 | + if (JSONResponse.isExist(response) == false) { | ||
| 647 | 653 | return Parser.extendErrorResult(requestObject, new ConditionErrorException("支付密码错误!")); | |
| 648 | 654 | } | |
| 649 | 655 | ||
@@ -652,13 +658,6 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) { | |||
| 652 | 658 | ||
| 653 | 659 | //验证金额范围<<<<<<<<<<<<<<<<<<<<<<< | |
| 654 | 660 | ||
| 655 | - JSONObject wallet = requestObject.getJSONObject(WALLET_); | ||
| 656 | - long id = wallet == null ? 0 : wallet.getLongValue(ID); | ||
| 657 | - if (id <= 0) { | ||
| 658 | - return Parser.extendErrorResult(requestObject, new ConditionErrorException("请设置Wallet及内部的id!")); | ||
| 659 | - } | ||
| 660 | - | ||
| 661 | - double change = wallet.getDoubleValue("balance+"); | ||
| 662 | 661 | if (change == 0) { | |
| 663 | 662 | return Parser.extendErrorResult(requestObject, new OutOfRangeException("balance+的值不能为0!")); | |
| 664 | 663 | } | |
@@ -672,20 +671,26 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) { | |||
| 672 | 671 | response = new JSONResponse( | |
| 673 | 672 | new Parser(POST_GET, true).parseResponse( | |
| 674 | 673 | new JSONRequest( | |
| 675 | - new Wallet(id).setUserId(AccessVerifier.getUserId(session)) | ||
| 674 | + new Privacy(userId) | ||
| 676 | 675 | ) | |
| 677 | 676 | ) | |
| 678 | 677 | ); | |
| 679 | - Wallet w = response == null ? null : response.getObject(Wallet.class); | ||
| 680 | - if (w == null) { | ||
| 678 | + Privacy privacy = response == null ? null : response.getObject(Privacy.class); | ||
| 679 | + long id = privacy == null ? 0 : BaseModel.value(privacy.getId()); | ||
| 680 | + if (id != userId) { | ||
| 681 | 681 | return Parser.extendErrorResult(requestObject, new Exception("服务器内部错误!")); | |
| 682 | 682 | } | |
| 683 | 683 | ||
| 684 | - if (w.getBalance() == null || w.getBalance().doubleValue() < -change) { | ||
| 684 | + if (BaseModel.value(privacy.getBalance()) < -change) { | ||
| 685 | 685 | return Parser.extendErrorResult(requestObject, new OutOfRangeException("余额不足!")); | |
| 686 | 686 | } | |
| 687 | 687 | } | |
| 688 | 688 | ||
| 689 | + | ||
| 690 | + privacyObj.remove(_PAY_PASSWORD); | ||
| 691 | + privacyObj.put("balance+", change); | ||
| 692 | + requestObject.put(PRIVACY_, privacyObj); | ||
| 693 | + requestObject.put(JSONRequest.KEY_TAG, PRIVACY_); | ||
| 689 | 694 | //不免验证,里面会验证身份 | |
| 690 | 695 | return new Parser(PUT).setSession(session).parseResponse(requestObject); | |
| 691 | 696 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,56 +18,7 @@ | |||
| 18 | 18 | ||
| 19 | 19 | /**评论类 | |
| 20 | 20 | * @author Lemon | |
| 21 | - * @see | ||
| 22 | - * <br >POST:<pre> | ||
| 23 | - { | ||
| 24 | - "Comment":{ | ||
| 25 | - "disallow":"id", | ||
| 26 | - "necessary":"userId,momentId,content" | ||
| 27 | - } | ||
| 28 | - } | ||
| 29 | - * </pre> | ||
| 30 | 21 | */ | |
| 31 | 22 | @MethodAccess | |
| 32 | - public class Comment extends BaseModel { | ||
| 33 | - private static final long serialVersionUID = 1L; | ||
| 34 | - | ||
| 35 | - private Long toId; | ||
| 36 | - private Long momentId; | ||
| 37 | - private String content; | ||
| 38 | - public Comment() { | ||
| 39 | - super(); | ||
| 40 | - } | ||
| 41 | - public Comment(long id) { | ||
| 42 | - this(); | ||
| 43 | - setId(id); | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - public Long getToId() { | ||
| 48 | - return toId; | ||
| 49 | - } | ||
| 50 | - public Comment setToId(Long toId) { | ||
| 51 | - this.toId = toId; | ||
| 52 | - return this; | ||
| 53 | - } | ||
| 54 | - public Comment setUserId(Long userId) { | ||
| 55 | - super.setUserId(userId); | ||
| 56 | - return this; | ||
| 57 | - } | ||
| 58 | - public Long getMomentId() { | ||
| 59 | - return momentId; | ||
| 60 | - } | ||
| 61 | - public Comment setMomentId(Long momentId) { | ||
| 62 | - this.momentId = momentId; | ||
| 63 | - return this; | ||
| 64 | - } | ||
| 65 | - public String getContent() { | ||
| 66 | - return content; | ||
| 67 | - } | ||
| 68 | - public Comment setContent(String content) { | ||
| 69 | - this.content = content; | ||
| 70 | - return this; | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - } | ||
| 23 | + public class Comment { | ||
| 24 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments