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