Changeset 35333 for main/trunk/greenstone3/src/java
- Timestamp:
- 2021-09-06T16:48:05+12:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/ModifyUsersDB.java
r35298 r35333 20 20 21 21 import java.sql.SQLException; 22 import java.util.Iterator; 22 23 import org.greenstone.gsdl3.service.Authentication; 23 24 //import org.greenstone.admin.guiext.PropertiesStep; … … 27 28 then run: 28 29 29 java -Dgsdl3.writablehome=/Scratch/ak19/gs3-svn-2Sep2015/web -cp web/WEB-INF/lib/gsdl3.jar:web/WEB-INF/lib/gutil.jar:web/WEB-INF/lib/derby.jar:web/WEB-INF/lib/derbyclient.jar:web/WEB-INF/lib/log4j-1.2.8.jar:web/WEB-INF/lib/commons-codec-1.7.jar:web/WEB-INF/classes org.greenstone.gsdl3.util.ModifyUsersDB web/etc/usersDB/ <username > [options specifying user fields to change, e.g.: password=me!]30 java -Dgsdl3.writablehome=/Scratch/ak19/gs3-svn-2Sep2015/web -cp web/WEB-INF/lib/gsdl3.jar:web/WEB-INF/lib/gutil.jar:web/WEB-INF/lib/derby.jar:web/WEB-INF/lib/derbyclient.jar:web/WEB-INF/lib/log4j-1.2.8.jar:web/WEB-INF/lib/commons-codec-1.7.jar:web/WEB-INF/classes org.greenstone.gsdl3.util.ModifyUsersDB web/etc/usersDB/ <username or 'ALL'> [options specifying user fields to change, e.g.: password=me!] 30 31 31 32 Don't forget to stop the networked derby server again at the end, if you had started it: ant stop-derby 32 33 [[ OLD METHOD ]] 33 34 or if using embedded derby, ensure that tomcat is stopped, then run: 34 35 java -cp /full/path/to/GS3/web/WEB-INF/lib/gsdl3.jar:/full/path/to/GS3/web/WEB-INF/lib/derby.jar org.greenstone.gsdl3.util.ModifyUsersDB web/etc/usersDB/ … … 44 45 if (args.length < 3) 45 46 { // at minimum one field belonging to a username has to be changed 46 System.out.println("Usage: java org.greenstone.gsdl3.ModifyUsersDB <full_path_of_the_usersDB> <username > [-noAdd] [password=pwd] [groups=grp] [accounstatus=status] [comment=cmt] [email=address]");47 System.out.println("Usage: java org.greenstone.gsdl3.ModifyUsersDB <full_path_of_the_usersDB> <username|ALL> [-noAdd] [password=pwd] [groups=grp] [addgroups=grp] [accounstatus=status] [comment=cmt] [email=address]"); 47 48 System.exit(0); 48 49 } … … 51 52 String username = args[1]; 52 53 53 String password = ""; 54 String groups = ""; 55 String accountstatus = "true"; 56 String comment = ""; 57 String email = ""; 54 String password = null; 55 String groups = null; 56 String addgroups = null; 57 String accountstatus = null; 58 String comment = null; 59 String email = null; 58 60 59 61 boolean noAdd = false; … … 69 71 if (password.length() < PWD_MIN_LENGTH || password.length() > PWD_MAX_LENGTH) 70 72 { 71 if (!password.equals("")) 72 { 73 System.out.println("Password not updated. It should be between " + PWD_MIN_LENGTH + " and " + PWD_MAX_LENGTH + " characters (inclusive)."); 74 } 73 System.out.println("Password not updated. It should be between " + PWD_MIN_LENGTH + " and " + PWD_MAX_LENGTH + " characters (inclusive)."); 74 75 password = null; 75 76 } 76 77 else … … 86 87 { 87 88 groups = args[i].substring("groups=".length()); 88 } 89 groups = UserTermInfo.expandGroups(groups); 90 } 91 else if (args[i].startsWith("addgroups=")) 92 { 93 addgroups = args[i].substring("addgroups=".length()); 94 addgroups = UserTermInfo.expandGroups(addgroups); 95 } 89 96 else if (args[i].startsWith("accountstatus=")) 90 97 { … … 109 116 } 110 117 118 if (groups != null && addgroups != null) { 119 System.err.println("You can't use groups and addgroup at the same time"); 120 System.exit(0); 121 } 111 122 // find the user to modify 112 123 DerbyWrapper dw = new DerbyWrapper(usersDB); 113 UserQueryResult findUserResult = dw.findUser(username); 114 115 if (findUserResult == null) 116 { 117 if (noAdd) 118 { 119 System.out.println("Failed to update user. Cannot find user " + username + " in " + usersDB + " database."); 120 } 121 else 122 { // add new user 123 groups = UserTermInfo.expandGroups(groups); 124 //System.err.println("**** Trying to add user: "); 125 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email); 126 dw.addUser(username, password, groups, accountstatus, comment, email); 127 } 124 if (username.equals("ALL")) { 125 // modify all users 126 UserQueryResult findUserResult = dw.findUser(null, null);// this returns all users 127 Iterator<UserTermInfo> it = findUserResult.getUserTerms().iterator(); 128 while (it.hasNext()) { 129 UserTermInfo uti = it.next(); 130 String un = uti.getUsername(); 131 modifyUser(dw, uti, un, password, groups, addgroups, accountstatus, comment, email); 132 } 133 } else { 134 UserQueryResult findUserResult = dw.findUser(username, null); 135 136 if (findUserResult == null) 137 { 138 if (noAdd) 139 { 140 System.out.println("Failed to update user. Cannot find user " + username + " in " + usersDB + " database."); 141 } 142 else 143 { // add new user 144 145 //System.err.println("**** Trying to add user: "); 146 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email); 147 148 if (password == null) { 149 System.err.println("cannot add a user without a password"); 150 151 } else { 152 if (groups == null) { 153 if (addgroups != null) { 154 groups = addgroups; 155 } else { 156 groups = ""; 157 } 158 } 159 if (accountstatus == null) { 160 accountstatus = "true"; 161 } 162 if (comment == null) { 163 comment = ""; 164 } 165 if (email == null) { 166 email = ""; 167 } 168 System.out.println("Adding new user: un=" + username + ", pw=" + password + ", groups=" + groups + ", status=" + accountstatus + ", comment=" + comment + ", email=" + email); 169 dw.addUser(username, password, groups, accountstatus, comment, email); 170 } 171 } 172 } 173 else 174 { // modify existing user data 175 UserTermInfo user = findUserResult.getUserTerms().get(0); 176 modifyUser(dw, user, username, password, groups, addgroups, accountstatus, comment, email); 177 } 178 128 179 } 129 else130 { // modify existing user data131 132 // in case any of the other fields are not specified, get fallbacks from the database133 UserTermInfo user = findUserResult.getUserTerms().get(0);134 135 if (password.equals(""))136 {137 password = user.getPassword(); // already stored hashed-and-hexed in DB138 }139 if (groups.equals(""))140 {141 // groups should be expandedGroups because we no longer store the groups in userDB142 // as user-entered or compacted, but as programmatically expanded.143 // This allows HttpServletRequest.isUserInRole() to now automatically retrieve the144 // expandedGroups list of a user to check collectionConfig.xml security elements against.145 146 groups = user.getExpandedGroups();147 } else {148 groups = UserTermInfo.expandGroups(groups); // ensure groups are stored expanded in userDB149 }150 if (accountstatus.equals(""))151 {152 accountstatus = user.getAccountStatus().equals("") ? "true" : user.getAccountStatus();153 }154 if (comment.equals(""))155 {156 comment = user.getComment();157 }158 if (email.equals(""))159 {160 email = user.getEmail();161 }162 163 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);164 dw.modifyUserInfo(username, password, groups, accountstatus, comment, email); // other than username and pwd, remaining fields are allowed to be ""165 }166 180 167 181 dw.closeDatabase(); 168 182 169 183 } 184 185 public static void modifyUser(DerbyWrapper dw, UserTermInfo user, String username, String password, String groups, String addgroups, String accountstatus, String comment, String email) { 186 187 if (groups == null && addgroups != null) { 188 groups = user.getExpandedGroups(); // get the groups from db, as we want to add on to what is already there 189 } 190 if (addgroups != null) { 191 if (!groups.equals("")) { 192 groups += ","; 193 } 194 groups += addgroups; 195 } 196 System.out.println("Modifying existing user: un=" + username + ", pw=" + password + ", groups=" + groups + ", status=" + accountstatus + ", comment=" + comment + ", email=" + email); 197 dw.modifyUserInfo(username, password, groups, accountstatus, comment, email); 198 } 170 199 } 200 201
Note:
See TracChangeset
for help on using the changeset viewer.