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