Ignore:
Timestamp:
2008-10-02T12:33:02+13:00 (16 years ago)
Author:
shaoqun
Message:

few changes to suit ActivityManagement: send back group info of users and addStudents etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/service/Authentication.java

    r14402 r17452  
    1111import java.util.Vector;
    1212import java.sql.SQLException;
    13 import org.apache.log4j.*;
    14 
    1513import java.util.regex.Pattern;
    1614import java.io.File;
     
    1816
    1917public class Authentication
    20     extends ServiceRack {
    21     //the services on offer
    22     protected static final String AUTHENTICATION_SERVICE="Authentication";
    23 
    24     /** constructor */
    25     public Authentication()
    26     { }
    27 
    28     public boolean configure(Element info, Element extra_info)
    29     {
    30     logger.info("Configuring Authentication...");
    31     this.config_info = info;
    32 
    33     // set up Authentication service info - for now just has name and type
    34     Element authentication_service= this.doc.createElement(GSXML.SERVICE_ELEM);
    35     authentication_service.setAttribute(GSXML.TYPE_ATT, "authen");
    36     authentication_service.setAttribute(GSXML.NAME_ATT, AUTHENTICATION_SERVICE);
    37     this.short_service_info.appendChild(authentication_service);
    38 
    39     return true;
    40     }
    41 
    42     protected Element getServiceDescription(String service_id, String lang, String subset)
    43     {
    44    
    45     Element authen_service=this.doc.createElement(GSXML.SERVICE_ELEM);
    46    
    47     if (service_id.equals(AUTHENTICATION_SERVICE)) {
    48         authen_service.setAttribute(GSXML.TYPE_ATT,"authen");
    49         authen_service.setAttribute(GSXML.NAME_ATT, AUTHENTICATION_SERVICE);
    50     } else {
    51         return null;
    52     }
    53    
    54     if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
    55         authen_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(service_id, lang) ));
    56         authen_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(service_id, lang)));
    57     }
    58     return authen_service; 
    59     }
    60    
    61     protected String getServiceName(String service_id, String lang) {
    62     return getTextString(service_id+".name", lang);
    63     }
    64 
    65     protected String getServiceSubmit(String service_id, String lang) {
    66     return getTextString(service_id+".submit", lang);
    67     }
    68 
    69     protected String getServiceDescription(String service_id, String lang) {
    70     return getTextString(service_id+".description", lang);
    71     }
    72 
    73     protected void addCustomParams(String service, Element param_list, String lang) {
    74     }
    75 
    76     protected void createParameter(String name, Element param_list, String lang) {
    77     }
    78  
    79     protected Element processAuthentication(Element request) throws SQLException, UnsupportedEncodingException{
    80 
    81     // Create a new (empty) result message
    82     Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
    83    
    84     result.setAttribute(GSXML.FROM_ATT, AUTHENTICATION_SERVICE);
    85     result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    86 
    87     String lang = request.getAttribute(GSXML.LANG_ATT);
    88     // Get the parameters of the request
    89     Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    90 
    91     if (param_list == null) {
    92         logger.error("AddUsers request had no paramList.");
    93         return result;  // Return the empty result
    94     }
    95      
    96     String aup=null; //Actions: ListUsers, AddUser, ModifyPassword, DeleteUser, Login
    97     String un=""; //login user's name
    98     String pw=""; //login user's password
    99     String asn=""; //whether a user is authenticated
    100     String uan=""; //whether a authentication for a particular action is needed
    101     String cm=""; //whether the action is confirmed
    102 
    103     String umun=""; //the new user name
    104     String umpw=""; //user's new password
    105     String umas=""; //user account status
    106     String umgp=""; //user greoups
    107     String umc=""; // comments for the user
    108 
    109     String oumun=""; //the original user's name
    110     String umpw1=""; //user's new password
    111     String umpw2=""; //user's retyped new password
    112 
    113     // get parameters from the request
    114     NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
    115     for (int i=0; i<params.getLength();i++) {
    116         Element param = (Element)params.item(i);
    117         String p_name = param.getAttribute(GSXML.NAME_ATT);
    118         String p_value = GSXML.getValue(param);
    119 
    120         if (p_name.equals("aup")){
    121         aup = p_value;
    122         }else if (p_name.equals("un")) {
    123         un = p_value;
    124         }else if(p_name.equals("pw")) {
    125         pw = p_value;
    126         }else if(p_name.equals("umun")) {
    127         umun = p_value;
    128         }else if(p_name.equals("umpw")) {
    129         umpw = p_value;
    130         }else if (p_name.equals("umas")){
    131         umas = p_value;
    132         }else if (p_name.equals("umgp")){
    133         umgp = p_value;
    134         }else if (p_name.equals("umc")){
    135         umc = p_value;
    136         }else if (p_name.equals("asn")){
    137         asn = p_value;
    138         }else if (p_name.equals("uan")){
    139         uan = p_value;
    140         }else if (p_name.equals("cm")){
    141         cm = p_value;
    142         }else if(p_name.equals("umpw1")) {
    143         umpw1 = p_value;
    144         }else if(p_name.equals("umpw2")) {
    145         umpw2 = p_value;
    146         }else if(p_name.equals("oumun")) {
    147         oumun = p_value;
    148         }
    149        
    150     }
    151    
    152     // create a Authentication node put into the result
    153     Element authen_node = this.doc.createElement(GSXML.AUTHEN_NODE_ELEM);
    154     result.appendChild(authen_node);
    155     result.appendChild(getCollectList(this.site_home + File.separatorChar + "collect"));
    156     // create a service node added into the Authentication node
    157     Element service_node = this.doc.createElement(GSXML.SERVICE_ELEM);
    158     authen_node.appendChild(service_node);
    159     service_node.setAttribute("aup",aup);
    160     // user's info
    161     UserQueryResult userQueryResult=null;
    162 
    163     // 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
    164     String usersDB_dir = this.site_home + File.separatorChar + "etc" + File.separatorChar + "usersDB";
    165     DerbyWrapper derbyWrapper=new DerbyWrapper();
    166     File usersDB_file =new File(usersDB_dir);
    167     if (!usersDB_file.exists()){
    168         String etc_dir = this.site_home + File.separatorChar + "etc";
    169         File etc_file =new File(etc_dir);
    170         if (!etc_file.exists()){
    171         boolean success = etc_file.mkdir();
    172         if (!success){
    173             logger.error("Couldn't create the etc dir under "+this.site_home + ".");
    174             return result;
    175         }
    176         }
    177         derbyWrapper.connectDatabase(usersDB_dir,true);
    178         derbyWrapper.createDatabase();
    179     }else{
    180         derbyWrapper.connectDatabase(usersDB_dir, false);
    181     }
    182 
    183     // Action: login
    184     if (aup.equals("Login")){
    185         if (uan.equals("")){ //return a login page, if the user's name is not given
    186         service_node.setAttribute("info","Login");
    187         derbyWrapper.closeDatabase();
     18extends ServiceRack {
     19    //the services on offer
     20    protected static final String AUTHENTICATION_SERVICE="Authentication";
     21
     22    /** constructor */
     23    public Authentication()
     24    { }
     25
     26    public boolean configure(Element info, Element extra_info)
     27    {
     28        logger.info("Configuring Authentication...");
     29        this.config_info = info;
     30
     31        // set up Authentication service info - for now just has name and type
     32        Element authentication_service= this.doc.createElement(GSXML.SERVICE_ELEM);
     33        authentication_service.setAttribute(GSXML.TYPE_ATT, "authen");
     34        authentication_service.setAttribute(GSXML.NAME_ATT, AUTHENTICATION_SERVICE);
     35        this.short_service_info.appendChild(authentication_service);
     36
     37        return true;
     38    }
     39
     40    protected Element getServiceDescription(String service_id, String lang, String subset)
     41    {
     42
     43        Element authen_service=this.doc.createElement(GSXML.SERVICE_ELEM);
     44
     45        if (service_id.equals(AUTHENTICATION_SERVICE)) {
     46            authen_service.setAttribute(GSXML.TYPE_ATT,"authen");
     47            authen_service.setAttribute(GSXML.NAME_ATT, AUTHENTICATION_SERVICE);
     48        } else {
     49            return null;
     50        }
     51
     52        if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
     53            authen_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(service_id, lang) ));
     54            authen_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(service_id, lang)));
     55        }
     56        return authen_service; 
     57    }
     58
     59    protected String getServiceName(String service_id, String lang) {
     60        return getTextString(service_id+".name", lang);
     61    }
     62
     63    protected String getServiceSubmit(String service_id, String lang) {
     64        return getTextString(service_id+".submit", lang);
     65    }
     66
     67    protected String getServiceDescription(String service_id, String lang) {
     68        return getTextString(service_id+".description", lang);
     69    }
     70
     71    protected void addCustomParams(String service, Element param_list, String lang) {
     72    }
     73
     74    protected void createParameter(String name, Element param_list, String lang) {
     75    }
     76
     77    protected Element processAuthentication(Element request) throws SQLException, UnsupportedEncodingException{
     78
     79        // Create a new (empty) result message
     80        Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     81
     82        result.setAttribute(GSXML.FROM_ATT, AUTHENTICATION_SERVICE);
     83        result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
     84
     85        String lang = request.getAttribute(GSXML.LANG_ATT);
     86        // Get the parameters of the request
     87        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     88
     89        if (param_list == null) {
     90            logger.error("AddUsers request had no paramList.");
     91            return result;  // Return the empty result
     92        }
     93
     94        String aup=null; //Actions: ListUsers, AddUser, ModifyPassword, DeleteUser, Login
     95        String un=""; //login user's name
     96        String pw=""; //login user's password
     97        String asn=""; //whether a user is authenticated
     98        String uan=""; //whether a authentication for a particular action is needed
     99        String cm=""; //whether the action is confirmed
     100
     101        String umun=""; //the new user name
     102        String umpw=""; //user's new password
     103        String umas=""; //user account status
     104        String umgp=""; //user greoups
     105        String umc=""; // comments for the user
     106
     107        String oumun=""; //the original user's name
     108        String umpw1=""; //user's new password
     109        String umpw2=""; //user's retyped new password
     110
     111        //used for adding a list of users at one time. Format: name,password,role]name,password,role]...
     112        //in which, role may be in the format: student:[teacher's username]
     113        String unpwlist="";
     114        String service = "";
     115       
     116        // get parameters from the request
     117        NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
     118        for (int i=0; i<params.getLength();i++) {
     119            Element param = (Element)params.item(i);
     120            String p_name = param.getAttribute(GSXML.NAME_ATT);
     121            String p_value = GSXML.getValue(param);
     122
     123            if (p_name.equals("aup")){
     124                aup = p_value;
     125            }else if (p_name.equals("un")) {
     126                un = p_value;
     127            }else if(p_name.equals("pw")) {
     128                pw = p_value;
     129            }else if(p_name.equals("umun")) {
     130                umun = p_value;
     131            }else if(p_name.equals("umpw")) {
     132                umpw = p_value;
     133            }else if (p_name.equals("umas")){
     134                umas = p_value;
     135            }else if (p_name.equals("umgp")){
     136                umgp = p_value;
     137            }else if (p_name.equals("umc")){
     138                umc = p_value;
     139            }else if (p_name.equals("asn")){
     140                asn = p_value;
     141            }else if (p_name.equals("uan")){
     142                uan = p_value;
     143            }else if (p_name.equals("cm")){
     144                cm = p_value;
     145            }else if(p_name.equals("umpw1")) {
     146                umpw1 = p_value;
     147            }else if(p_name.equals("umpw2")) {
     148                umpw2 = p_value;
     149            }else if(p_name.equals("oumun")) {
     150                oumun = p_value;
     151            }else if(p_name.equals("unpwlist")) {
     152                unpwlist = p_value;
     153            }
     154
     155        }
     156
     157        // create a Authentication node put into the result
     158        Element authen_node = this.doc.createElement(GSXML.AUTHEN_NODE_ELEM);
     159        result.appendChild(authen_node);
     160        result.appendChild(getCollectList(this.site_home + File.separatorChar + "collect"));
     161        // create a service node added into the Authentication node
     162        Element service_node = this.doc.createElement(GSXML.SERVICE_ELEM);
     163        authen_node.appendChild(service_node);
     164        service_node.setAttribute("aup",aup);
     165        // user's info
     166        UserQueryResult userQueryResult=null;
     167
     168        // 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
     169        String usersDB_dir = this.site_home + File.separatorChar + "etc" + File.separatorChar + "usersDB";
     170        DerbyWrapper derbyWrapper=new DerbyWrapper();
     171        File usersDB_file =new File(usersDB_dir);
     172        if (!usersDB_file.exists()){
     173            String etc_dir = this.site_home + File.separatorChar + "etc";
     174            File etc_file =new File(etc_dir);
     175            if (!etc_file.exists()){
     176                boolean success = etc_file.mkdir();
     177                if (!success){
     178                    logger.error("Couldn't create the etc dir under "+this.site_home + ".");
     179                    return result;
     180                }
     181            }
     182            derbyWrapper.connectDatabase(usersDB_dir,true);
     183            derbyWrapper.createDatabase();
     184        }else{
     185            derbyWrapper.connectDatabase(usersDB_dir, false);
     186        }
     187
     188        // Action: login
     189        if (aup.equals("Login")){
     190            if (uan.equals("")){ //return a login page, if the user's name is not given
     191                service_node.setAttribute("info","Login");
     192                derbyWrapper.closeDatabase();
     193                return result;
     194            }
     195            String groups = "";
     196            // if the authentication(uan=1) is required,but the user hasn't been authenticated(asn=0),the user is asked to login first
     197            if ((uan.equals("1") && asn.equals("0"))) {
     198                if ((un.length()==0) && (pw.length()==0)){
     199                    service_node.setAttribute("asn","0");
     200                    service_node.setAttribute("info","Login");
     201                    derbyWrapper.closeDatabase();
     202                    return result;
     203                }
     204                if ((un.length()==0) || (pw.length()==0)){
     205                    service_node.setAttribute("asn","0");
     206                    service_node.setAttribute("info","Login");
     207                    service_node.setAttribute("err","un-pw-err");
     208                    derbyWrapper.closeDatabase();
     209                    return result;
     210                }else{ 
     211                    userQueryResult=derbyWrapper.findUser(un,pw);//looking for the user from the users table
     212                    service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
     213                    service_node.setAttribute("un",un);
     214                    if (userQueryResult==null){
     215                        //the user isn't a vaild user
     216                        service_node.setAttribute("asn","0");
     217                        service_node.setAttribute("err","un-pw-err");// either unsername or password is wrong
     218                        service_node.setAttribute("info","Login");
     219                        derbyWrapper.closeDatabase();
     220                        return result;
     221                    }else{
     222                        // asn="1"; //the user is a member of the "administrator" group
     223                        Vector userInfo=userQueryResult.users_;
     224                        groups=((UserTermInfo)userInfo.get(0)).groups_;
     225                        String accountstatus=((UserTermInfo)userInfo.get(0)).accountstatus_;
     226                        if (accountstatus.trim().equals("false")){
     227                            service_node.setAttribute("asn","0");
     228                            service_node.setAttribute("err","as-false");//the account status is false
     229                            service_node.setAttribute("info","Login");
     230                            derbyWrapper.closeDatabase();
     231                            return result;
     232                        }
     233                        String[] groups_array=groups.split(",");
     234                        for (int i=0; i<groups_array.length;i++){
     235                            if ((groups_array[i].trim().toLowerCase()).equals("administrator")){// check whether the user is in the administrator group
     236                                asn="1";
     237                                service_node.setAttribute("asn","1");
     238                                break;
     239                            }
     240                        }
     241                        if (!asn.equals("1")){
     242                            asn="2";
     243                            service_node.setAttribute("asn","2");//the user is authenticated                           
     244                        }
     245                    }
     246                }
     247            }
     248
     249            //asn!=0 This is a valid user
     250            if (!asn.equals("0")){
     251                service_node.setAttribute("info","Login");
     252                service_node.setAttribute("un",un);
     253                service_node.setAttribute("pw",pw);
     254                service_node.setAttribute("asn",asn);
     255                service_node.setAttribute("umgp",groups);
     256                derbyWrapper.closeDatabase();
     257                return result;
     258            }
     259        }
     260
     261        //Action: listuser
     262        if (aup.equals("ListUsers")){
     263            if (asn.equals("") && un.equals("")){
     264                service_node.setAttribute("info","Login");
     265                derbyWrapper.closeDatabase();
     266                return result;
     267            }
     268
     269            //valid users but not in the administrator group(asn=2), they cannot list all users
     270            if (asn.equals("2")){
     271                service_node.setAttribute("info","Login");
     272                service_node.setAttribute("err","no-permission");
     273                service_node.setAttribute("un",un);
     274                service_node.setAttribute("asn",asn);
     275                derbyWrapper.closeDatabase();
     276                return result;
     277            }
     278            //valid users belong to the administrator group(asn=1), they can list all users
     279            if (asn.equals("1")){
     280                userQueryResult=derbyWrapper.findUser(null,null);
     281                derbyWrapper.closeDatabase();
     282                service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
     283                service_node.setAttribute("un",un);
     284                service_node.setAttribute("asn",asn);
     285
     286                if (userQueryResult!=null && userQueryResult.getSize()>0){
     287                    service_node.setAttribute("info","all-un"); // got a user list
     288                    Element user_node=getUserNode(userQueryResult);
     289                    service_node.appendChild(user_node);
     290                    derbyWrapper.closeDatabase();
     291                    return result;
     292                }else {
     293                    service_node.setAttribute("err","no-un"); // no user returned
     294                    derbyWrapper.closeDatabase();
     295                    return result;
     296                }   
     297            }
     298        }
     299        //TODO: Action : addStudents (bulk adding)
     300        if (aup.equals("AddStudents")){
     301            String[] users = unpwlist.split("]");
     302            for(int i=0; i<users.length; i++) {
     303                String[] user = users[i].split(",");
     304                String uname = user[0];
     305                String password = user[1];
     306                String group = user[2].split(":")[0];
     307                String add_user=derbyWrapper.addUser(uname, password, group,"true","");
     308                if (add_user.equals("succeed")){
     309                    userQueryResult=derbyWrapper.findUser(null,null);
     310                    derbyWrapper.closeDatabase();
     311                    service_node.setAttribute("info","all-un"); // return a list of all users if the user has been added
     312                    Element user_node=getUserNode(userQueryResult);
     313                    service_node.appendChild(user_node);
     314                    derbyWrapper.closeDatabase();
     315                    return result;
     316                }           
     317            }
     318        }
     319       
     320        //Action : adduder
     321        if (aup.equals("AddUser")){
     322            if (asn.equals("") && un.equals("")){
     323                service_node.setAttribute("info","Login");
     324                derbyWrapper.closeDatabase();
     325                return result;
     326            }
     327            //valid users can't add a new user because they aren't in the administrator group(asn=2)
     328            if (asn.equals("2")){
     329                service_node.setAttribute("info","Login");
     330                service_node.setAttribute("err","no-permission");
     331                service_node.setAttribute("un",un);
     332                service_node.setAttribute("asn",asn);
     333                derbyWrapper.closeDatabase();
     334                return result;
     335            }
     336            //valid users are in the administrator group, they can add a new user(asn=1)
     337            if (asn.equals("1")){
     338                service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
     339                service_node.setAttribute("un",un);
     340                service_node.setAttribute("asn",asn);
     341
     342                if (umun.length()==0 && umpw.length()==0 && umgp.length()==0 && umas.length()==0 && umc.length()==0){
     343                    service_node.setAttribute("info","adduser_interface");
     344                    derbyWrapper.closeDatabase();
     345                    return result;
     346                }
     347
     348                //check the strings of username and password
     349                if ((umun==null) || (umun.length()<2) || (umun.length()>30) || (!(Pattern.matches("[a-zA-Z0-9//_//.]+",umun)))){
     350                    service_node.setAttribute("err","un-err"); //the input username string is illegal
     351                    service_node.setAttribute("info","adduser_interface");
     352                    derbyWrapper.closeDatabase();
     353                    return result;
     354                }
     355
     356                if ((umpw==null) || (umpw.length()<3) || (umpw.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw)))){
     357                    service_node.setAttribute("err","pw-err"); //the input passwrod string is illegal
     358                    service_node.setAttribute("info","adduser_interface");
     359                    derbyWrapper.closeDatabase();
     360                    return result;
     361                }
     362
     363                // add the new users into the users table
     364                umgp=umgp.replaceAll(" ","");//get rid of the space of the groups string
     365                userQueryResult=derbyWrapper.findUser(umun,null);// check whether the new user name has existed in the table.
     366                if (userQueryResult!=null){
     367                    service_node.setAttribute("err","un-exist"); //the new username string is duplicated
     368                    service_node.setAttribute("info","adduser_interface");
     369                    derbyWrapper.closeDatabase();
     370                    return result;
     371                }else{
     372                    String add_user=derbyWrapper.addUser(umun,umpw,umgp,umas,umc);
     373                    if (add_user.equals("succeed")){
     374                        userQueryResult=derbyWrapper.findUser(null,null);
     375                        derbyWrapper.closeDatabase();
     376                        service_node.setAttribute("info","all-un"); // return a list of all users if the user has been added
     377                        Element user_node=getUserNode(userQueryResult);
     378                        service_node.appendChild(user_node);
     379                        derbyWrapper.closeDatabase();
     380                        return result;
     381                    }else{
     382                        derbyWrapper.closeDatabase();
     383                        service_node.setAttribute("err",add_user);// return the error message if the user couldn't be added
     384                        derbyWrapper.closeDatabase();
     385                        return result;
     386                    }
     387                }
     388            }
     389        }
     390
     391        //Action: edituser
     392        if (aup.equals("EditUser")){
     393            service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
     394            service_node.setAttribute("un",un);
     395            service_node.setAttribute("asn",asn);
     396
     397            //Get the user's info from the database
     398            if (cm.length()==0){
     399                service_node.setAttribute("info","edituser-interface");
     400                userQueryResult=derbyWrapper.findUser(umun,null);
     401                derbyWrapper.closeDatabase();
     402                Vector userInfo=userQueryResult.users_;
     403                String username=((UserTermInfo)userInfo.get(0)).username_;
     404                String password=((UserTermInfo)userInfo.get(0)).password_;
     405                String groups=((UserTermInfo)userInfo.get(0)).groups_;
     406                String accountstatus=((UserTermInfo)userInfo.get(0)).accountstatus_;
     407                String comment=((UserTermInfo)userInfo.get(0)).comment_;
     408
     409                service_node.setAttribute("oumun",oumun);
     410                service_node.setAttribute("umun",username);
     411                service_node.setAttribute("umpw",password);
     412                service_node.setAttribute("umgp",groups);
     413                service_node.setAttribute("umas",accountstatus);
     414                service_node.setAttribute("umc",comment);
     415                derbyWrapper.closeDatabase();
     416                return result;
     417            }
     418
     419            //Commit the modified user's info to the database
     420            if (cm.toLowerCase().equals("submit")){
     421                if (oumun.equals(umun)){// the user's name hasn't been changed, update the user's info
     422                    if (umpw.length()==0){
     423                        derbyWrapper.modifyUserInfo(umun,null,umgp,umas,umc);
     424                        userQueryResult=derbyWrapper.findUser(null,null);
     425                        derbyWrapper.closeDatabase();
     426                        service_node.setAttribute("info","all-un"); // the user's info has been updated, return a list of all users
     427                        Element user_node=getUserNode(userQueryResult);
     428                        service_node.appendChild(user_node);
     429                        derbyWrapper.closeDatabase();
     430                        return result;
     431                    }else{
     432                        if ((umpw.length()==0) || (umpw.length()<3) || (umpw.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw)))){
     433                            service_node.setAttribute("err","umpw-err"); //the input passwrod string is illegal
     434                            service_node.setAttribute("info","edituser-interface");
     435                            service_node.setAttribute("umun",umun);
     436                            service_node.setAttribute("umpw",umpw);
     437                            service_node.setAttribute("umgp",umgp);
     438                            service_node.setAttribute("umas",umas);
     439                            service_node.setAttribute("umc",umc);
     440                            service_node.setAttribute("oumun",oumun);
     441                            derbyWrapper.closeDatabase();
     442                            return result;
     443                        }
     444                        umgp=umgp.replaceAll(" ","");// get rid of the space
     445                        derbyWrapper.modifyUserInfo(umun,umpw,umgp,umas,umc);
     446                        userQueryResult=derbyWrapper.listAllUser();
     447                        derbyWrapper.closeDatabase();
     448                        service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users
     449                        Element user_node=getUserNode(userQueryResult);
     450                        service_node.appendChild(user_node);
     451                        derbyWrapper.closeDatabase();
     452                        return result;
     453                    }
     454                }
     455                // The user's name has been changed, add a new user record to the database
     456                else{
     457                    if ((umun.length()==0) || (umun.length()<2) || (umun.length()>30) || (!(Pattern.matches("[a-zA-Z0-9//_//.]+",umun)))){
     458                        service_node.setAttribute("err","umun-err"); //the input username string is illegal
     459                        service_node.setAttribute("umun",umun);
     460                        service_node.setAttribute("umpw",umpw);
     461                        service_node.setAttribute("umgp",umgp);
     462                        service_node.setAttribute("umas",umas);
     463                        service_node.setAttribute("umc",umc);
     464                        service_node.setAttribute("oumun",oumun);
     465                        service_node.setAttribute("info","edituser-interface");
     466                        derbyWrapper.closeDatabase();
     467                        return result;
     468                    }
     469                    if (umpw.length()==0){
     470                        service_node.setAttribute("err","ini-umpw-err"); //the input passwrod string is illegal
     471                        service_node.setAttribute("info","edituser-interface");
     472                        service_node.setAttribute("umun",umun);
     473                        service_node.setAttribute("umpw",umpw);
     474                        service_node.setAttribute("umgp",umgp);
     475                        service_node.setAttribute("umas",umas);
     476                        service_node.setAttribute("umc",umc);
     477                        service_node.setAttribute("oumun",oumun);
     478                        derbyWrapper.closeDatabase();
     479                        return result;
     480                    }
     481                    if ((umpw.length()<3) || (umpw.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw)))){
     482                        service_node.setAttribute("err","umpw-err"); //the input passwrod string is illegal
     483                        service_node.setAttribute("info","edituser-interface");
     484                        service_node.setAttribute("umun",umun);
     485                        service_node.setAttribute("umpw",umpw);
     486                        service_node.setAttribute("umgp",umgp);
     487                        service_node.setAttribute("umas",umas);
     488                        service_node.setAttribute("umc",umc);
     489                        service_node.setAttribute("oumun",oumun);
     490                        derbyWrapper.closeDatabase();
     491                        return result;
     492                    }
     493                    umgp=umgp.replaceAll(" ","");// get rid of the space
     494                    userQueryResult=derbyWrapper.findUser(umun,null);// check whether the new user name has existed in the table.
     495                    if (userQueryResult!=null){
     496                        service_node.setAttribute("err","un-exist"); //the new username string is duplicated
     497                        service_node.setAttribute("info","edituser-interface");
     498                        service_node.setAttribute("umun","");
     499                        service_node.setAttribute("umpw","");
     500                        service_node.setAttribute("umgp",umgp);
     501                        service_node.setAttribute("umas",umas);
     502                        service_node.setAttribute("umc",umc);
     503                        service_node.setAttribute("oumun",oumun);
     504                        derbyWrapper.closeDatabase();
     505                        return result;
     506                    }else{
     507                        derbyWrapper.addUser(umun,umpw,umgp,umas,umc);
     508                        userQueryResult=derbyWrapper.listAllUser();
     509                        derbyWrapper.closeDatabase();
     510                        service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users
     511                        Element user_node=getUserNode(userQueryResult);
     512                        service_node.appendChild(user_node);
     513                        derbyWrapper.closeDatabase();
     514                        return result;
     515                    }
     516                }
     517            }
     518
     519            if (cm.toLowerCase().equals("cancel")){
     520                userQueryResult=derbyWrapper.listAllUser();
     521                derbyWrapper.closeDatabase();
     522                service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users
     523                Element user_node=getUserNode(userQueryResult);
     524                service_node.appendChild(user_node);
     525                derbyWrapper.closeDatabase();
     526                return result;
     527            }
     528        }
     529
     530        //Action: modifypassword
     531        if (aup.equals("ModifyPassword")){
     532            if (un.equals("")){
     533                service_node.setAttribute("info","Login");
     534                derbyWrapper.closeDatabase();
     535                return result;
     536            }
     537
     538            service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
     539            service_node.setAttribute("un",un);
     540            service_node.setAttribute("asn",asn);
     541
     542            userQueryResult=derbyWrapper.findUser(un,null);
     543            Vector userInfo=userQueryResult.users_;
     544            pw=((UserTermInfo)userInfo.get(0)).password_;
     545
     546            if ((umpw1.length()==0) && (umpw2.length()==0) && (umpw.length()==0)){
     547                service_node.setAttribute("info","modify_interface");// call the interface of the modifying password
     548                derbyWrapper.closeDatabase();
     549                return result;
     550            }
     551
     552            if (!pw.equals(umpw) && umpw.length()>0){
     553                service_node.setAttribute("info","modify_interface");
     554                service_node.setAttribute("err","pw-umpw-nm-err");//if the original password is not match
     555                derbyWrapper.closeDatabase();
     556                return result;
     557            }
     558
     559            if ((umpw1.length()==0) || (umpw2.length()==0)){
     560                service_node.setAttribute("info","modify_interface");
     561                service_node.setAttribute("err","umpw1-umpw2-null-err");//if one of the password strings is none,return the err info back
     562                derbyWrapper.closeDatabase();
     563                return result;
     564            }
     565
     566            if(!umpw1.equals(umpw2)){
     567                service_node.setAttribute("info","modify_interface");
     568                service_node.setAttribute("err","umpw1-umpw2-nm-err");//if one of the password strings is none,return the err info back
     569                derbyWrapper.closeDatabase();
     570                return result;
     571            }
     572
     573            if (umpw.length()==0){
     574                service_node.setAttribute("info","modify_interface");
     575                service_node.setAttribute("err","umpw-null-err");//if one of the password strings is none,return the err info back
     576                derbyWrapper.closeDatabase();
     577                return result;
     578            }
     579            //check the new password and the retyped password
     580            if ((umpw1==null) || (umpw1.length()<3) || (umpw1.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw1)))){
     581                service_node.setAttribute("info","modify_interface");
     582                service_node.setAttribute("err","umpw1-err");// the new password is illegal
     583                derbyWrapper.closeDatabase();
     584                return result;
     585            } 
     586
     587            if ((umpw2==null) || (umpw2.length()<3) || (umpw2.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw2)))){
     588                service_node.setAttribute("info","modify_interface");
     589                service_node.setAttribute("err","umpw2-err"); // the retyped password is illegal
     590                derbyWrapper.closeDatabase();
     591                return result;
     592            } 
     593            String modify_user_info=derbyWrapper.modifyUserInfo(un,umpw1,null,null,null);
     594            if (modify_user_info.equals("succeed")){
     595                service_node.setAttribute("err","");// the passsword has been changed successfully
     596                derbyWrapper.closeDatabase();
     597                return result;
     598            }else{
     599                service_node.setAttribute("err",modify_user_info);// return the error message of the pasword couldn't be modified
     600                derbyWrapper.closeDatabase();
     601                return result;
     602            }
     603        }
     604
     605        //Action: deleteuser
     606        if (aup.equals("DeleteUser")){
     607            service_node.setAttribute("un",un);
     608            service_node.setAttribute("asn",asn);
     609            service_node.setAttribute("umun",umun);
     610            if (cm.equals("yes")){
     611                String delete_user=derbyWrapper.deleteUser(umun);
     612                if (delete_user.equals("succeed")){
     613                    service_node.setAttribute("err","");
     614                    userQueryResult=derbyWrapper.listAllUser();
     615                    service_node.setAttribute("info","all-un"); //  return a list of all users
     616                    Element user_node=getUserNode(userQueryResult);
     617                    service_node.appendChild(user_node);
     618                }else{
     619                    service_node.setAttribute("err",delete_user);//return the error message
     620                    derbyWrapper.closeDatabase();
     621                    return result;
     622                }
     623            }else if (cm.equals("no")){
     624                service_node.setAttribute("err","");
     625                userQueryResult=derbyWrapper.listAllUser();
     626                service_node.setAttribute("info","all-un"); //  return a list of all users
     627                Element user_node=getUserNode(userQueryResult);
     628                service_node.appendChild(user_node);
     629                derbyWrapper.closeDatabase();
     630                return result;
     631            }else{
     632                service_node.setAttribute("info","confirm");
     633                derbyWrapper.closeDatabase();
     634                return result;
     635            }
     636        }
     637
    188638        return result;
    189         }
    190    
    191     // if the authentication(uan=1) is required,but the user hasn't been authenticated(asn=0),the user is asked to login first
    192         if ((uan.equals("1") && asn.equals("0"))) {
    193         if ((un.length()==0) && (pw.length()==0)){
    194             service_node.setAttribute("asn","0");
    195             service_node.setAttribute("info","Login");
    196             derbyWrapper.closeDatabase();
    197             return result;
    198         }
    199         if ((un.length()==0) || (pw.length()==0)){
    200             service_node.setAttribute("asn","0");
    201             service_node.setAttribute("info","Login");
    202             service_node.setAttribute("err","un-pw-err");
    203             derbyWrapper.closeDatabase();
    204             return result;
    205         }else{ 
    206             userQueryResult=derbyWrapper.findUser(un,pw);//looking for the user from the users table
    207             service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
    208             service_node.setAttribute("un",un);
    209             if (userQueryResult==null){
    210             //the user isn't a vaild user
    211             service_node.setAttribute("asn","0");
    212             service_node.setAttribute("err","un-pw-err");// either unsername or password is wrong
    213             service_node.setAttribute("info","Login");
    214             derbyWrapper.closeDatabase();
    215             return result;
    216             }else{
    217             // asn="1"; //the user is a member of the "administrator" group
    218             Vector userInfo=userQueryResult.users_;
    219             String groups=((UserTermInfo)userInfo.get(0)).groups_;
    220             String accountstatus=((UserTermInfo)userInfo.get(0)).accountstatus_;
    221             if (accountstatus.trim().equals("false")){
    222                 service_node.setAttribute("asn","0");
    223                 service_node.setAttribute("err","as-false");//the account status is false
    224                 service_node.setAttribute("info","Login");
    225                 derbyWrapper.closeDatabase();
    226                 return result;
    227             }
    228             String[] groups_array=groups.split(",");
    229             for (int i=0; i<groups_array.length;i++){
    230                 if ((groups_array[i].trim().toLowerCase()).equals("administrator")){// check whether the user is in the administrator group
    231                 asn="1";
    232                 service_node.setAttribute("asn","1");
    233                 break;
    234                 }
    235             }
    236             if (!asn.equals("1")){
    237                 asn="2";
    238                 service_node.setAttribute("asn","2");//the user is authenticated
    239             }
    240             }
    241         }
    242         }
    243 
    244         //asn!=0 This is a valid user
    245         if (!asn.equals("0")){
    246         service_node.setAttribute("info","Login");
    247         service_node.setAttribute("un",un);
    248         service_node.setAttribute("asn",asn);
    249         derbyWrapper.closeDatabase();
    250         return result;
    251         }
    252     }
    253    
    254     //Action: listuser
    255     if (aup.equals("ListUsers")){
    256         if (asn.equals("") && un.equals("")){
    257         service_node.setAttribute("info","Login");
    258         derbyWrapper.closeDatabase();
    259         return result;
    260         }
    261 
    262        //valid users but not in the administrator group(asn=2), they cannot list all users
    263         if (asn.equals("2")){
    264         service_node.setAttribute("info","Login");
    265         service_node.setAttribute("err","no-permission");
    266         service_node.setAttribute("un",un);
    267         service_node.setAttribute("asn",asn);
    268         derbyWrapper.closeDatabase();
    269         return result;
    270         }
    271         //valid users belong to the administrator group(asn=1), they can list all users
    272         if (asn.equals("1")){
    273         userQueryResult=derbyWrapper.findUser(null,null);
    274         derbyWrapper.closeDatabase();
    275         service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
    276         service_node.setAttribute("un",un);
    277         service_node.setAttribute("asn",asn);
    278        
    279         if (userQueryResult!=null && userQueryResult.getSize()>0){
    280             service_node.setAttribute("info","all-un"); // got a user list
    281             Element user_node=getUserNode(userQueryResult);
    282             service_node.appendChild(user_node);
    283             derbyWrapper.closeDatabase();
    284             return result;
    285         }else {
    286             service_node.setAttribute("err","no-un"); // no user returned
    287             derbyWrapper.closeDatabase();
    288             return result;
    289         }   
    290         }
    291     }
    292    
    293     //Action : adduder
    294     if (aup.equals("AddUser")){
    295         if (asn.equals("") && un.equals("")){
    296         service_node.setAttribute("info","Login");
    297         derbyWrapper.closeDatabase();
    298         return result;
    299         }
    300         //valid users can't add a new user because they aren't in the administrator group(asn=2)
    301         if (asn.equals("2")){
    302         service_node.setAttribute("info","Login");
    303         service_node.setAttribute("err","no-permission");
    304         service_node.setAttribute("un",un);
    305         service_node.setAttribute("asn",asn);
    306         derbyWrapper.closeDatabase();
    307         return result;
    308         }
    309         //valid users are in the administrator group, they can add a new user(asn=1)
    310         if (asn.equals("1")){
    311         service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
    312         service_node.setAttribute("un",un);
    313         service_node.setAttribute("asn",asn);
    314 
    315         if (umun.length()==0 && umpw.length()==0 && umgp.length()==0 && umas.length()==0 && umc.length()==0){
    316             service_node.setAttribute("info","adduser_interface");
    317             derbyWrapper.closeDatabase();
    318             return result;
    319         }
    320        
    321         //check the strings of username and password
    322         if ((umun==null) || (umun.length()<2) || (umun.length()>30) || (!(Pattern.matches("[a-zA-Z0-9//_//.]+",umun)))){
    323             service_node.setAttribute("err","un-err"); //the input username string is illegal
    324             service_node.setAttribute("info","adduser_interface");
    325             derbyWrapper.closeDatabase();
    326             return result;
    327         }
    328        
    329         if ((umpw==null) || (umpw.length()<3) || (umpw.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw)))){
    330             service_node.setAttribute("err","pw-err"); //the input passwrod string is illegal
    331             service_node.setAttribute("info","adduser_interface");
    332             derbyWrapper.closeDatabase();
    333             return result;
    334         }
    335 
    336         // add the new users into the users table
    337         umgp=umgp.replaceAll(" ","");//get rid of the space of the groups string
    338         userQueryResult=derbyWrapper.findUser(umun,null);// check whether the new user name has existed in the table.
    339         if (userQueryResult!=null){
    340             service_node.setAttribute("err","un-exist"); //the new username string is duplicated
    341             service_node.setAttribute("info","adduser_interface");
    342             derbyWrapper.closeDatabase();
    343             return result;
    344         }else{
    345             String add_user=derbyWrapper.addUser(umun,umpw,umgp,umas,umc);
    346             if (add_user.equals("succeed")){
    347             userQueryResult=derbyWrapper.findUser(null,null);
    348             derbyWrapper.closeDatabase();
    349             service_node.setAttribute("info","all-un"); // return a list of all users if the user has been added
    350             Element user_node=getUserNode(userQueryResult);
    351             service_node.appendChild(user_node);
    352             derbyWrapper.closeDatabase();
    353             return result;
    354             }else{
    355             derbyWrapper.closeDatabase();
    356             service_node.setAttribute("err",add_user);// return the error message if the user couldn't be added
    357             derbyWrapper.closeDatabase();
    358             return result;
    359             }
    360         }
    361         }
    362     }
    363 
    364     //Action: edituser
    365     if (aup.equals("EditUser")){
    366         service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
    367         service_node.setAttribute("un",un);
    368         service_node.setAttribute("asn",asn);
    369        
    370         //Get the user's info from the database
    371         if (cm.length()==0){
    372         service_node.setAttribute("info","edituser-interface");
    373         userQueryResult=derbyWrapper.findUser(umun,null);
    374         derbyWrapper.closeDatabase();
     639    }
     640
     641    private Element getUserNode(UserQueryResult userQueryResult){
     642        Element user_list_node= this.doc.createElement(GSXML.USER_NODE_ELEM+"List");
     643
    375644        Vector userInfo=userQueryResult.users_;
    376         String username=((UserTermInfo)userInfo.get(0)).username_;
    377         String password=((UserTermInfo)userInfo.get(0)).password_;
    378         String groups=((UserTermInfo)userInfo.get(0)).groups_;
    379         String accountstatus=((UserTermInfo)userInfo.get(0)).accountstatus_;
    380         String comment=((UserTermInfo)userInfo.get(0)).comment_;
    381 
    382         service_node.setAttribute("oumun",oumun);
    383         service_node.setAttribute("umun",username);
    384         service_node.setAttribute("umpw",password);
    385         service_node.setAttribute("umgp",groups);
    386         service_node.setAttribute("umas",accountstatus);
    387         service_node.setAttribute("umc",comment);
    388         derbyWrapper.closeDatabase();
    389         return result;
    390         }
    391        
    392         //Commit the modified user's info to the database
    393         if (cm.toLowerCase().equals("submit")){
    394         if (oumun.equals(umun)){// the user's name hasn't been changed, update the user's info
    395             if (umpw.length()==0){
    396             derbyWrapper.modifyUserInfo(umun,null,umgp,umas,umc);
    397             userQueryResult=derbyWrapper.findUser(null,null);
    398             derbyWrapper.closeDatabase();
    399             service_node.setAttribute("info","all-un"); // the user's info has been updated, return a list of all users
    400             Element user_node=getUserNode(userQueryResult);
    401             service_node.appendChild(user_node);
    402             derbyWrapper.closeDatabase();
    403             return result;
    404             }else{
    405             if ((umpw.length()==0) || (umpw.length()<3) || (umpw.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw)))){
    406                 service_node.setAttribute("err","umpw-err"); //the input passwrod string is illegal
    407                 service_node.setAttribute("info","edituser-interface");
    408                 service_node.setAttribute("umun",umun);
    409                 service_node.setAttribute("umpw",umpw);
    410                 service_node.setAttribute("umgp",umgp);
    411                 service_node.setAttribute("umas",umas);
    412                 service_node.setAttribute("umc",umc);
    413                 service_node.setAttribute("oumun",oumun);
    414                 derbyWrapper.closeDatabase();
    415                 return result;
    416             }
    417             umgp=umgp.replaceAll(" ","");// get rid of the space
    418             derbyWrapper.modifyUserInfo(umun,umpw,umgp,umas,umc);
    419             userQueryResult=derbyWrapper.listAllUser();
    420             derbyWrapper.closeDatabase();
    421             service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users
    422             Element user_node=getUserNode(userQueryResult);
    423             service_node.appendChild(user_node);
    424             derbyWrapper.closeDatabase();
    425             return result;
    426             }
    427         }
    428         // The user's name has been changed, add a new user record to the database
    429         else{
    430             if ((umun.length()==0) || (umun.length()<2) || (umun.length()>30) || (!(Pattern.matches("[a-zA-Z0-9//_//.]+",umun)))){
    431             service_node.setAttribute("err","umun-err"); //the input username string is illegal
    432             service_node.setAttribute("umun",umun);
    433             service_node.setAttribute("umpw",umpw);
    434             service_node.setAttribute("umgp",umgp);
    435             service_node.setAttribute("umas",umas);
    436             service_node.setAttribute("umc",umc);
    437             service_node.setAttribute("oumun",oumun);
    438             service_node.setAttribute("info","edituser-interface");
    439             derbyWrapper.closeDatabase();
    440             return result;
    441             }
    442             if (umpw.length()==0){
    443             service_node.setAttribute("err","ini-umpw-err"); //the input passwrod string is illegal
    444             service_node.setAttribute("info","edituser-interface");
    445             service_node.setAttribute("umun",umun);
    446             service_node.setAttribute("umpw",umpw);
    447             service_node.setAttribute("umgp",umgp);
    448             service_node.setAttribute("umas",umas);
    449             service_node.setAttribute("umc",umc);
    450             service_node.setAttribute("oumun",oumun);
    451             derbyWrapper.closeDatabase();
    452             return result;
    453             }
    454             if ((umpw.length()<3) || (umpw.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw)))){
    455             service_node.setAttribute("err","umpw-err"); //the input passwrod string is illegal
    456             service_node.setAttribute("info","edituser-interface");
    457             service_node.setAttribute("umun",umun);
    458             service_node.setAttribute("umpw",umpw);
    459             service_node.setAttribute("umgp",umgp);
    460             service_node.setAttribute("umas",umas);
    461             service_node.setAttribute("umc",umc);
    462             service_node.setAttribute("oumun",oumun);
    463             derbyWrapper.closeDatabase();
    464             return result;
    465             }
    466             umgp=umgp.replaceAll(" ","");// get rid of the space
    467             userQueryResult=derbyWrapper.findUser(umun,null);// check whether the new user name has existed in the table.
    468             if (userQueryResult!=null){
    469             service_node.setAttribute("err","un-exist"); //the new username string is duplicated
    470             service_node.setAttribute("info","edituser-interface");
    471             service_node.setAttribute("umun","");
    472             service_node.setAttribute("umpw","");
    473             service_node.setAttribute("umgp",umgp);
    474             service_node.setAttribute("umas",umas);
    475             service_node.setAttribute("umc",umc);
    476             service_node.setAttribute("oumun",oumun);
    477             derbyWrapper.closeDatabase();
    478             return result;
    479             }else{
    480             derbyWrapper.addUser(umun,umpw,umgp,umas,umc);
    481             userQueryResult=derbyWrapper.listAllUser();
    482             derbyWrapper.closeDatabase();
    483             service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users
    484             Element user_node=getUserNode(userQueryResult);
    485             service_node.appendChild(user_node);
    486             derbyWrapper.closeDatabase();
    487             return result;
    488             }
    489         }
    490         }
    491 
    492         if (cm.toLowerCase().equals("cancel")){
    493         userQueryResult=derbyWrapper.listAllUser();
    494         derbyWrapper.closeDatabase();
    495         service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users
    496         Element user_node=getUserNode(userQueryResult);
    497         service_node.appendChild(user_node);
    498         derbyWrapper.closeDatabase();
    499         return result;
    500         }
    501     }
    502 
    503     //Action: modifypassword
    504     if (aup.equals("ModifyPassword")){
    505         if (un.equals("")){
    506         service_node.setAttribute("info","Login");
    507         derbyWrapper.closeDatabase();
    508         return result;
    509         }
    510 
    511         service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
    512         service_node.setAttribute("un",un);
    513         service_node.setAttribute("asn",asn);
    514 
    515         userQueryResult=derbyWrapper.findUser(un,null);
    516         Vector userInfo=userQueryResult.users_;
    517         pw=((UserTermInfo)userInfo.get(0)).password_;
    518          
    519         if ((umpw1.length()==0) && (umpw2.length()==0) && (umpw.length()==0)){
    520         service_node.setAttribute("info","modify_interface");// call the interface of the modifying password
    521         derbyWrapper.closeDatabase();
    522         return result;
    523         }
    524 
    525         if (!pw.equals(umpw) && umpw.length()>0){
    526         service_node.setAttribute("info","modify_interface");
    527         service_node.setAttribute("err","pw-umpw-nm-err");//if the original password is not match
    528         derbyWrapper.closeDatabase();
    529         return result;
    530         }
    531          
    532         if ((umpw1.length()==0) || (umpw2.length()==0)){
    533         service_node.setAttribute("info","modify_interface");
    534         service_node.setAttribute("err","umpw1-umpw2-null-err");//if one of the password strings is none,return the err info back
    535         derbyWrapper.closeDatabase();
    536         return result;
    537         }
    538 
    539         if(!umpw1.equals(umpw2)){
    540         service_node.setAttribute("info","modify_interface");
    541         service_node.setAttribute("err","umpw1-umpw2-nm-err");//if one of the password strings is none,return the err info back
    542         derbyWrapper.closeDatabase();
    543         return result;
    544         }
    545        
    546         if (umpw.length()==0){
    547         service_node.setAttribute("info","modify_interface");
    548         service_node.setAttribute("err","umpw-null-err");//if one of the password strings is none,return the err info back
    549         derbyWrapper.closeDatabase();
    550         return result;
    551         }
    552         //check the new password and the retyped password
    553         if ((umpw1==null) || (umpw1.length()<3) || (umpw1.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw1)))){
    554         service_node.setAttribute("info","modify_interface");
    555         service_node.setAttribute("err","umpw1-err");// the new password is illegal
    556         derbyWrapper.closeDatabase();
    557         return result;
    558         } 
    559        
    560         if ((umpw2==null) || (umpw2.length()<3) || (umpw2.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw2)))){
    561         service_node.setAttribute("info","modify_interface");
    562         service_node.setAttribute("err","umpw2-err"); // the retyped password is illegal
    563         derbyWrapper.closeDatabase();
    564         return result;
    565          } 
    566         String modify_user_info=derbyWrapper.modifyUserInfo(un,umpw1,null,null,null);
    567         if (modify_user_info.equals("succeed")){
    568         service_node.setAttribute("err","");// the passsword has been changed successfully
    569         derbyWrapper.closeDatabase();
    570         return result;
    571         }else{
    572         service_node.setAttribute("err",modify_user_info);// return the error message of the pasword couldn't be modified
    573         derbyWrapper.closeDatabase();
    574         return result;
    575         }
    576     }
    577 
    578     //Action: deleteuser
    579     if (aup.equals("DeleteUser")){
    580         service_node.setAttribute("un",un);
    581         service_node.setAttribute("asn",asn);
    582         service_node.setAttribute("umun",umun);
    583         if (cm.equals("yes")){
    584         String delete_user=derbyWrapper.deleteUser(umun);
    585         if (delete_user.equals("succeed")){
    586             service_node.setAttribute("err","");
    587             userQueryResult=derbyWrapper.listAllUser();
    588             service_node.setAttribute("info","all-un"); //  return a list of all users
    589             Element user_node=getUserNode(userQueryResult);
    590             service_node.appendChild(user_node);
    591         }else{
    592             service_node.setAttribute("err",delete_user);//return the error message
    593             derbyWrapper.closeDatabase();
    594             return result;
    595         }
    596         }else if (cm.equals("no")){
    597         service_node.setAttribute("err","");
    598         userQueryResult=derbyWrapper.listAllUser();
    599         service_node.setAttribute("info","all-un"); //  return a list of all users
    600         Element user_node=getUserNode(userQueryResult);
    601         service_node.appendChild(user_node);
    602         derbyWrapper.closeDatabase();
    603         return result;
    604         }else{
    605         service_node.setAttribute("info","confirm");
    606         derbyWrapper.closeDatabase();
    607         return result;
    608         }
    609     }
    610 
    611     return result;
    612     }
    613    
    614     private Element getUserNode(UserQueryResult userQueryResult){
    615     Element user_list_node= this.doc.createElement(GSXML.USER_NODE_ELEM+"List");
    616    
    617     Vector userInfo=userQueryResult.users_;
    618    
    619     for (int i=0; i<userQueryResult.getSize(); i++){
    620         Element user_node= this.doc.createElement(GSXML.USER_NODE_ELEM);
    621         String username=((UserTermInfo)userInfo.get(i)).username_;
    622         String password=((UserTermInfo)userInfo.get(i)).password_;
    623         String groups=((UserTermInfo)userInfo.get(i)).groups_;
    624         String accountstatus=((UserTermInfo)userInfo.get(i)).accountstatus_;
    625         String comment=((UserTermInfo)userInfo.get(i)).comment_;
    626         user_node.setAttribute("umun",username);
    627         user_node.setAttribute("umpw",password);
    628         user_node.setAttribute("umgp",groups);
    629         user_node.setAttribute("umas",accountstatus);
    630         user_node.setAttribute("umc",comment);
    631        
    632         user_list_node.appendChild(user_node);
    633     }
    634     return user_list_node; 
    635     }
    636        
    637     private Element getCollectList(String collect){
    638     Element collect_list_node = this.doc.createElement(GSXML.COLLECTION_ELEM+"List");
    639     File[] collect_dir= (new File(collect)).listFiles();
    640     if(collect_dir!=null && collect_dir.length > 0){
    641         for (int i=0;i<collect_dir.length;i++){
    642         if (collect_dir[i].isDirectory() && (!collect_dir[i].getName().startsWith(".svn"))){
    643             Element collect_node = this.doc.createElement(GSXML.COLLECTION_ELEM);
    644             collect_node.setAttribute("name",collect_dir[i].getName());
    645             collect_list_node.appendChild(collect_node);
    646         }
    647         }
    648     }
    649     return collect_list_node;
    650     }
     645
     646        for (int i=0; i<userQueryResult.getSize(); i++){
     647            Element user_node= this.doc.createElement(GSXML.USER_NODE_ELEM);
     648            String username=((UserTermInfo)userInfo.get(i)).username_;
     649            String password=((UserTermInfo)userInfo.get(i)).password_;
     650            String groups=((UserTermInfo)userInfo.get(i)).groups_;
     651            String accountstatus=((UserTermInfo)userInfo.get(i)).accountstatus_;
     652            String comment=((UserTermInfo)userInfo.get(i)).comment_;
     653            user_node.setAttribute("umun",username);
     654            user_node.setAttribute("umpw",password);
     655            user_node.setAttribute("umgp",groups);
     656            user_node.setAttribute("umas",accountstatus);
     657            user_node.setAttribute("umc",comment);
     658
     659            user_list_node.appendChild(user_node);
     660        }
     661        return user_list_node; 
     662    }
     663
     664    private Element getCollectList(String collect){
     665        Element collect_list_node = this.doc.createElement(GSXML.COLLECTION_ELEM+"List");
     666        File[] collect_dir= (new File(collect)).listFiles();
     667        if(collect_dir!=null && collect_dir.length > 0){
     668            for (int i=0;i<collect_dir.length;i++){
     669                if (collect_dir[i].isDirectory() && (!collect_dir[i].getName().startsWith(".svn"))){
     670                    Element collect_node = this.doc.createElement(GSXML.COLLECTION_ELEM);
     671                    collect_node.setAttribute("name",collect_dir[i].getName());
     672                    collect_list_node.appendChild(collect_node);
     673                }
     674            }
     675        }
     676        return collect_list_node;
     677    }
    651678}
    652  
     679
Note: See TracChangeset for help on using the changeset viewer.