Skip to content

Commit e4cbf09

Browse files
committed
远程函数:改用支持引用路径的 getArgVal 方法来取参数值
1 parent 7f00d0b commit e4cbf09

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoFunctionParser.java

+27-22
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414

1515
package apijson.demo;
1616

17-
import java.util.ArrayList;
18-
import java.util.Arrays;
19-
import java.util.Collection;
20-
import java.util.List;
17+
import java.util.*;
2118

2219
import javax.servlet.http.HttpSession;
2320

2421
import apijson.orm.script.JavaScriptExecutor;
22+
import com.alibaba.fastjson.JSON;
2523
import com.alibaba.fastjson.JSONArray;
2624
import com.alibaba.fastjson.JSONObject;
2725

@@ -49,29 +47,30 @@ public class DemoFunctionParser extends APIJSONFunctionParser {
4947
public DemoFunctionParser() {
5048
this(null, null, 0, null, null);
5149
}
50+
5251
public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) {
5352
super(method, tag, version, request, session);
5453
}
55-
54+
5655
public Visitor<Long> getCurrentUser(@NotNull JSONObject curObj) {
5756
return DemoVerifier.getVisitor(getSession());
5857
}
59-
58+
6059
public Long getCurrentUserId(@NotNull JSONObject curObj) {
6160
return DemoVerifier.getVisitorId(getSession());
6261
}
63-
62+
6463
public List<Long> getCurrentUserIdAsList(@NotNull JSONObject curObj) {
6564
List<Long> list = new ArrayList<>(1);
6665
list.add(DemoVerifier.getVisitorId(getSession()));
6766
return list;
6867
}
69-
68+
7069
public List<Long> getCurrentContactIdList(@NotNull JSONObject curObj) {
7170
Visitor<Long> user = getCurrentUser(curObj);
7271
return user == null ? null : user.getContactIdList();
7372
}
74-
73+
7574

7675
/**
7776
* @param curObj
@@ -80,15 +79,15 @@ public List<Long> getCurrentContactIdList(@NotNull JSONObject curObj) {
8079
* @throws Exception
8180
*/
8281
public void verifyIdList(@NotNull JSONObject curObj, @NotNull String idList) throws Exception {
83-
Object obj = curObj.get(idList);
82+
Object obj = getArgVal(idList);
8483
if (obj == null) {
8584
return;
8685
}
87-
86+
8887
if (obj instanceof Collection == false) {
8988
throw new IllegalArgumentException(idList + " 不符合 Array 数组类型! 结构必须是 [] !");
9089
}
91-
90+
9291
Collection<?> collection = (Collection<?>) obj;
9392
if (collection != null) {
9493
int i = -1;
@@ -101,6 +100,12 @@ public void verifyIdList(@NotNull JSONObject curObj, @NotNull String idList) thr
101100
}
102101
}
103102

103+
@Override
104+
public boolean isContain(JSONObject curObj, String array, String value) {
105+
List<String> list = apijson.JSON.parseArray(JSON.toJSONString(getArgVal(array)), String.class);
106+
Object val = getArgVal(value);
107+
return list != null && list.contains(val == null ? null : String.valueOf(val));
108+
}
104109

105110
/**
106111
* @param curObj
@@ -109,15 +114,15 @@ public void verifyIdList(@NotNull JSONObject curObj, @NotNull String idList) thr
109114
* @throws Exception
110115
*/
111116
public void verifyURLList(@NotNull JSONObject curObj, @NotNull String urlList) throws Exception {
112-
Object obj = curObj.get(urlList);
117+
Object obj = getArgVal(urlList);
113118
if (obj == null) {
114119
return;
115120
}
116-
121+
117122
if (obj instanceof Collection == false) {
118123
throw new IllegalArgumentException(urlList + " 不符合 Array 数组类型! 结构必须是 [] !");
119124
}
120-
125+
121126
Collection<?> collection = (Collection<?>) obj;
122127
if (collection != null) {
123128
int i = -1;
@@ -138,8 +143,8 @@ public void verifyURLList(@NotNull JSONObject curObj, @NotNull String urlList) t
138143
* @throws Exception
139144
*/
140145
public int deleteCommentOfMoment(@NotNull JSONObject curObj, @NotNull String momentId) throws Exception {
141-
long mid = curObj.getLongValue(momentId);
142-
if (mid <= 0 || curObj.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
146+
Long mid = getArgVal(momentId);
147+
if (mid == null || mid <= 0 || curObj.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
143148
return 0;
144149
}
145150

@@ -165,8 +170,8 @@ public int deleteCommentOfMoment(@NotNull JSONObject curObj, @NotNull String mom
165170
* @return
166171
*/
167172
public int deleteChildComment(@NotNull JSONObject curObj, @NotNull String toId) throws Exception {
168-
long tid = curObj.getLongValue(toId);
169-
if (tid <= 0 || curObj.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
173+
Long tid = getArgVal(toId);
174+
if (tid == null || tid <= 0 || curObj.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
170175
return 0;
171176
}
172177

@@ -243,9 +248,9 @@ public JSONArray getIdList(@NotNull JSONObject curObj) {
243248
* @throws Exception
244249
*/
245250
public Object verifyAccess(@NotNull JSONObject curObj) throws Exception {
246-
long userId = curObj.getLongValue(JSONRequest.KEY_USER_ID);
247-
String role = curObj.getString(JSONRequest.KEY_ROLE);
248-
if (AbstractVerifier.OWNER.equals(role) && userId != (Long) DemoVerifier.getVisitorId(getSession())) {
251+
String role = getArgVal(JSONRequest.KEY_ROLE);
252+
Long userId = getArgVal(JSONRequest.KEY_USER_ID);
253+
if (AbstractVerifier.OWNER.equals(role) && ! Objects.equals(userId, DemoVerifier.getVisitorId(getSession()))) {
249254
throw new IllegalAccessException("登录用户与角色OWNER不匹配!");
250255
}
251256
return null;

0 commit comments

Comments
 (0)