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

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

Fixing bug in GS3 installer that didn't use the longer password provided, despite the installer now allowing more than 8. The problem was bigger and manifold. 1. Fixed ModifyUsersDB to allow up to and incl 20 chars instead of 8. 2. Fixed the DerbyWrapper classes since the ant config-admin and config-user tasks had stopped working when global.properties was consulted for the derbyserver name and port. Now providing fallbacks again for the installer. 3. build.xml may no longer need to stop tomcat then modify the derby db and then start tomcat again because we're no longer using an embedded derby, but because we're using a networked derby, modifying the usersdb now didn't work unless greenstone (actually derby) was running. Changed the config targets to allow modifying admin and user data even if greenstone is not running, by making the update-userdb target that's called by these two tasks ensure the derbyserver is running before modifying the userdb, and stopping it if it was started up by the target.

File size: 5.0 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// To run this from the command-line, first make sure that the tomcat server is stopped, then run:
26// 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.usersDB2txt web/etc/usersDB/
27public class ModifyUsersDB
28{
29 public static int PWD_MIN_LENGTH = 3;
30 public static int PWD_MAX_LENGTH = 20;
31
32 public static void main(String[] args) throws SQLException
33 {
34
35 if (args.length < 3)
36 { // at minimum one field belonging to a username has to be changed
37 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]");
38 System.exit(0);
39 }
40
41 String usersDB = args[0];
42 String username = args[1];
43
44 String password = "";
45 String groups = "";
46 String accountstatus = "true";
47 String comment = "";
48 String email = "";
49
50 boolean noAdd = false;
51
52 // If the user specifically sets any of the fields on the cmdline, they'll be overwritten in the db,
53 // even if the user had set them to empty. Except the password which must be between PWD_MIN_LENGTH and PWD_MAX_LENGTH characters.
54 for (int i = 2; i < args.length; i++)
55 {
56 if (args[i].startsWith("password="))
57 {
58 password = args[i].substring("password=".length());
59
60 if (password.length() < PWD_MIN_LENGTH || password.length() > PWD_MAX_LENGTH)
61 {
62 if (!password.equals(""))
63 {
64 System.out.println("Password not updated. It should be between " + PWD_MIN_LENGTH + " and " + PWD_MAX_LENGTH + " characters (inclusive).");
65 }
66 }
67 else
68 {
69 // Use the same encryption technique used by the Admin Authentication page
70 // This ensures that the password generated for a string remains consistent
71 //System.err.println("**** Password entered was: " + password);
72 password = Authentication.hashPassword(password);
73 }
74
75 }
76 else if (args[i].startsWith("groups="))
77 {
78 groups = args[i].substring("groups=".length());
79 }
80 else if (args[i].startsWith("accountstatus="))
81 {
82 accountstatus = args[i].substring("accountstatus=".length());
83 }
84 else if (args[i].startsWith("status="))
85 {
86 accountstatus = args[i].substring("status=".length());
87 }
88 else if (args[i].startsWith("comment="))
89 {
90 comment = args[i].substring("comment=".length());
91 }
92 else if (args[i].startsWith("email="))
93 {
94 email = args[i].substring("email=".length());
95 }
96 else if (args[i].equals("-noAdd"))
97 {
98 noAdd = true;
99 }
100 }
101
102 // find the user to modify
103 DerbyWrapper dw = new DerbyWrapper(usersDB);
104 UserQueryResult findUserResult = dw.findUser(username);
105
106 if (findUserResult == null)
107 {
108 if (noAdd)
109 {
110 System.out.println("Failed to update user. Cannot find user " + username + " in " + usersDB + " database.");
111 }
112 else
113 { // add new user
114 //System.err.println("**** Trying to add user: ");
115 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
116 dw.addUser(username, password, groups, accountstatus, comment, email);
117 }
118 }
119 else
120 { // modify existing user data
121
122 // in case any of the other fields are not specified, get fallbacks from the database
123 UserTermInfo user = findUserResult.getUserTerms().get(0);
124
125 if (password.equals(""))
126 {
127 password = user.password; // already stored hashed-and-hexed in DB
128 }
129 if (groups.equals(""))
130 {
131 groups = user.groups;
132 }
133 if (accountstatus.equals(""))
134 {
135 accountstatus = user.accountstatus.equals("") ? "true" : user.accountstatus;
136 }
137 if (comment.equals(""))
138 {
139 comment = user.comment;
140 }
141 if (email.equals(""))
142 {
143 email = user.email;
144 }
145
146 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
147 dw.modifyUserInfo(username, password, groups, accountstatus, comment, email); // other than username and pwd, remaining fields are allowed to be ""
148 }
149
150 dw.closeDatabase();
151
152 }
153}
Note: See TracBrowser for help on using the repository browser.