1414
1515package apijson .demo .server ;
1616
17+ import static zuo .biao .apijson .RequestMethod .DELETE ;
18+ import static zuo .biao .apijson .RequestMethod .GET ;
19+ import static zuo .biao .apijson .RequestMethod .HEAD ;
20+ import static zuo .biao .apijson .RequestMethod .POST ;
21+ import static zuo .biao .apijson .RequestMethod .POST_GET ;
22+ import static zuo .biao .apijson .RequestMethod .POST_HEAD ;
23+ import static zuo .biao .apijson .RequestMethod .PUT ;
24+
1725import java .util .ArrayList ;
1826import java .util .HashMap ;
1927import java .util .List ;
3240import apijson .demo .server .model .User ;
3341import apijson .demo .server .model .Verify ;
3442import zuo .biao .apijson .JSON ;
43+ import zuo .biao .apijson .JSONResponse ;
3544import zuo .biao .apijson .Log ;
3645import zuo .biao .apijson .MethodAccess ;
3746import zuo .biao .apijson .RequestMethod ;
3847import zuo .biao .apijson .RequestRole ;
3948import zuo .biao .apijson .model .Column ;
4049import zuo .biao .apijson .model .Table ;
4150import zuo .biao .apijson .model .Test ;
51+ import zuo .biao .apijson .server .JSONRequest ;
52+ import zuo .biao .apijson .server .Parser ;
53+ import zuo .biao .apijson .server .exception .ConflictException ;
4254import zuo .biao .apijson .server .exception .NotLoggedInException ;
4355import zuo .biao .apijson .server .sql .SQLConfig ;
4456
4557/**权限验证类
4658 * @author Lemon
4759 */
48- public class AccessVerifier {
49- private static final String TAG = "AccessVerifier " ;
60+ public class Verifier {
61+ private static final String TAG = "Verifier " ;
5062
5163
5264 public static final String KEY_PASSWORD = "password" ;
5365 public static final String KEY_LOGIN_PASSWORD = "loginPassword" ;
5466 public static final String KEY_PAY_PASSWORD = "payPassword" ;
67+ public static final String KEY_OLD_PASSWORD = "oldPassword" ;
5568
5669
5770 // <TableName, <METHOD, allowRoles>>
@@ -81,13 +94,13 @@ private static HashMap<RequestMethod, RequestRole[]> getAccessMap(MethodAccess a
8194 }
8295
8396 HashMap <RequestMethod , RequestRole []> map = new HashMap <>();
84- map .put (RequestMethod . GET , access .GET ());
85- map .put (RequestMethod . HEAD , access .HEAD ());
86- map .put (RequestMethod . POST_GET , access .POST_GET ());
87- map .put (RequestMethod . POST_HEAD , access .POST_HEAD ());
88- map .put (RequestMethod . POST , access .POST ());
89- map .put (RequestMethod . PUT , access .PUT ());
90- map .put (RequestMethod . DELETE , access .DELETE ());
97+ map .put (GET , access .GET ());
98+ map .put (HEAD , access .HEAD ());
99+ map .put (POST_GET , access .POST_GET ());
100+ map .put (POST_HEAD , access .POST_HEAD ());
101+ map .put (POST , access .POST ());
102+ map .put (PUT , access .PUT ());
103+ map .put (DELETE , access .DELETE ());
91104
92105 return map ;
93106 }
@@ -161,7 +174,7 @@ public static boolean verify(SQLConfig config, User visitor) throws Exception {
161174 }
162175 if (list .contains (new Long ("" + id )) == false ) {//Integer等转为Long才能正确判断。强转崩溃
163176 if (method == null ) {
164- method = RequestMethod . GET ;
177+ method = GET ;
165178 }
166179 throw new IllegalAccessException (userIdkey + " = " + id + " 的 " + table
167180 + " 不允许 " + role .name () + " 用户的 " + method .name () + " 请求!" );
@@ -197,7 +210,7 @@ public static void verifyRole(String table, RequestMethod method, RequestRole ro
197210 Log .d (TAG , "verifyRole table = " + table + "; method = " + method + "; role = " + role );
198211 if (table != null ) {
199212 if (method == null ) {
200- method = RequestMethod . GET ;
213+ method = GET ;
201214 }
202215 if (role == null ) {
203216 role = RequestRole .UNKNOWN ;
@@ -218,6 +231,7 @@ public static void verifyRole(String table, RequestMethod method, RequestRole ro
218231 * @throws Exception
219232 */
220233 public static void verifyLogin (HttpSession session ) throws Exception {
234+ Log .d (TAG , "verifyLogin session.getId() = " + (session == null ? null : session .getId ()));
221235 verifyLogin (getUserId (session ));
222236 }
223237 /**登录校验
@@ -231,6 +245,50 @@ public static void verifyLogin(Long userId) throws Exception {
231245 throw new NotLoggedInException ("未登录,请登录后再操作!" );
232246 }
233247 }
248+
249+
250+
251+ /**验证是否重复
252+ * @param table
253+ * @param key
254+ * @param value
255+ * @throws Exception
256+ */
257+ public static void verifyRepeat (String table , String key , Object value ) throws Exception {
258+ verifyRepeat (table , key , value , 0 );
259+ }
260+ /**验证是否重复
261+ * @param table
262+ * @param key
263+ * @param value
264+ * @param exceptId 不包含id
265+ * @throws Exception
266+ */
267+ public static void verifyRepeat (String table , String key , Object value , long exceptId ) throws Exception {
268+ if (key == null || value == null ) {
269+ Log .e (TAG , "verifyRepeat key == null || value == null >> return;" );
270+ return ;
271+ }
272+ if (value instanceof JSON ) {
273+ throw new UnsupportedDataTypeException (key + ":value 中value的类型不能为JSON!" );
274+ }
275+
276+ JSONRequest request = new JSONRequest (key , value );
277+ if (exceptId > 0 ) {//允许修改自己的属性为该属性原来的值
278+ request .put (JSONRequest .KEY_ID + "!" , exceptId );
279+ }
280+ JSONObject repeat = new Parser (HEAD , true ).parseResponse (
281+ new JSONRequest (table , request )
282+ );
283+ repeat = repeat == null ? null : repeat .getJSONObject (table );
284+ if (repeat == null ) {
285+ throw new Exception ("服务器内部错误 verifyRepeat repeat == null" );
286+ }
287+ if (repeat .getIntValue (JSONResponse .KEY_COUNT ) > 0 ) {
288+ throw new ConflictException (key + ": " + value + " 已经存在,不能重复!" );
289+ }
290+ }
291+
234292
235293 /**获取来访用户的id
236294 * @author Lemon
@@ -267,6 +325,7 @@ public static JSONObject removeAccessInfo(JSONObject requestObject) {
267325 requestObject .remove (KEY_PASSWORD );
268326 requestObject .remove (KEY_LOGIN_PASSWORD );
269327 requestObject .remove (KEY_PAY_PASSWORD );
328+ requestObject .remove (KEY_OLD_PASSWORD );
270329 }
271330 return requestObject ;
272331 }
0 commit comments