Changeset 25338 for main/trunk/greenstone3/src/java
- Timestamp:
- 2012-04-03T20:56:04+12:00 (12 years ago)
- 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 21 21 import java.io.BufferedReader; 22 22 import java.io.IOException; 23 import java.io.File; 23 24 import java.io.FileReader; 24 25 import java.sql.SQLException; 26 27 import org.greenstone.gsdl3.service.Authentication; 25 28 26 29 public class txt2usersDB { 27 30 28 31 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); 32 37 System.exit(0); 33 38 } 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 34 46 try { 35 47 BufferedReader in = new BufferedReader(new FileReader(args[0])); … … 37 49 DerbyWrapper dw=new DerbyWrapper(); 38 50 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 45 65 String username=null; 46 66 String password=null; … … 49 69 String comment=null; 50 70 String email=null; 71 51 72 while ((str = in.readLine()) != null) { 52 73 //ystem.out.println(str); … … 88 109 groups=str.substring(str.indexOf(">")+1,str.length()); 89 110 } 90 if (field.equals("password")){ 91 //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length())); 111 if (field.equals("password")){ 92 112 password=str.substring(str.indexOf(">")+1,str.length()); 93 113 } … … 97 117 } 98 118 else if (str.equals("----------------------------------------------------------------------") 99 || str.equals("-------------------------------------")) { 119 || str.equals("-------------------------------------")) { 100 120 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)) { 102 122 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 104 164 username=null; 105 165 password=null; … … 112 172 } 113 173 } 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 115 177 // 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(); 121 185 in.close(); 122 186 } catch (IOException e) {
Note:
See TracChangeset
for help on using the changeset viewer.