From dd40f5ff3c0a27c338c53e37ac14ef8af7f1a2c6 Mon Sep 17 00:00:00 2001 From: "Mark H. Wood" Date: Fri, 13 Aug 2021 15:57:52 -0400 Subject: [PATCH] Add option to change user's password. #3363 --- .../org/dspace/eperson/EPersonCLITool.java | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/eperson/EPersonCLITool.java b/dspace-api/src/main/java/org/dspace/eperson/EPersonCLITool.java index aee2e7a082..de3609c5d9 100644 --- a/dspace-api/src/main/java/org/dspace/eperson/EPersonCLITool.java +++ b/dspace-api/src/main/java/org/dspace/eperson/EPersonCLITool.java @@ -13,6 +13,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.SQLException; +import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -57,8 +58,11 @@ public class EPersonCLITool { private static final Option OPT_NEW_EMAIL = new Option("i", "newEmail", true, "new email address"); private static final Option OPT_NEW_NETID = new Option("I", "newNetid", true, "new network ID"); + private static final Option OPT_NEW_PASSWORD + = new Option("w", "newPassword", false, "prompt for new password"); - private static final EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService(); + private static final EPersonService ePersonService + = EPersonServiceFactory.getInstance().getEPersonService(); /** * Default constructor @@ -120,6 +124,8 @@ public class EPersonCLITool { System.err.println(ex.getMessage()); } } + + System.exit(status); } /** @@ -177,11 +183,11 @@ public class EPersonCLITool { EPerson eperson = null; try { eperson = ePersonService.create(context); - } catch (SQLException ex) { + } catch (SQLException | AuthorizeException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; - } catch (AuthorizeException ex) { /* XXX SNH */ } + } eperson.setCanLogIn(true); eperson.setSelfRegistered(false); @@ -204,11 +210,11 @@ public class EPersonCLITool { try { ePersonService.update(context, eperson); System.out.printf("Created EPerson %s\n", eperson.getID().toString()); - } catch (SQLException ex) { + } catch (SQLException | AuthorizeException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; - } catch (AuthorizeException ex) { /* XXX SNH */ } + } return 0; } @@ -315,6 +321,7 @@ public class EPersonCLITool { options.addOption(OPT_CAN_LOGIN); options.addOption(OPT_NEW_EMAIL); options.addOption(OPT_NEW_NETID); + options.addOption(OPT_NEW_PASSWORD); options.addOption("h", "help", false, "explain --modify options"); @@ -334,11 +341,14 @@ public class EPersonCLITool { // Modify! EPerson eperson = null; + String userName = null; try { if (command.hasOption(OPT_NETID.getOpt())) { - eperson = ePersonService.findByNetid(context, command.getOptionValue(OPT_NETID.getOpt())); + userName = command.getOptionValue(OPT_NETID.getOpt()); + eperson = ePersonService.findByNetid(context, userName); } else if (command.hasOption(OPT_EMAIL.getOpt())) { - eperson = ePersonService.findByEmail(context, command.getOptionValue(OPT_EMAIL.getOpt())); + userName = command.getOptionValue(OPT_EMAIL.getOpt()); + eperson = ePersonService.findByEmail(context, userName); } else { System.err.println("No EPerson selected"); return 1; @@ -361,6 +371,24 @@ public class EPersonCLITool { eperson.setNetid(command.getOptionValue(OPT_NEW_NETID.getOpt())); modified = true; } + if (command.hasOption(OPT_NEW_PASSWORD.getOpt())) { + // TODO prompt, collect password, verify + char[] password = System.console() + .readPassword("Enter new password for user %s", userName); + char[] password2 = System.console() + .readPassword("Enter new password again to verify"); + if (Arrays.equals(password, password2)) { + PasswordHash newHashedPassword = new PasswordHash(String.valueOf(password)); + Arrays.fill(password, '\0'); // Obliterate cleartext passwords + Arrays.fill(password2, '\0'); + eperson.setPassword(newHashedPassword.getHashString()); + eperson.setSalt(newHashedPassword.getSaltString()); + eperson.setDigestAlgorithm(newHashedPassword.getAlgorithm()); + modified = true; + } else { + System.err.println("Passwords do not match. Password not set"); + } + } if (command.hasOption(OPT_GIVENNAME.getOpt())) { eperson.setFirstName(context, command.getOptionValue(OPT_GIVENNAME.getOpt())); modified = true; @@ -387,15 +415,16 @@ public class EPersonCLITool { eperson.setCanLogIn(Boolean.valueOf(command.getOptionValue(OPT_CAN_LOGIN.getOpt()))); modified = true; } + if (modified) { try { ePersonService.update(context, eperson); System.out.printf("Modified EPerson %s\n", eperson.getID().toString()); - } catch (SQLException ex) { + } catch (SQLException | AuthorizeException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; - } catch (AuthorizeException ex) { /* XXX SNH */ } + } } else { System.out.println("No changes."); }