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

Last change on this file since 28965 was 28202, checked in by sjm84, 11 years ago

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

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