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

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

Working version of hierarchical groups: now the usersDB stores expandedGroups instead of user-entered groups in the 'roles' table. The expandedGroups listing is therefore now consulted behind-the-scenes/automatically by HttpServletRequest.isUserInRole(), which is configured (in tomcat servlet configuration file greenstone3.xml) to query the roles table of the userDB. The new UserTermInfo.compactGroups() function takes care that the display value of the groups listing in the administration pages is the compacted version: it's not exactly the same as the user-entered value as the compactedGroups listing is in natural (alphabetic/ASCII) order and with duplicates removed.

File size: 8.3 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, UserTermInfo.expandGroups(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 expandedGroups because we no longer store the groups in userDB
188 // as user-entered or compacted, but as programmatically expanded.
189 // This allows HttpServletRequest.isUserInRole() to now automatically retrieve the
190 // expandedGroups list of a user to check collectionConfig.xml security elements against.
191
192 groups = groups.equals("") ? user.getExpandedGroups() : UserTermInfo.expandGroups(groups);
193 accountstatus = accountstatus.equals("") ? user.getAccountStatus() : accountstatus;
194 comment = comment.equals("") ? user.getComment() : comment;
195
196 if (email == null)
197 { // special checking for backwards compatibility since old DB did not have email field
198 email = "";
199 }
200 if (user.getEmail() == null)
201 {
202 user.setEmail("");
203 }
204 if (email.equals(""))
205 {
206 email = user.getEmail();
207 }
208
209 //System.err.println("**** Password: " + password);
210 //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
211 dw.modifyUserInfo(username, password, groups, accountstatus, comment, email);
212 }
213
214 username = null;
215 password = null;
216 groups = null;
217 accountstatus = null;
218 comment = null;
219 email = null;
220 //dw.connectDatabase(args[1],false); // should this be closeDatabase()????
221 dw.closeDatabase();
222 }
223 }
224
225 // only true back when when hashed passwords weren't being converted to hex
226 //else { // encrypted passwords can span multiple lines for some reason
227 // assume that is the case here
228 //if(password != null) {
229 // password = password + "\n" + str;
230 // }
231 //}
232
233 }
234 //dw.closeDatabase();
235 in.close();
236 }
237 catch (IOException e)
238 {
239 }
240 }
241}
Note: See TracBrowser for help on using the repository browser.