diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/file/Parameters.java b/authentication/source/java/ch/systemsx/cisd/authentication/file/Parameters.java index 321e3118b12401ddb1cd2dc047c11fbd90319ca4..a2e97ff8bf02291e325660149824019ae8e5d38c 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/file/Parameters.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/file/Parameters.java @@ -198,21 +198,27 @@ final class Parameters } /** - * Returns the first name of the user, or <code>null</code>. + * Returns the first name of the user, or an empty string otherwise. */ - final String tryGetFirstName() + final String getFirstName() { - return firstNameOrNull; + return firstNameOrNull == null ? "" : firstNameOrNull; } - final String tryGetLastName() + /** + * Returns the last name of the user, or an empty string otherwise. + */ + final String getLastName() { - return lastNameOrNull; + return lastNameOrNull == null ? "" : lastNameOrNull; } - final String tryGetEmail() + /** + * Returns the email of the user, or an empty string otherwise. + */ + final String getEmail() { - return emailOrNull; + return emailOrNull == null ? "" : emailOrNull; } /** diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/file/PasswordEditorCommand.java b/authentication/source/java/ch/systemsx/cisd/authentication/file/PasswordEditorCommand.java index cdcf17d04e05b3f83e1436355cd2f6219a8c5128..9850973edc3cc5028e133808001f91a70957fcdf 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/file/PasswordEditorCommand.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/file/PasswordEditorCommand.java @@ -31,6 +31,10 @@ import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; public class PasswordEditorCommand { + private static final String ENTER_NEW_PASSWORD_MSG = "Enter new password: "; + + private static final String ENTER_PASSWORD_MSG = "Enter the user's password: "; + private final static File PASSWORD_FILE = new File("etc/passwd"); private static ConsoleReader consoleReader; @@ -52,11 +56,11 @@ public class PasswordEditorCommand return consoleReader; } - private final static String readPassword() + private final static String readPassword(final String message) { try { - return getConsoleReader().readLine("Enter new password: ", Character.valueOf('*')); + return getConsoleReader().readLine(message, Character.valueOf('*')); } catch (IOException ex) { System.err.println("Error reading password (" + ex.getMessage() + ")."); @@ -111,11 +115,11 @@ public class PasswordEditorCommand password = params.tryGetPassword(); } else { - password = readPassword(); + password = readPassword(ENTER_NEW_PASSWORD_MSG); } final UserEntry user = - new UserEntry(params.getUserId(), params.tryGetFirstName(), params - .tryGetLastName(), params.tryGetEmail(), password); + new UserEntry(params.getUserId(), params.getFirstName(), params + .getLastName(), params.getEmail(), password); userStore.addOrUpdateUser(user); break; } @@ -129,24 +133,24 @@ public class PasswordEditorCommand System.exit(1); return; // Fake: convince compiler that it is save to dereference userOrNull } - if (params.tryGetFirstName() != null) + if (params.getFirstName() != null) { - userOrNull.setFirstName(params.tryGetFirstName()); + userOrNull.setFirstName(params.getFirstName()); } - if (params.tryGetLastName() != null) + if (params.getLastName() != null) { - userOrNull.setLastName(params.tryGetLastName()); + userOrNull.setLastName(params.getLastName()); } - if (params.tryGetEmail() != null) + if (params.getEmail() != null) { - userOrNull.setEmail(params.tryGetEmail()); + userOrNull.setEmail(params.getEmail()); } if (params.tryGetPassword() != null) { userOrNull.setPassword(params.tryGetPassword()); } else if (params.isChangePassword()) { - userOrNull.setPassword(readPassword()); + userOrNull.setPassword(readPassword(ENTER_NEW_PASSWORD_MSG)); } userStore.addOrUpdateUser(userOrNull); break; @@ -194,7 +198,7 @@ public class PasswordEditorCommand System.exit(1); return; // Fake: convince compiler that it is save to dereference userOrNull } - final String password = readPassword(); + final String password = readPassword(ENTER_PASSWORD_MSG); if (userStore.isPasswordCorrect(userId, password)) { System.out.printf("User '%s' successfully authenticated.\n", userId);