Ignore:
Timestamp:
2013-09-03T12:30:35+12:00 (11 years ago)
Author:
sjm84
Message:

Some major changes to DerbyWrapper to try and make it more reliable and consistent

File:
1 edited

Legend:

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

    r25338 r28202  
    2727import org.greenstone.gsdl3.service.Authentication;
    2828
    29 public class txt2usersDB {
    30    
    31     public static void main(String[] args) throws SQLException{
    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);
    37         System.exit(0);
     29public class txt2usersDB
     30{
     31
     32    public static void main(String[] args) throws SQLException
     33    {
     34        boolean appending = false;
     35
     36        String usage = "Usage: java org.greenstone.gsdl3.txt2usersDB full_path_of_the_text_file full_path_of_the_usersDB [-append]";
     37        if (args.length < 2)
     38        {
     39            System.out.println(usage);
     40            System.exit(0);
     41        }
     42        File txtfile = new File(args[0]);
     43        if (!txtfile.exists())
     44        {
     45            System.out.println("File " + args[0] + " does not exist.");
     46            System.out.println(usage);
     47            System.exit(0);
     48        }
     49
     50        try
     51        {
     52            BufferedReader in = new BufferedReader(new FileReader(args[0]));
     53            String str;
     54            DerbyWrapper dw = new DerbyWrapper(args[1]);
     55
     56            if (args.length > 2 && args[2].equals("-append"))
     57            {
     58                appending = true;
     59            }
     60            else
     61            {
     62                // no appending, replace existing database: the text file
     63                // represents the new database, so delete the existing DB first
     64                boolean delete_rows = dw.deleteAllUser();
     65                dw.closeDatabase();
     66                if (!delete_rows)
     67                {
     68                    System.out.println("Couldn't delete rows of the users table");
     69                    System.exit(0);
     70                }
     71            }
     72
     73            String username = null;
     74            String password = null;
     75            String groups = null;
     76            String accountstatus = null;
     77            String comment = null;
     78            String email = null;
     79
     80            while ((str = in.readLine()) != null)
     81            {
     82                //ystem.out.println(str);
     83
     84                if (str.indexOf(" = ") != -1)
     85                { // works with DerbyWrapper.db2txt() and usersDB2txt.java. Fields listed as: USERNAME = admin
     86                    String field = str.substring(0, str.indexOf(" = "));
     87                    if (field.equalsIgnoreCase("email"))
     88                    {
     89                        email = str.substring(str.indexOf(" = ") + 3, str.length());
     90                    }
     91                    if (field.equalsIgnoreCase("comment"))
     92                    {
     93                        comment = str.substring(str.indexOf(" = ") + 3, str.length());
     94                    }
     95                    if (field.equalsIgnoreCase("status"))
     96                    {
     97                        accountstatus = str.substring(str.indexOf(" = ") + 3, str.length());
     98                    }
     99                    if (field.equalsIgnoreCase("groups"))
     100                    {
     101                        groups = str.substring(str.indexOf(" = ") + 3, str.length());
     102                    }
     103                    if (field.equalsIgnoreCase("password"))
     104                    {
     105                        //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length()));
     106                        password = str.substring(str.indexOf(" = ") + 3, str.length());
     107                    }
     108                    if (field.equalsIgnoreCase("username"))
     109                    {
     110                        username = str.substring(str.indexOf(" = ") + 3, str.length());
     111                    }
     112                }
     113                else if (str.startsWith("<"))
     114                { // fields listed as: <username>admin
     115                    String field = str.substring(1, str.indexOf(">"));
     116                    if (field.equals("email"))
     117                    {
     118                        email = str.substring(str.indexOf(">") + 1, str.length());
     119                    }
     120                    if (field.equals("comment"))
     121                    {
     122                        comment = str.substring(str.indexOf(">") + 1, str.length());
     123                    }
     124                    if (field.equals("enabled") || field.equals("status"))
     125                    {
     126                        accountstatus = str.substring(str.indexOf(">") + 1, str.length());
     127                    }
     128                    if (field.equals("groups"))
     129                    {
     130                        groups = str.substring(str.indexOf(">") + 1, str.length());
     131                    }
     132                    if (field.equals("password"))
     133                    {
     134                        password = str.substring(str.indexOf(">") + 1, str.length());
     135                    }
     136                    if (field.equals("username"))
     137                    {
     138                        username = str.substring(str.indexOf(">") + 1, str.length());
     139                    }
     140                }
     141                else if (str.equals("----------------------------------------------------------------------") || str.equals("-------------------------------------"))
     142                {
     143
     144                    if ((username != null) && (password != null) && (groups != null) && (accountstatus != null) && (comment != null))
     145                    {
     146                        dw.connectDatabase(args[1], false);
     147
     148                        // check if it's a new user or already exists in the database
     149                        UserQueryResult findUserResult = dw.findUser(username);
     150
     151                        if (findUserResult == null)
     152                        { // add new user
     153                            if (password.length() >= 3 && password.length() <= 8)
     154                            { // if not yet encrypted, encrypt first
     155                                password = Authentication.hashPassword(password);
     156                            } // if > 8 chars, password for user being added was already encrypted (hashed-and-hexed)
     157                            dw.addUser(username, password, groups, accountstatus, comment, email);
     158                        }
     159
     160                        else
     161                        { // modify existing user
     162                            // if any of the other fields are not specified, get them from the database
     163                            UserTermInfo user = findUserResult.getUserTerms().get(0);
     164
     165                            if (password.length() < 3 || password.length() > 8)
     166                            { // includes empty string case
     167                                password = user.password;
     168                            }
     169                            else
     170                            { // need to first encrypt (hash-and-hex) the user-entered password
     171                                // Use the same encryption technique used by the Admin Authentication page
     172                                // This ensures that the password generated for a string remains consistent
     173                                password = Authentication.hashPassword(password);
     174                            }
     175                            groups = groups.equals("") ? user.groups : groups;
     176                            accountstatus = accountstatus.equals("") ? user.accountstatus : accountstatus;
     177                            comment = comment.equals("") ? user.comment : comment;
     178
     179                            if (email == null)
     180                            { // special checking for backwards compatibility since old DB did not have email field
     181                                email = "";
     182                            }
     183                            if (user.email == null)
     184                            {
     185                                user.email = "";
     186                            }
     187                            if (email.equals(""))
     188                            {
     189                                email = user.email;
     190                            }
     191
     192                            //System.err.println("**** Password: " + password);             
     193                            //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
     194                            dw.modifyUserInfo(username, password, groups, accountstatus, comment, email);
     195                        }
     196
     197                        username = null;
     198                        password = null;
     199                        groups = null;
     200                        accountstatus = null;
     201                        comment = null;
     202                        email = null;
     203                        //dw.connectDatabase(args[1],false); // should this be closeDatabase()????
     204                        dw.closeDatabase();
     205                    }
     206                }
     207
     208                // only true back when when hashed passwords weren't being converted to hex
     209                //else { // encrypted passwords can span multiple lines for some reason
     210                // assume that is the case here
     211                //if(password != null) {
     212                //  password = password + "\n" + str;
     213                //  }
     214                //}
     215
     216            }
     217            //dw.closeDatabase();
     218            in.close();
     219        }
     220        catch (IOException e)
     221        {
     222        }
    38223    }
    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 
    46     try {
    47         BufferedReader in = new BufferedReader(new FileReader(args[0]));
    48         String str;
    49         DerbyWrapper dw=new DerbyWrapper();
    50         dw.connectDatabase(args[1],false);
    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 
    65         String username=null;
    66         String password=null;
    67         String groups=null;
    68         String accountstatus=null;
    69         String comment=null;
    70         String email=null;
    71 
    72         while ((str = in.readLine()) != null) {
    73         //ystem.out.println(str);
    74        
    75         if(str.indexOf(" = ") != -1) { // works with DerbyWrapper.db2txt() and usersDB2txt.java. Fields listed as: USERNAME = admin
    76             String field=str.substring(0,str.indexOf(" = "));
    77             if (field.equalsIgnoreCase("email")){
    78             email=str.substring(str.indexOf(" = ")+3,str.length());
    79             }
    80             if (field.equalsIgnoreCase("comment")){
    81             comment=str.substring(str.indexOf(" = ")+3,str.length());
    82             }
    83             if (field.equalsIgnoreCase("status")){
    84             accountstatus=str.substring(str.indexOf(" = ")+3,str.length());
    85             }
    86             if (field.equalsIgnoreCase("groups")){
    87             groups=str.substring(str.indexOf(" = ")+3,str.length());
    88             }
    89             if (field.equalsIgnoreCase("password")){
    90             //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length()));
    91             password=str.substring(str.indexOf(" = ")+3,str.length());
    92             }
    93             if (field.equalsIgnoreCase("username")){
    94             username=str.substring(str.indexOf(" = ")+3,str.length());
    95             }
    96         }       
    97         else if (str.startsWith("<")){ // fields listed as: <username>admin
    98             String field=str.substring(1,str.indexOf(">"));
    99             if (field.equals("email")){
    100             email=str.substring(str.indexOf(">")+1,str.length());
    101             }
    102             if (field.equals("comment")){
    103             comment=str.substring(str.indexOf(">")+1,str.length());
    104             }
    105             if (field.equals("enabled") || field.equals("status")){
    106             accountstatus=str.substring(str.indexOf(">")+1,str.length());
    107             }
    108             if (field.equals("groups")){
    109             groups=str.substring(str.indexOf(">")+1,str.length());
    110             }
    111             if (field.equals("password")){         
    112             password=str.substring(str.indexOf(">")+1,str.length());
    113             }
    114             if (field.equals("username")){
    115             username=str.substring(str.indexOf(">")+1,str.length());
    116             }
    117         }
    118         else if (str.equals("----------------------------------------------------------------------")
    119              || str.equals("-------------------------------------")) {
    120            
    121             if ((username!=null) && (password!=null) && (groups!=null) && (accountstatus!=null) && (comment!=null)) {
    122             dw.connectDatabase(args[1],false);
    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            
    164             username=null;
    165             password=null;
    166             groups=null;
    167             accountstatus=null;
    168             comment=null;
    169             email=null;
    170             //dw.connectDatabase(args[1],false); // should this be closeDatabase()????
    171             dw.closeDatabase();
    172             }
    173         }
    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
    177                // assume that is the case here
    178         //if(password != null) {
    179         //  password = password + "\n" + str;
    180         //  }
    181         //}
    182 
    183         }
    184         //dw.closeDatabase();
    185         in.close();
    186     } catch (IOException e) {
    187     }
    188     }
    189224}
Note: See TracChangeset for help on using the changeset viewer.