source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/txt2usersDB.java@ 35287

Last change on this file since 35287 was 35287, checked in by anupama, 3 years ago

Part 2 of commit on expanded user groups, related to prev commit (revision 35286) which was part 1. In this commit, I think I've identified the only times UserTermInfo.getExpandedGroups() needs to be called: when authentication-ping/ServletRealmCheck is called, which redirect to Authentication.processRemoteAuthentication(). At other times, the original group listing is what's required (for user listing display/modification in GS3 Reader Interface) or is sufficient (to check user is in admin group, the group listing in userDB/originally entered group listing is enough and no expanded list is needed. Maybe origGroups can better be renamed back to groups and getOrigGroups replaced with plain getGroups(), since getExpandedGroups() is the method called in more exceptional circumstances after all.

File size: 8.1 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.io.BufferedReader;
22import java.io.IOException;
23import java.io.File;
24import java.io.FileReader;
25import java.sql.SQLException;
26
27import org.greenstone.gsdl3.service.Authentication;
28
29/**
30 To run this from the command-line, first make sure that the derby networked server is running (ant start-derby),
31 then run:
32
33 java -Dgsdl3.writablehome=/full/path/to/GS3/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/classes org.greenstone.gsdl3.util.txt2usersDB <filename>.txt web/etc/usersDB/ [-append]
34
35 Don't forget to stop the networked derby server again at the end, if you had started it: ant stop-derby
36
37 Or if using embedded derby, ensure that tomcat is stopped, then run:
38 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.txt2usersDB <filename>.txt web/etc/usersDB/ [-append]
39*/
40public class txt2usersDB
41{
42
43 public static void main(String[] args) throws SQLException
44 {
45 boolean appending = false;
46
47 String usage = "Usage: java org.greenstone.gsdl3.txt2usersDB full_path_of_the_text_file full_path_of_the_usersDB [-append]";
48 if (args.length < 2)
49 {
50 System.out.println(usage);
51 System.exit(0);
52 }
53 File txtfile = new File(args[0]);
54 if (!txtfile.exists())
55 {
56 System.out.println("File " + args[0] + " does not exist.");
57 System.out.println(usage);
58 System.exit(0);
59 }
60
61 try
62 {
63 BufferedReader in = new BufferedReader(new FileReader(args[0]));
64 String str;
65 DerbyWrapper dw = new DerbyWrapper(args[1]);
66
67 if (args.length > 2 && args[2].equals("-append"))
68 {
69 appending = true;
70 }
71 else
72 {
73 // no appending, replace existing database: the text file
74 // represents the new database, so delete the existing DB first
75 boolean delete_rows = dw.deleteAllUser();
76 dw.closeDatabase();
77 if (!delete_rows)
78 {
79 System.out.println("Couldn't delete rows of the users table");
80 System.exit(0);
81 }
82 }
83
84 String username = null;
85 String password = null;
86 String groups = null;
87 String accountstatus = null;
88 String comment = null;
89 String email = null;
90
91 while ((str = in.readLine()) != null)
92 {
93 //ystem.out.println(str);
94
95 if (str.indexOf(" = ") != -1)
96 { // works with DerbyWrapper.db2txt() and usersDB2txt.java. Fields listed as: USERNAME = admin
97 String field = str.substring(0, str.indexOf(" = "));
98 if (field.equalsIgnoreCase("email"))
99 {
100 email = str.substring(str.indexOf(" = ") + 3, str.length());
101 }
102 if (field.equalsIgnoreCase("comment"))
103 {
104 comment = str.substring(str.indexOf(" = ") + 3, str.length());
105 }
106 if (field.equalsIgnoreCase("status"))
107 {
108 accountstatus = str.substring(str.indexOf(" = ") + 3, str.length());
109 }
110 if (field.equalsIgnoreCase("groups"))
111 {
112 groups = str.substring(str.indexOf(" = ") + 3, str.length());
113 }
114 if (field.equalsIgnoreCase("password"))
115 {
116 //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length()));
117 password = str.substring(str.indexOf(" = ") + 3, str.length());
118 }
119 if (field.equalsIgnoreCase("username"))
120 {
121 username = str.substring(str.indexOf(" = ") + 3, str.length());
122 }
123 }
124 else if (str.startsWith("<"))
125 { // fields listed as: <username>admin
126 String field = str.substring(1, str.indexOf(">"));
127 if (field.equals("email"))
128 {
129 email = str.substring(str.indexOf(">") + 1, str.length());
130 }
131 if (field.equals("comment"))
132 {
133 comment = str.substring(str.indexOf(">") + 1, str.length());
134 }
135 if (field.equals("enabled") || field.equals("status"))
136 {
137 accountstatus = str.substring(str.indexOf(">") + 1, str.length());
138 }
139 if (field.equals("groups"))
140 {
141 groups = str.substring(str.indexOf(">") + 1, str.length());
142 }
143 if (field.equals("password"))
144 {
145 password = str.substring(str.indexOf(">") + 1, str.length());
146 }
147 if (field.equals("username"))
148 {
149 username = str.substring(str.indexOf(">") + 1, str.length());
150 }
151 }
152 else if (str.equals("----------------------------------------------------------------------") || str.equals("-------------------------------------"))
153 {
154
155 if ((username != null) && (password != null) && (groups != null) && (accountstatus != null) && (comment != null))
156 {
157 dw.connectDatabase(args[1], false);
158
159 // check if it's a new user or already exists in the database
160 UserQueryResult findUserResult = dw.findUser(username);
161
162 if (findUserResult == null)
163 { // add new user
164 if (password.length() >= 3 && password.length() <= 8)
165 { // if not yet encrypted, encrypt first
166 password = Authentication.hashPassword(password);
167 } // if > 8 chars, password for user being added was already encrypted (hashed-and-hexed)
168 dw.addUser(username, password, groups, accountstatus, comment, email);
169 }
170
171 else
172 { // modify existing user
173 // if any of the other fields are not specified, get them from the database
174 UserTermInfo user = findUserResult.getUserTerms().get(0);
175
176 if (password.length() < 3 || password.length() > 8)
177 { // includes empty string case
178 password = user.getPassword();
179 }
180 else
181 { // need to first encrypt (hash-and-hex) the user-entered password
182 // Use the same encryption technique used by the Admin Authentication page
183 // This ensures that the password generated for a string remains consistent
184 password = Authentication.hashPassword(password);
185 }
186
187 // groups should be origGroups and not expandedGroups
188 // As we want to store the groups in DB as entered
189 // and not as programmatically expanded.
190 groups = groups.equals("") ? user.getOrigGroups() : groups;
191 accountstatus = accountstatus.equals("") ? user.getAccountStatus() : accountstatus;
192 comment = comment.equals("") ? user.getComment() : comment;
193
194 if (email == null)
195 { // special checking for backwards compatibility since old DB did not have email field
196 email = "";
197 }
198 if (user.getEmail() == null)
199 {
200 user.setEmail("");
201 }
202 if (email.equals(""))
203 {
204 email = user.getEmail();
205 }
206
207 //System.err.println("**** Password: " + password);
208 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
209 dw.modifyUserInfo(username, password, groups, accountstatus, comment, email);
210 }
211
212 username = null;
213 password = null;
214 groups = null;
215 accountstatus = null;
216 comment = null;
217 email = null;
218 //dw.connectDatabase(args[1],false); // should this be closeDatabase()????
219 dw.closeDatabase();
220 }
221 }
222
223 // only true back when when hashed passwords weren't being converted to hex
224 //else { // encrypted passwords can span multiple lines for some reason
225 // assume that is the case here
226 //if(password != null) {
227 // password = password + "\n" + str;
228 // }
229 //}
230
231 }
232 //dw.closeDatabase();
233 in.close();
234 }
235 catch (IOException e)
236 {
237 }
238 }
239}
Note: See TracBrowser for help on using the repository browser.