1 | /*
|
---|
2 | * ModifyUsersDB.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 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import java.sql.SQLException;
|
---|
22 | import java.util.Iterator;
|
---|
23 | import org.greenstone.gsdl3.service.Authentication;
|
---|
24 | //import org.greenstone.admin.guiext.PropertiesStep;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | To run this from the command-line, first make sure that the networked derby server is running (ant start-derby),
|
---|
28 | then run:
|
---|
29 |
|
---|
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!]
|
---|
31 |
|
---|
32 | Don't forget to stop the networked derby server again at the end, if you had started it: ant stop-derby
|
---|
33 | [[ OLD METHOD ]]
|
---|
34 | or if using embedded derby, ensure that tomcat is stopped, then run:
|
---|
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/
|
---|
36 | */
|
---|
37 | public class ModifyUsersDB
|
---|
38 | {
|
---|
39 | public static int PWD_MIN_LENGTH = 3;
|
---|
40 | public static int PWD_MAX_LENGTH = 20;
|
---|
41 |
|
---|
42 | public static void main(String[] args) throws SQLException
|
---|
43 | {
|
---|
44 |
|
---|
45 | if (args.length < 3)
|
---|
46 | { // at minimum one field belonging to a username has to be changed
|
---|
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]");
|
---|
48 | System.exit(0);
|
---|
49 | }
|
---|
50 |
|
---|
51 | String usersDB = args[0];
|
---|
52 | String username = args[1];
|
---|
53 |
|
---|
54 | String password = null;
|
---|
55 | String groups = null;
|
---|
56 | String addgroups = null;
|
---|
57 | String accountstatus = null;
|
---|
58 | String comment = null;
|
---|
59 | String email = null;
|
---|
60 |
|
---|
61 | boolean noAdd = false;
|
---|
62 |
|
---|
63 | // If the user specifically sets any of the fields on the cmdline, they'll be overwritten in the db,
|
---|
64 | // even if the user had set them to empty. Except the password which must be between PWD_MIN_LENGTH and PWD_MAX_LENGTH characters.
|
---|
65 | for (int i = 2; i < args.length; i++)
|
---|
66 | {
|
---|
67 | if (args[i].startsWith("password="))
|
---|
68 | {
|
---|
69 | password = args[i].substring("password=".length());
|
---|
70 |
|
---|
71 | if (password.length() < PWD_MIN_LENGTH || password.length() > PWD_MAX_LENGTH)
|
---|
72 | {
|
---|
73 | System.out.println("Password not updated. It should be between " + PWD_MIN_LENGTH + " and " + PWD_MAX_LENGTH + " characters (inclusive).");
|
---|
74 |
|
---|
75 | password = null;
|
---|
76 | }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | // Use the same encryption technique used by the Admin Authentication page
|
---|
80 | // This ensures that the password generated for a string remains consistent
|
---|
81 | //System.err.println("**** Password entered was: " + password);
|
---|
82 | password = Authentication.hashPassword(password);
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|
86 | else if (args[i].startsWith("groups="))
|
---|
87 | {
|
---|
88 | groups = args[i].substring("groups=".length());
|
---|
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 | }
|
---|
96 | else if (args[i].startsWith("accountstatus="))
|
---|
97 | {
|
---|
98 | accountstatus = args[i].substring("accountstatus=".length());
|
---|
99 | }
|
---|
100 | else if (args[i].startsWith("status="))
|
---|
101 | {
|
---|
102 | accountstatus = args[i].substring("status=".length());
|
---|
103 | }
|
---|
104 | else if (args[i].startsWith("comment="))
|
---|
105 | {
|
---|
106 | comment = args[i].substring("comment=".length());
|
---|
107 | }
|
---|
108 | else if (args[i].startsWith("email="))
|
---|
109 | {
|
---|
110 | email = args[i].substring("email=".length());
|
---|
111 | }
|
---|
112 | else if (args[i].equals("-noAdd"))
|
---|
113 | {
|
---|
114 | noAdd = true;
|
---|
115 | }
|
---|
116 | }
|
---|
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 | }
|
---|
122 | // find the user to modify
|
---|
123 | DerbyWrapper dw = new DerbyWrapper(usersDB);
|
---|
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 |
|
---|
179 | }
|
---|
180 |
|
---|
181 | dw.closeDatabase();
|
---|
182 |
|
---|
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 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|