001: /*
002: * CreateAdministrator.java
003: *
004: * Version: $Revision: 1947 $
005: *
006: * Date: $Date: 2007-05-18 08:50:29 -0500 (Fri, 18 May 2007) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.administer;
041:
042: import java.io.BufferedReader;
043: import java.io.InputStreamReader;
044: import java.util.Locale;
045:
046: import org.apache.commons.cli.CommandLine;
047: import org.apache.commons.cli.CommandLineParser;
048: import org.apache.commons.cli.Options;
049: import org.apache.commons.cli.PosixParser;
050:
051: import org.dspace.core.ConfigurationManager;
052: import org.dspace.core.Context;
053: import org.dspace.core.I18nUtil;
054: import org.dspace.eperson.EPerson;
055: import org.dspace.eperson.Group;
056:
057: /**
058: * A command-line tool for creating an initial administrator for setting up a
059: * DSpace site. Prompts for an e-mail address, last name, first name and
060: * password from standard input. An administrator group is then created and the
061: * data passed in used to create an e-person in that group.
062: * <P>
063: * Alternatively, it can be used to take the email, first name, last name and
064: * desired password as arguments thus:
065: *
066: * CreateAdministrator -e [email] -f [first name] -l [last name] -p [password]
067: *
068: * This is particularly convenient for automated deploy scripts that require an
069: * initial administrator, for example, before deployment can be completed
070: *
071: * @author Robert Tansley
072: * @author Richard Jones
073: *
074: * @version $Revision: 1947 $
075: */
076: public class CreateAdministrator {
077: /** DSpace Context object */
078: private Context context;
079:
080: /**
081: * For invoking via the command line. If called with no command line arguments,
082: * it will negotiate with the user for the administrator details
083: *
084: * @param argv
085: * command-line arguments
086: */
087: public static void main(String[] argv) throws Exception {
088: CommandLineParser parser = new PosixParser();
089: Options options = new Options();
090:
091: CreateAdministrator ca = new CreateAdministrator();
092:
093: options.addOption("e", "email", true,
094: "administrator email address");
095: options.addOption("f", "first", true,
096: "administrator first name");
097: options
098: .addOption("l", "last", true,
099: "administrator lastt name");
100: options.addOption("c", "language", true,
101: "administrator language");
102: options.addOption("p", "password", true,
103: "administrator password");
104:
105: CommandLine line = parser.parse(options, argv);
106:
107: if (line.hasOption("e") && line.hasOption("f")
108: && line.hasOption("l") && line.hasOption("c")
109: && line.hasOption("p")) {
110: ca.createAdministrator(line.getOptionValue("e"), line
111: .getOptionValue("f"), line.getOptionValue("l"),
112: line.getOptionValue("c"), line.getOptionValue("p"));
113: } else {
114: ca.negotiateAdministratorDetails();
115: }
116: }
117:
118: /**
119: * constructor, which just creates and object with a ready context
120: *
121: * @throws Exception
122: */
123: private CreateAdministrator() throws Exception {
124: context = new Context();
125: }
126:
127: /**
128: * Method which will negotiate with the user via the command line to
129: * obtain the administrator's details
130: *
131: * @throws Exception
132: */
133: private void negotiateAdministratorDetails() throws Exception {
134: // For easier reading of typing
135: BufferedReader input = new BufferedReader(
136: new InputStreamReader(System.in));
137:
138: System.out.println("Creating an initial administrator account");
139:
140: boolean dataOK = false;
141:
142: String email = null;
143: String firstName = null;
144: String lastName = null;
145: String password1 = null;
146: String password2 = null;
147: String language = I18nUtil.DEFAULTLOCALE.getLanguage();
148:
149: while (!dataOK) {
150: System.out.print("E-mail address: ");
151: System.out.flush();
152:
153: email = input.readLine().trim();
154:
155: System.out.print("First name: ");
156: System.out.flush();
157:
158: firstName = input.readLine().trim();
159:
160: System.out.print("Last name: ");
161: System.out.flush();
162:
163: lastName = input.readLine().trim();
164:
165: if (ConfigurationManager
166: .getProperty("webui.supported.locales") != null) {
167: System.out
168: .println("Select one of the following languages: "
169: + ConfigurationManager
170: .getProperty("webui.supported.locales"));
171: System.out.print("Language: ");
172: System.out.flush();
173:
174: language = input.readLine().trim();
175: language = I18nUtil.getSupportedLocale(
176: new Locale(language)).getLanguage();
177: }
178:
179: System.out
180: .println("WARNING: Password will appear on-screen.");
181: System.out.print("Password: ");
182: System.out.flush();
183:
184: password1 = input.readLine().trim();
185:
186: System.out.print("Again to confirm: ");
187: System.out.flush();
188:
189: password2 = input.readLine().trim();
190:
191: if (!password1.equals("") && password1.equals(password2)) {
192: // password OK
193: System.out
194: .print("Is the above data correct? (y or n): ");
195: System.out.flush();
196:
197: String s = input.readLine().trim();
198:
199: if (s.toLowerCase().startsWith("y")) {
200: dataOK = true;
201: }
202: } else {
203: System.out.println("Passwords don't match");
204: }
205: }
206:
207: // if we make it to here, we are ready to create an administrator
208: createAdministrator(email, firstName, lastName, language,
209: password1);
210: }
211:
212: /**
213: * Create the administrator with the given details. If the user
214: * already exists then they are simply upped to administrator status
215: *
216: * @param email the email for the user
217: * @param first user's first name
218: * @param last user's last name
219: * @param ps desired password
220: *
221: * @throws Exception
222: */
223: private void createAdministrator(String email, String first,
224: String last, String language, String pw) throws Exception {
225: // Of course we aren't an administrator yet so we need to
226: // circumvent authorisation
227: context.setIgnoreAuthorization(true);
228:
229: // Find administrator group
230: Group admins = Group.find(context, 1);
231:
232: if (admins == null) {
233: throw new Exception("Error, no admin group (group 1) found");
234: }
235:
236: // Create the administrator e-person
237: EPerson eperson = EPerson.findByEmail(context, email);
238:
239: // check if the email belongs to a registered user,
240: // if not create a new user with this email
241: if (eperson == null) {
242: eperson = EPerson.create(context);
243: eperson.setEmail(email);
244: eperson.setCanLogIn(true);
245: eperson.setRequireCertificate(false);
246: eperson.setSelfRegistered(false);
247: }
248:
249: eperson.setLastName(last);
250: eperson.setFirstName(first);
251: eperson.setLanguage(language);
252: eperson.setPassword(pw);
253: eperson.update();
254:
255: admins.addMember(eperson);
256: admins.update();
257:
258: context.complete();
259:
260: System.out.println("Administrator account created");
261: }
262: }
|