source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/ModifyUsersDB.java@ 30196

Last change on this file since 30196 was 30196, checked in by ak19, 9 years ago

Fixes to do with networked Derby: port and host number changes propagated from build.props to global.props are now at last picked up by DerbyWrapper.java by build.xml setting a system property (-Dgsdl3.writablehome), which is then used to force load Global.Properties if it's not loaded yet. It won't be loaded when launching the ant update-userdb cmd which runs ModifyUsersDB.java. Or if running ModifyUsersDB.java, txt2usersDB.java or usersDB2txt from the cmdline.

File size: 5.6 KB
Line 
1/*
2 * txt2usersDB.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.sql.SQLException;
22import org.greenstone.gsdl3.service.Authentication;
23//import org.greenstone.admin.guiext.PropertiesStep;
24
25/**
26 To run this from the command-line, first make sure that the networked derby server is running (ant start-derby),
27 then run:
28
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
31 Don't forget to stop the networked derby server again at the end, if you had started it: ant stop-derby
32
33 or if using embedded derby, ensure that tomcat is stopped, then run:
34 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/
35*/
36public class ModifyUsersDB
37{
38 public static int PWD_MIN_LENGTH = 3;
39 public static int PWD_MAX_LENGTH = 20;
40
41 public static void main(String[] args) throws SQLException
42 {
43
44 if (args.length < 3)
45 { // 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.exit(0);
48 }
49
50 String usersDB = args[0];
51 String username = args[1];
52
53 String password = "";
54 String groups = "";
55 String accountstatus = "true";
56 String comment = "";
57 String email = "";
58
59 boolean noAdd = false;
60
61 // If the user specifically sets any of the fields on the cmdline, they'll be overwritten in the db,
62 // even if the user had set them to empty. Except the password which must be between PWD_MIN_LENGTH and PWD_MAX_LENGTH characters.
63 for (int i = 2; i < args.length; i++)
64 {
65 if (args[i].startsWith("password="))
66 {
67 password = args[i].substring("password=".length());
68
69 if (password.length() < PWD_MIN_LENGTH || password.length() > PWD_MAX_LENGTH)
70 {
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 }
75 }
76 else
77 {
78 // Use the same encryption technique used by the Admin Authentication page
79 // This ensures that the password generated for a string remains consistent
80 //System.err.println("**** Password entered was: " + password);
81 password = Authentication.hashPassword(password);
82 }
83
84 }
85 else if (args[i].startsWith("groups="))
86 {
87 groups = args[i].substring("groups=".length());
88 }
89 else if (args[i].startsWith("accountstatus="))
90 {
91 accountstatus = args[i].substring("accountstatus=".length());
92 }
93 else if (args[i].startsWith("status="))
94 {
95 accountstatus = args[i].substring("status=".length());
96 }
97 else if (args[i].startsWith("comment="))
98 {
99 comment = args[i].substring("comment=".length());
100 }
101 else if (args[i].startsWith("email="))
102 {
103 email = args[i].substring("email=".length());
104 }
105 else if (args[i].equals("-noAdd"))
106 {
107 noAdd = true;
108 }
109 }
110
111 // find the user to modify
112 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 //System.err.println("**** Trying to add user: ");
124 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
125 dw.addUser(username, password, groups, accountstatus, comment, email);
126 }
127 }
128 else
129 { // modify existing user data
130
131 // in case any of the other fields are not specified, get fallbacks from the database
132 UserTermInfo user = findUserResult.getUserTerms().get(0);
133
134 if (password.equals(""))
135 {
136 password = user.password; // already stored hashed-and-hexed in DB
137 }
138 if (groups.equals(""))
139 {
140 groups = user.groups;
141 }
142 if (accountstatus.equals(""))
143 {
144 accountstatus = user.accountstatus.equals("") ? "true" : user.accountstatus;
145 }
146 if (comment.equals(""))
147 {
148 comment = user.comment;
149 }
150 if (email.equals(""))
151 {
152 email = user.email;
153 }
154
155 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
156 dw.modifyUserInfo(username, password, groups, accountstatus, comment, email); // other than username and pwd, remaining fields are allowed to be ""
157 }
158
159 dw.closeDatabase();
160
161 }
162}
Note: See TracBrowser for help on using the repository browser.