source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/Authentication.java@ 25124

Last change on this file since 25124 was 25124, checked in by sjm84, 12 years ago

Added a new service to get the details about a user

File size: 29.5 KB
Line 
1package org.greenstone.gsdl3.service;
2
3import org.greenstone.gsdl3.util.GSXML;
4import org.greenstone.gsdl3.util.DerbyWrapper;
5import org.greenstone.gsdl3.util.UserQueryResult;
6import org.greenstone.gsdl3.util.UserTermInfo;
7
8import org.w3c.dom.Element;
9import org.w3c.dom.NodeList;
10
11import java.util.HashMap;
12import java.util.Vector;
13import java.sql.SQLException;
14import java.util.regex.Pattern;
15import java.io.File;
16import java.io.UnsupportedEncodingException;
17
18public class Authentication extends ServiceRack
19{
20 //the services on offer
21 protected static final String AUTHENTICATION_SERVICE = "Authentication";
22 protected static final String GET_USER_INFORMATION_SERVICE = "GetUserInformation";
23
24 /** constructor */
25 public Authentication()
26 {
27 }
28
29 public boolean configure(Element info, Element extra_info)
30 {
31 logger.info("Configuring Authentication...");
32 this.config_info = info;
33
34 // set up Authentication service info - for now just has name and type
35 Element authentication_service = this.doc.createElement(GSXML.SERVICE_ELEM);
36 authentication_service.setAttribute(GSXML.TYPE_ATT, "authen");
37 authentication_service.setAttribute(GSXML.NAME_ATT, AUTHENTICATION_SERVICE);
38 this.short_service_info.appendChild(authentication_service);
39
40 // set up Authentication service info - for now just has name and type
41 Element getUserInformation_service = this.doc.createElement(GSXML.SERVICE_ELEM);
42 getUserInformation_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
43 getUserInformation_service.setAttribute(GSXML.NAME_ATT, GET_USER_INFORMATION_SERVICE);
44 this.short_service_info.appendChild(getUserInformation_service);
45
46 return true;
47 }
48
49 protected Element getServiceDescription(String service_id, String lang, String subset)
50 {
51
52 Element authen_service = this.doc.createElement(GSXML.SERVICE_ELEM);
53
54 if (service_id.equals(AUTHENTICATION_SERVICE))
55 {
56 authen_service.setAttribute(GSXML.TYPE_ATT, "authen");
57 authen_service.setAttribute(GSXML.NAME_ATT, AUTHENTICATION_SERVICE);
58 }
59 else if (service_id.equals(GET_USER_INFORMATION_SERVICE))
60 {
61 authen_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
62 authen_service.setAttribute(GSXML.NAME_ATT, GET_USER_INFORMATION_SERVICE);
63 }
64 else
65 {
66 return null;
67 }
68
69 if (service_id.equals(AUTHENTICATION_SERVICE) && (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER)))
70 {
71 authen_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(service_id, lang)));
72 authen_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(service_id, lang)));
73 }
74 return authen_service;
75 }
76
77 protected String getServiceName(String service_id, String lang)
78 {
79 return getTextString(service_id + ".name", lang);
80 }
81
82 protected String getServiceSubmit(String service_id, String lang)
83 {
84 return getTextString(service_id + ".submit", lang);
85 }
86
87 protected String getServiceDescription(String service_id, String lang)
88 {
89 return getTextString(service_id + ".description", lang);
90 }
91
92 protected void addCustomParams(String service, Element param_list, String lang)
93 {
94 }
95
96 protected void createParameter(String name, Element param_list, String lang)
97 {
98 }
99
100 protected Element processGetUserInformation(Element request)
101 {
102 // Create a new (empty) result message
103 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
104
105 result.setAttribute(GSXML.FROM_ATT, GET_USER_INFORMATION_SERVICE);
106 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
107
108 Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
109 if (paramList == null)
110 {
111 logger.error(GET_USER_INFORMATION_SERVICE + ": Param list does not exist ");
112 return null;
113 }
114
115 HashMap params = GSXML.extractParams(paramList, true);
116
117 String username = (String) params.get("username");
118
119 if (username == null)
120 {
121 logger.error(GET_USER_INFORMATION_SERVICE + ": No username specified");
122 return result;
123 }
124
125 DerbyWrapper dbWrapper = new DerbyWrapper();
126
127 String usersDB_dir = this.site_home + File.separatorChar + "etc" + File.separatorChar + "usersDB";
128 dbWrapper.connectDatabase(usersDB_dir, true);
129
130 UserQueryResult userQueryResult;
131 try
132 {
133 userQueryResult = dbWrapper.findUser(username);
134 Vector<UserTermInfo> terms = userQueryResult.getUserTerms();
135
136 if (terms.size() == 0)
137 {
138 logger.error(GET_USER_INFORMATION_SERVICE + ": Requested user was not found");
139 return result;
140 }
141
142 UserTermInfo userInfo = terms.get(0);
143 Element userInfoList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
144 result.appendChild(userInfoList);
145
146 Element usernameField = GSXML.createParameter(this.doc, "username", userInfo.username_);
147 Element passwordField = GSXML.createParameter(this.doc, "password", userInfo.password_);
148 Element groupsField = GSXML.createParameter(this.doc, "groups", userInfo.groups_);
149 Element accountStatusField = GSXML.createParameter(this.doc, "accountstatus", userInfo.accountstatus_);
150 Element commentField = GSXML.createParameter(this.doc, "comment", userInfo.comment_);
151
152 userInfoList.appendChild(usernameField);
153 userInfoList.appendChild(passwordField);
154 userInfoList.appendChild(groupsField);
155 userInfoList.appendChild(accountStatusField);
156 userInfoList.appendChild(commentField);
157 }
158 catch (Exception ex)
159 {
160 ex.printStackTrace();
161 }
162
163 return result;
164 }
165
166 protected Element processAuthentication(Element request) throws SQLException, UnsupportedEncodingException
167 {
168
169 // Create a new (empty) result message
170 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
171
172 result.setAttribute(GSXML.FROM_ATT, AUTHENTICATION_SERVICE);
173 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
174
175 String lang = request.getAttribute(GSXML.LANG_ATT);
176 // Get the parameters of the request
177 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
178
179 if (param_list == null)
180 {
181 logger.error("AddUsers request had no paramList.");
182 return result; // Return the empty result
183 }
184
185 String aup = null; //Actions: ListUsers, AddUser, ModifyPassword, DeleteUser, Login
186 String un = ""; //login user's name
187 String pw = ""; //login user's password
188 String asn = ""; //whether a user is authenticated
189 String uan = ""; //whether a authentication for a particular action is needed
190 String cm = ""; //whether the action is confirmed
191
192 String umun = ""; //the new user name
193 String umpw = ""; //user's new password
194 String umas = ""; //user account status
195 String umgp = ""; //user greoups
196 String umc = ""; // comments for the user
197
198 String oumun = ""; //the original user's name
199 String umpw1 = ""; //user's new password
200 String umpw2 = ""; //user's retyped new password
201
202 //used for adding a list of users at one time. Format: name,password,role]name,password,role]...
203 //in which, role may be in the format: student:[teacher's username]
204 String unpwlist = "";
205 String service = "";
206
207 // get parameters from the request
208 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
209 for (int i = 0; i < params.getLength(); i++)
210 {
211 Element param = (Element) params.item(i);
212 String p_name = param.getAttribute(GSXML.NAME_ATT);
213 String p_value = GSXML.getValue(param);
214
215 if (p_name.equals("aup"))
216 {
217 aup = p_value;
218 }
219 else if (p_name.equals("un"))
220 {
221 un = p_value;
222 }
223 else if (p_name.equals("pw"))
224 {
225 pw = p_value;
226 }
227 else if (p_name.equals("umun"))
228 {
229 umun = p_value;
230 }
231 else if (p_name.equals("umpw"))
232 {
233 umpw = p_value;
234 }
235 else if (p_name.equals("umas"))
236 {
237 umas = p_value;
238 }
239 else if (p_name.equals("umgp"))
240 {
241 umgp = p_value;
242 }
243 else if (p_name.equals("umc"))
244 {
245 umc = p_value;
246 }
247 else if (p_name.equals("asn"))
248 {
249 asn = p_value;
250 }
251 else if (p_name.equals("uan"))
252 {
253 uan = p_value;
254 }
255 else if (p_name.equals("cm"))
256 {
257 cm = p_value;
258 }
259 else if (p_name.equals("umpw1"))
260 {
261 umpw1 = p_value;
262 }
263 else if (p_name.equals("umpw2"))
264 {
265 umpw2 = p_value;
266 }
267 else if (p_name.equals("oumun"))
268 {
269 oumun = p_value;
270 }
271 else if (p_name.equals("unpwlist"))
272 {
273 unpwlist = p_value;
274 }
275
276 }
277
278 // create a Authentication node put into the result
279 Element authen_node = this.doc.createElement(GSXML.AUTHEN_NODE_ELEM);
280 result.appendChild(authen_node);
281 result.appendChild(getCollectList(this.site_home + File.separatorChar + "collect"));
282 // create a service node added into the Authentication node
283 Element service_node = this.doc.createElement(GSXML.SERVICE_ELEM);
284 authen_node.appendChild(service_node);
285 service_node.setAttribute("aup", aup);
286 // user's info
287 UserQueryResult userQueryResult = null;
288
289 // check the usersDb database, if it isn't existing, check the etc dir, create the etc dir if it isn't existing, then create the user database and add a "admin" user
290 String usersDB_dir = this.site_home + File.separatorChar + "etc" + File.separatorChar + "usersDB";
291 DerbyWrapper derbyWrapper = new DerbyWrapper();
292 File usersDB_file = new File(usersDB_dir);
293 if (!usersDB_file.exists())
294 {
295 String etc_dir = this.site_home + File.separatorChar + "etc";
296 File etc_file = new File(etc_dir);
297 if (!etc_file.exists())
298 {
299 boolean success = etc_file.mkdir();
300 if (!success)
301 {
302 logger.error("Couldn't create the etc dir under " + this.site_home + ".");
303 return result;
304 }
305 }
306 derbyWrapper.connectDatabase(usersDB_dir, true);
307 derbyWrapper.createDatabase();
308 }
309 else
310 {
311 derbyWrapper.connectDatabase(usersDB_dir, false);
312 }
313
314 // Action: login
315 if (aup.equals("Login"))
316 {
317 if (uan.equals(""))
318 { //return a login page, if the user's name is not given
319 service_node.setAttribute("info", "Login");
320 derbyWrapper.closeDatabase();
321 return result;
322 }
323 String groups = "";
324 // if the authentication(uan=1) is required,but the user hasn't been authenticated(asn=0),the user is asked to login first
325 if ((uan.equals("1") && asn.equals("0")))
326 {
327 if ((un.length() == 0) && (pw.length() == 0))
328 {
329 service_node.setAttribute("asn", "0");
330 service_node.setAttribute("info", "Login");
331 derbyWrapper.closeDatabase();
332 return result;
333 }
334 if ((un.length() == 0) || (pw.length() == 0))
335 {
336 service_node.setAttribute("asn", "0");
337 service_node.setAttribute("info", "Login");
338 service_node.setAttribute("err", "un-pw-err");
339 derbyWrapper.closeDatabase();
340 return result;
341 }
342 else
343 {
344 userQueryResult = derbyWrapper.findUser(un, pw);//looking for the user from the users table
345 service_node.setAttribute(GSXML.NAME_ATT, "Authentication");
346 service_node.setAttribute("un", un);
347 if (userQueryResult == null)
348 {
349 //the user isn't a vaild user
350 service_node.setAttribute("asn", "0");
351 service_node.setAttribute("err", "un-pw-err");// either unsername or password is wrong
352 service_node.setAttribute("info", "Login");
353 derbyWrapper.closeDatabase();
354 return result;
355 }
356 else
357 {
358 // asn="1"; //the user is a member of the "administrator" group
359 Vector userInfo = userQueryResult.users_;
360 groups = ((UserTermInfo) userInfo.get(0)).groups_;
361 String accountstatus = ((UserTermInfo) userInfo.get(0)).accountstatus_;
362 if (accountstatus.trim().equals("false"))
363 {
364 service_node.setAttribute("asn", "0");
365 service_node.setAttribute("err", "as-false");//the account status is false
366 service_node.setAttribute("info", "Login");
367 derbyWrapper.closeDatabase();
368 return result;
369 }
370 String[] groups_array = groups.split(",");
371 for (int i = 0; i < groups_array.length; i++)
372 {
373 if ((groups_array[i].trim().toLowerCase()).equals("administrator"))
374 {// check whether the user is in the administrator group
375 asn = "1";
376 service_node.setAttribute("asn", "1");
377 break;
378 }
379 }
380 if (!asn.equals("1"))
381 {
382 asn = "2";
383 service_node.setAttribute("asn", "2");//the user is authenticated
384 }
385 }
386 }
387 }
388
389 //asn!=0 This is a valid user
390 if (!asn.equals("0"))
391 {
392 service_node.setAttribute("info", "Login");
393 service_node.setAttribute("un", un);
394 service_node.setAttribute("pw", pw);
395 service_node.setAttribute("asn", asn);
396 service_node.setAttribute("umgp", groups);
397 derbyWrapper.closeDatabase();
398 return result;
399 }
400 }
401
402 //Action: listuser
403 if (aup.equals("ListUsers"))
404 {
405 if (asn.equals("") && un.equals(""))
406 {
407 service_node.setAttribute("info", "Login");
408 derbyWrapper.closeDatabase();
409 return result;
410 }
411
412 //valid users but not in the administrator group(asn=2), they cannot list all users
413 if (asn.equals("2"))
414 {
415 service_node.setAttribute("info", "Login");
416 service_node.setAttribute("err", "no-permission");
417 service_node.setAttribute("un", un);
418 service_node.setAttribute("asn", asn);
419 derbyWrapper.closeDatabase();
420 return result;
421 }
422 //valid users belong to the administrator group(asn=1), they can list all users
423 if (asn.equals("1"))
424 {
425 userQueryResult = derbyWrapper.findUser(null, null);
426 derbyWrapper.closeDatabase();
427 service_node.setAttribute(GSXML.NAME_ATT, "Authentication");
428 service_node.setAttribute("un", un);
429 service_node.setAttribute("asn", asn);
430
431 if (userQueryResult != null && userQueryResult.getSize() > 0)
432 {
433 service_node.setAttribute("info", "all-un"); // got a user list
434 Element user_node = getUserNode(userQueryResult);
435 service_node.appendChild(user_node);
436 derbyWrapper.closeDatabase();
437 return result;
438 }
439 else
440 {
441 service_node.setAttribute("err", "no-un"); // no user returned
442 derbyWrapper.closeDatabase();
443 return result;
444 }
445 }
446 }
447 //TODO: Action : addStudents (bulk adding)
448 if (aup.equals("AddStudents"))
449 {
450 String[] users = unpwlist.split("]");
451 for (int i = 0; i < users.length; i++)
452 {
453 String[] user = users[i].split(",");
454 String uname = user[0];
455 String password = user[1];
456 String group = user[2].split(":")[0];
457 String add_user = derbyWrapper.addUser(uname, password, group, "true", "");
458 if (add_user.equals("succeed"))
459 {
460 userQueryResult = derbyWrapper.findUser(null, null);
461 derbyWrapper.closeDatabase();
462 service_node.setAttribute("info", "all-un"); // return a list of all users if the user has been added
463 Element user_node = getUserNode(userQueryResult);
464 service_node.appendChild(user_node);
465 derbyWrapper.closeDatabase();
466 return result;
467 }
468 }
469 }
470
471 //Action : adduder
472 if (aup.equals("AddUser"))
473 {
474 if (asn.equals("") && un.equals(""))
475 {
476 service_node.setAttribute("info", "Login");
477 derbyWrapper.closeDatabase();
478 return result;
479 }
480 //valid users can't add a new user because they aren't in the administrator group(asn=2)
481 if (asn.equals("2"))
482 {
483 service_node.setAttribute("info", "Login");
484 service_node.setAttribute("err", "no-permission");
485 service_node.setAttribute("un", un);
486 service_node.setAttribute("asn", asn);
487 derbyWrapper.closeDatabase();
488 return result;
489 }
490 //valid users are in the administrator group, they can add a new user(asn=1)
491 if (asn.equals("1"))
492 {
493 service_node.setAttribute(GSXML.NAME_ATT, "Authentication");
494 service_node.setAttribute("un", un);
495 service_node.setAttribute("asn", asn);
496
497 if (umun.length() == 0 && umpw.length() == 0 && umgp.length() == 0 && umas.length() == 0 && umc.length() == 0)
498 {
499 service_node.setAttribute("info", "adduser_interface");
500 derbyWrapper.closeDatabase();
501 return result;
502 }
503
504 //check the strings of username and password
505 if ((umun == null) || (umun.length() < 2) || (umun.length() > 30) || (!(Pattern.matches("[a-zA-Z0-9//_//.]+", umun))))
506 {
507 service_node.setAttribute("err", "un-err"); //the input username string is illegal
508 service_node.setAttribute("info", "adduser_interface");
509 derbyWrapper.closeDatabase();
510 return result;
511 }
512
513 if ((umpw == null) || (umpw.length() < 3) || (umpw.length() > 8) || (!(Pattern.matches("[\\p{ASCII}]+", umpw))))
514 {
515 service_node.setAttribute("err", "pw-err"); //the input passwrod string is illegal
516 service_node.setAttribute("info", "adduser_interface");
517 derbyWrapper.closeDatabase();
518 return result;
519 }
520
521 // add the new users into the users table
522 umgp = umgp.replaceAll(" ", "");//get rid of the space of the groups string
523 userQueryResult = derbyWrapper.findUser(umun, null);// check whether the new user name has existed in the table.
524 if (userQueryResult != null)
525 {
526 service_node.setAttribute("err", "un-exist"); //the new username string is duplicated
527 service_node.setAttribute("info", "adduser_interface");
528 derbyWrapper.closeDatabase();
529 return result;
530 }
531 else
532 {
533 String add_user = derbyWrapper.addUser(umun, umpw, umgp, umas, umc);
534 if (add_user.equals("succeed"))
535 {
536 userQueryResult = derbyWrapper.findUser(null, null);
537 derbyWrapper.closeDatabase();
538 service_node.setAttribute("info", "all-un"); // return a list of all users if the user has been added
539 Element user_node = getUserNode(userQueryResult);
540 service_node.appendChild(user_node);
541 derbyWrapper.closeDatabase();
542 return result;
543 }
544 else
545 {
546 derbyWrapper.closeDatabase();
547 service_node.setAttribute("err", add_user);// return the error message if the user couldn't be added
548 derbyWrapper.closeDatabase();
549 return result;
550 }
551 }
552 }
553 }
554
555 //Action: edituser
556 if (aup.equals("EditUser"))
557 {
558 service_node.setAttribute(GSXML.NAME_ATT, "Authentication");
559 service_node.setAttribute("un", un);
560 service_node.setAttribute("asn", asn);
561
562 //Get the user's info from the database
563 if (cm.length() == 0)
564 {
565 service_node.setAttribute("info", "edituser-interface");
566 userQueryResult = derbyWrapper.findUser(umun, null);
567 derbyWrapper.closeDatabase();
568 Vector userInfo = userQueryResult.users_;
569 String username = ((UserTermInfo) userInfo.get(0)).username_;
570 String password = ((UserTermInfo) userInfo.get(0)).password_;
571 String groups = ((UserTermInfo) userInfo.get(0)).groups_;
572 String accountstatus = ((UserTermInfo) userInfo.get(0)).accountstatus_;
573 String comment = ((UserTermInfo) userInfo.get(0)).comment_;
574
575 service_node.setAttribute("oumun", oumun);
576 service_node.setAttribute("umun", username);
577 service_node.setAttribute("umpw", password);
578 service_node.setAttribute("umgp", groups);
579 service_node.setAttribute("umas", accountstatus);
580 service_node.setAttribute("umc", comment);
581 derbyWrapper.closeDatabase();
582 return result;
583 }
584
585 //Commit the modified user's info to the database
586 if (cm.toLowerCase().equals("submit"))
587 {
588 if (oumun.equals(umun))
589 {// the user's name hasn't been changed, update the user's info
590 if (umpw.length() == 0)
591 {
592 derbyWrapper.modifyUserInfo(umun, null, umgp, umas, umc);
593 userQueryResult = derbyWrapper.findUser(null, null);
594 derbyWrapper.closeDatabase();
595 service_node.setAttribute("info", "all-un"); // the user's info has been updated, return a list of all users
596 Element user_node = getUserNode(userQueryResult);
597 service_node.appendChild(user_node);
598 derbyWrapper.closeDatabase();
599 return result;
600 }
601 else
602 {
603 if ((umpw.length() == 0) || (umpw.length() < 3) || (umpw.length() > 8) || (!(Pattern.matches("[\\p{ASCII}]+", umpw))))
604 {
605 service_node.setAttribute("err", "umpw-err"); //the input passwrod string is illegal
606 service_node.setAttribute("info", "edituser-interface");
607 service_node.setAttribute("umun", umun);
608 service_node.setAttribute("umpw", umpw);
609 service_node.setAttribute("umgp", umgp);
610 service_node.setAttribute("umas", umas);
611 service_node.setAttribute("umc", umc);
612 service_node.setAttribute("oumun", oumun);
613 derbyWrapper.closeDatabase();
614 return result;
615 }
616 umgp = umgp.replaceAll(" ", "");// get rid of the space
617 derbyWrapper.modifyUserInfo(umun, umpw, umgp, umas, umc);
618 userQueryResult = derbyWrapper.listAllUser();
619 derbyWrapper.closeDatabase();
620 service_node.setAttribute("info", "all-un"); // if the new user has been added successfully, return a list of all users
621 Element user_node = getUserNode(userQueryResult);
622 service_node.appendChild(user_node);
623 derbyWrapper.closeDatabase();
624 return result;
625 }
626 }
627 // The user's name has been changed, add a new user record to the database
628 else
629 {
630 if ((umun.length() == 0) || (umun.length() < 2) || (umun.length() > 30) || (!(Pattern.matches("[a-zA-Z0-9//_//.]+", umun))))
631 {
632 service_node.setAttribute("err", "umun-err"); //the input username string is illegal
633 service_node.setAttribute("umun", umun);
634 service_node.setAttribute("umpw", umpw);
635 service_node.setAttribute("umgp", umgp);
636 service_node.setAttribute("umas", umas);
637 service_node.setAttribute("umc", umc);
638 service_node.setAttribute("oumun", oumun);
639 service_node.setAttribute("info", "edituser-interface");
640 derbyWrapper.closeDatabase();
641 return result;
642 }
643 if (umpw.length() == 0)
644 {
645 service_node.setAttribute("err", "ini-umpw-err"); //the input passwrod string is illegal
646 service_node.setAttribute("info", "edituser-interface");
647 service_node.setAttribute("umun", umun);
648 service_node.setAttribute("umpw", umpw);
649 service_node.setAttribute("umgp", umgp);
650 service_node.setAttribute("umas", umas);
651 service_node.setAttribute("umc", umc);
652 service_node.setAttribute("oumun", oumun);
653 derbyWrapper.closeDatabase();
654 return result;
655 }
656 if ((umpw.length() < 3) || (umpw.length() > 8) || (!(Pattern.matches("[\\p{ASCII}]+", umpw))))
657 {
658 service_node.setAttribute("err", "umpw-err"); //the input passwrod string is illegal
659 service_node.setAttribute("info", "edituser-interface");
660 service_node.setAttribute("umun", umun);
661 service_node.setAttribute("umpw", umpw);
662 service_node.setAttribute("umgp", umgp);
663 service_node.setAttribute("umas", umas);
664 service_node.setAttribute("umc", umc);
665 service_node.setAttribute("oumun", oumun);
666 derbyWrapper.closeDatabase();
667 return result;
668 }
669 umgp = umgp.replaceAll(" ", "");// get rid of the space
670 userQueryResult = derbyWrapper.findUser(umun, null);// check whether the new user name has existed in the table.
671 if (userQueryResult != null)
672 {
673 service_node.setAttribute("err", "un-exist"); //the new username string is duplicated
674 service_node.setAttribute("info", "edituser-interface");
675 service_node.setAttribute("umun", "");
676 service_node.setAttribute("umpw", "");
677 service_node.setAttribute("umgp", umgp);
678 service_node.setAttribute("umas", umas);
679 service_node.setAttribute("umc", umc);
680 service_node.setAttribute("oumun", oumun);
681 derbyWrapper.closeDatabase();
682 return result;
683 }
684 else
685 {
686 derbyWrapper.addUser(umun, umpw, umgp, umas, umc);
687 userQueryResult = derbyWrapper.listAllUser();
688 derbyWrapper.closeDatabase();
689 service_node.setAttribute("info", "all-un"); // if the new user has been added successfully, return a list of all users
690 Element user_node = getUserNode(userQueryResult);
691 service_node.appendChild(user_node);
692 derbyWrapper.closeDatabase();
693 return result;
694 }
695 }
696 }
697
698 if (cm.toLowerCase().equals("cancel"))
699 {
700 userQueryResult = derbyWrapper.listAllUser();
701 derbyWrapper.closeDatabase();
702 service_node.setAttribute("info", "all-un"); // if the new user has been added successfully, return a list of all users
703 Element user_node = getUserNode(userQueryResult);
704 service_node.appendChild(user_node);
705 derbyWrapper.closeDatabase();
706 return result;
707 }
708 }
709
710 //Action: modifypassword
711 if (aup.equals("ModifyPassword"))
712 {
713 if (un.equals(""))
714 {
715 service_node.setAttribute("info", "Login");
716 derbyWrapper.closeDatabase();
717 return result;
718 }
719
720 service_node.setAttribute(GSXML.NAME_ATT, "Authentication");
721 service_node.setAttribute("un", un);
722 service_node.setAttribute("asn", asn);
723
724 userQueryResult = derbyWrapper.findUser(un, null);
725 Vector userInfo = userQueryResult.users_;
726 pw = ((UserTermInfo) userInfo.get(0)).password_;
727
728 if ((umpw1.length() == 0) && (umpw2.length() == 0) && (umpw.length() == 0))
729 {
730 service_node.setAttribute("info", "modify_interface");// call the interface of the modifying password
731 derbyWrapper.closeDatabase();
732 return result;
733 }
734
735 if (!pw.equals(umpw) && umpw.length() > 0)
736 {
737 service_node.setAttribute("info", "modify_interface");
738 service_node.setAttribute("err", "pw-umpw-nm-err");//if the original password is not match
739 derbyWrapper.closeDatabase();
740 return result;
741 }
742
743 if ((umpw1.length() == 0) || (umpw2.length() == 0))
744 {
745 service_node.setAttribute("info", "modify_interface");
746 service_node.setAttribute("err", "umpw1-umpw2-null-err");//if one of the password strings is none,return the err info back
747 derbyWrapper.closeDatabase();
748 return result;
749 }
750
751 if (!umpw1.equals(umpw2))
752 {
753 service_node.setAttribute("info", "modify_interface");
754 service_node.setAttribute("err", "umpw1-umpw2-nm-err");//if one of the password strings is none,return the err info back
755 derbyWrapper.closeDatabase();
756 return result;
757 }
758
759 if (umpw.length() == 0)
760 {
761 service_node.setAttribute("info", "modify_interface");
762 service_node.setAttribute("err", "umpw-null-err");//if one of the password strings is none,return the err info back
763 derbyWrapper.closeDatabase();
764 return result;
765 }
766 //check the new password and the retyped password
767 if ((umpw1 == null) || (umpw1.length() < 3) || (umpw1.length() > 8) || (!(Pattern.matches("[\\p{ASCII}]+", umpw1))))
768 {
769 service_node.setAttribute("info", "modify_interface");
770 service_node.setAttribute("err", "umpw1-err");// the new password is illegal
771 derbyWrapper.closeDatabase();
772 return result;
773 }
774
775 if ((umpw2 == null) || (umpw2.length() < 3) || (umpw2.length() > 8) || (!(Pattern.matches("[\\p{ASCII}]+", umpw2))))
776 {
777 service_node.setAttribute("info", "modify_interface");
778 service_node.setAttribute("err", "umpw2-err"); // the retyped password is illegal
779 derbyWrapper.closeDatabase();
780 return result;
781 }
782 String modify_user_info = derbyWrapper.modifyUserInfo(un, umpw1, null, null, null);
783 if (modify_user_info.equals("succeed"))
784 {
785 service_node.setAttribute("err", "");// the passsword has been changed successfully
786 derbyWrapper.closeDatabase();
787 return result;
788 }
789 else
790 {
791 service_node.setAttribute("err", modify_user_info);// return the error message of the pasword couldn't be modified
792 derbyWrapper.closeDatabase();
793 return result;
794 }
795 }
796
797 //Action: deleteuser
798 if (aup.equals("DeleteUser"))
799 {
800 service_node.setAttribute("un", un);
801 service_node.setAttribute("asn", asn);
802 service_node.setAttribute("umun", umun);
803 if (cm.equals("yes"))
804 {
805 String delete_user = derbyWrapper.deleteUser(umun);
806 if (delete_user.equals("succeed"))
807 {
808 service_node.setAttribute("err", "");
809 userQueryResult = derbyWrapper.listAllUser();
810 service_node.setAttribute("info", "all-un"); // return a list of all users
811 Element user_node = getUserNode(userQueryResult);
812 service_node.appendChild(user_node);
813 }
814 else
815 {
816 service_node.setAttribute("err", delete_user);//return the error message
817 derbyWrapper.closeDatabase();
818 return result;
819 }
820 }
821 else if (cm.equals("no"))
822 {
823 service_node.setAttribute("err", "");
824 userQueryResult = derbyWrapper.listAllUser();
825 service_node.setAttribute("info", "all-un"); // return a list of all users
826 Element user_node = getUserNode(userQueryResult);
827 service_node.appendChild(user_node);
828 derbyWrapper.closeDatabase();
829 return result;
830 }
831 else
832 {
833 service_node.setAttribute("info", "confirm");
834 derbyWrapper.closeDatabase();
835 return result;
836 }
837 }
838
839 return result;
840 }
841
842 private Element getUserNode(UserQueryResult userQueryResult)
843 {
844 Element user_list_node = this.doc.createElement(GSXML.USER_NODE_ELEM + "List");
845
846 Vector userInfo = userQueryResult.users_;
847
848 for (int i = 0; i < userQueryResult.getSize(); i++)
849 {
850 Element user_node = this.doc.createElement(GSXML.USER_NODE_ELEM);
851 String username = ((UserTermInfo) userInfo.get(i)).username_;
852 String password = ((UserTermInfo) userInfo.get(i)).password_;
853 String groups = ((UserTermInfo) userInfo.get(i)).groups_;
854 String accountstatus = ((UserTermInfo) userInfo.get(i)).accountstatus_;
855 String comment = ((UserTermInfo) userInfo.get(i)).comment_;
856 user_node.setAttribute("umun", username);
857 user_node.setAttribute("umpw", password);
858 user_node.setAttribute("umgp", groups);
859 user_node.setAttribute("umas", accountstatus);
860 user_node.setAttribute("umc", comment);
861
862 user_list_node.appendChild(user_node);
863 }
864 return user_list_node;
865 }
866
867 private Element getCollectList(String collect)
868 {
869 Element collect_list_node = this.doc.createElement(GSXML.COLLECTION_ELEM + "List");
870 File[] collect_dir = (new File(collect)).listFiles();
871 if (collect_dir != null && collect_dir.length > 0)
872 {
873 for (int i = 0; i < collect_dir.length; i++)
874 {
875 if (collect_dir[i].isDirectory() && (!collect_dir[i].getName().startsWith(".svn")))
876 {
877 Element collect_node = this.doc.createElement(GSXML.COLLECTION_ELEM);
878 collect_node.setAttribute("name", collect_dir[i].getName());
879 collect_list_node.appendChild(collect_node);
880 }
881 }
882 }
883 return collect_list_node;
884 }
885}
Note: See TracBrowser for help on using the repository browser.