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

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

Jeremy found that status could be used instead of accountstatus for the usersDB. Adding a further clause for handling that.

File size: 4.8 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("status="))
83 {
84 accountstatus = args[i].substring("status=".length());
85 }
86 else if (args[i].startsWith("comment="))
87 {
88 comment = args[i].substring("comment=".length());
89 }
90 else if (args[i].startsWith("email="))
91 {
92 email = args[i].substring("email=".length());
93 }
94 else if (args[i].equals("-noAdd"))
95 {
96 noAdd = true;
97 }
98 }
99
100 // find the user to modify
101 DerbyWrapper dw = new DerbyWrapper(usersDB);
102 UserQueryResult findUserResult = dw.findUser(username);
103
104 if (findUserResult == null)
105 {
106 if (noAdd)
107 {
108 System.out.println("Failed to update user. Cannot find user " + username + " in " + usersDB + " database.");
109 }
110 else
111 { // add new user
112 //System.err.println("**** Trying to add user: ");
113 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
114 dw.addUser(username, password, groups, accountstatus, comment, email);
115 }
116 }
117 else
118 { // modify existing user data
119
120 // in case any of the other fields are not specified, get fallbacks from the database
121 UserTermInfo user = findUserResult.getUserTerms().get(0);
122
123 if (password.equals(""))
124 {
125 password = user.password; // already stored hashed-and-hexed in DB
126 }
127 if (groups.equals(""))
128 {
129 groups = user.groups;
130 }
131 if (accountstatus.equals(""))
132 {
133 accountstatus = user.accountstatus.equals("") ? "true" : user.accountstatus;
134 }
135 if (comment.equals(""))
136 {
137 comment = user.comment;
138 }
139 if (email.equals(""))
140 {
141 email = user.email;
142 }
143
144 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
145 dw.modifyUserInfo(username, password, groups, accountstatus, comment, email); // other than username and pwd, remaining fields are allowed to be ""
146 }
147
148 dw.closeDatabase();
149
150 }
151}
Note: See TracBrowser for help on using the repository browser.