Ignore:
Timestamp:
2012-04-03T20:56:04+12:00 (12 years ago)
Author:
ak19
Message:
  1. Replacing ChangePwdUsersDB.java with new file ModifyUsersDB.java, since the latter allows you to modify any and all fields for a username. 2. Now build.xml's config-admin target has been updated to call ModifyUsersDB with a new password for the admin user. A new target, config-user, takes user input to set any or all fields of any username. This can then be called by the release-kit if we wish to add a demo user during installation as we did in the GS2 releasekit. 3. Updated txt2usersDB.java to take the append flag: with this flag on, it will no longer delete the entire DB and read a new DB in from the input text file, but will append the additional entries in the input text file to the existing entries in the usersDB.
Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/txt2usersDB.java

    r25308 r25338  
    2121import java.io.BufferedReader;
    2222import java.io.IOException;
     23import java.io.File;
    2324import java.io.FileReader;
    2425import java.sql.SQLException;
     26
     27import org.greenstone.gsdl3.service.Authentication;
    2528
    2629public class txt2usersDB {
    2730   
    2831    public static void main(String[] args) throws SQLException{
    29    
    30     if (args.length!=2){
    31         System.out.println("Usage: java org.greenstone.gsdl3.txt2usersDB full_path_of_the_text_file full_path_of_the_usersDB");
     32    boolean appending = false;
     33
     34    String usage = "Usage: java org.greenstone.gsdl3.txt2usersDB full_path_of_the_text_file full_path_of_the_usersDB [-append]";
     35    if (args.length < 2){
     36        System.out.println(usage);
    3237        System.exit(0);
    3338    }
     39    File txtfile = new File(args[0]);
     40    if(!txtfile.exists()) {
     41        System.out.println("File " + args[0] + " does not exist.");
     42        System.out.println(usage);     
     43        System.exit(0);
     44    }
     45
    3446    try {
    3547        BufferedReader in = new BufferedReader(new FileReader(args[0]));
     
    3749        DerbyWrapper dw=new DerbyWrapper();
    3850        dw.connectDatabase(args[1],false);
    39         boolean delete_rows = dw.deleteAllUser();
    40         dw.closeDatabase();
    41         if (!delete_rows){
    42         System.out.println("Couldn't delete rows of the users table");
    43         System.exit(0);
    44         }
     51
     52        if(args.length > 2 && args[2].equals("-append")) {
     53            appending = true;
     54        } else {
     55        // no appending, replace existing database: the text file
     56        // represents the new database, so delete the existing DB first
     57        boolean delete_rows = dw.deleteAllUser();
     58        dw.closeDatabase();
     59        if (!delete_rows){
     60            System.out.println("Couldn't delete rows of the users table");
     61            System.exit(0);
     62        }
     63        }
     64
    4565        String username=null;
    4666        String password=null;
     
    4969        String comment=null;
    5070        String email=null;
     71
    5172        while ((str = in.readLine()) != null) {
    5273        //ystem.out.println(str);
     
    88109            groups=str.substring(str.indexOf(">")+1,str.length());
    89110            }
    90             if (field.equals("password")){
    91             //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length()));
     111            if (field.equals("password")){         
    92112            password=str.substring(str.indexOf(">")+1,str.length());
    93113            }
     
    97117        }
    98118        else if (str.equals("----------------------------------------------------------------------")
    99              || str.equals("-------------------------------------")) {         
     119             || str.equals("-------------------------------------")) {
    100120           
    101             if ((username!=null) && (password!=null) && (groups!=null) && (accountstatus!=null) && (comment!=null) && (email!=null)) {
     121            if ((username!=null) && (password!=null) && (groups!=null) && (accountstatus!=null) && (comment!=null)) {
    102122            dw.connectDatabase(args[1],false);
    103             dw.addUser(username, password, groups, accountstatus, comment, email);
     123
     124            // check if it's a new user or already exists in the database
     125            UserQueryResult findUserResult = dw.findUser(username);
     126           
     127            if(findUserResult == null) { // add new user
     128                if(password.length() >= 3 && password.length() <= 8) { // if not yet encrypted, encrypt first
     129                password = Authentication.hashPassword(password);
     130                } // if > 8 chars, password for user being added was already encrypted (hashed-and-hexed)
     131                dw.addUser(username, password, groups, accountstatus, comment, email);
     132            }
     133
     134            else { // modify existing user
     135                // if any of the other fields are not specified, get them from the database
     136                UserTermInfo user = findUserResult.getUserTerms().get(0);
     137               
     138                if(password.length() < 3 || password.length() > 8) { // includes empty string case
     139                password = user.password;
     140                } else { // need to first encrypt (hash-and-hex) the user-entered password
     141                // Use the same encryption technique used by the Admin Authentication page
     142                // This ensures that the password generated for a string remains consistent
     143                password = Authentication.hashPassword(password);
     144                }
     145                groups = groups.equals("") ? user.groups : groups;
     146                accountstatus = accountstatus.equals("") ? user.accountstatus : accountstatus;
     147                comment = comment.equals("") ? user.comment : comment;
     148
     149                if (email == null) { // special checking for backwards compatibility since old DB did not have email field
     150                email = "";
     151                }
     152                if(user.email == null) {
     153                user.email = "";
     154                }
     155                if(email.equals("")) {
     156                email = user.email;
     157                }
     158               
     159                //System.err.println("**** Password: " + password);             
     160                //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
     161                dw.modifyUserInfo(username, password, groups, accountstatus, comment, email);
     162            }
     163           
    104164            username=null;
    105165            password=null;
     
    112172            }
    113173        }
    114         else { // encrypted passwords can span multiple lines for some reason
     174       
     175        // only true back when when hashed passwords weren't being converted to hex
     176        //else { // encrypted passwords can span multiple lines for some reason
    115177               // assume that is the case here
    116             if(password != null) {
    117             password = password + "\n" + str;
    118             }
    119         }
    120         }   
     178        //if(password != null) {
     179        //  password = password + "\n" + str;
     180        //  }
     181        //}
     182
     183        }
     184        //dw.closeDatabase();
    121185        in.close();
    122186    } catch (IOException e) {
Note: See TracChangeset for help on using the changeset viewer.