From dddb61aa7718b3d6a14c69eb2465034ec3f32638 Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Wed, 6 May 2015 15:53:03 +0200 Subject: [PATCH] On init of admin user use email from SSH key file as default email If DEVELOPMENT_BECOME_ANY_ACCOUNT is used there is an init step for creating the initial admin user. When an SSH key file is given, use the comment of the key as default for the email address if it is a valid email. Change-Id: Ic91442d757065725b0ee6964f15b13ed1f8316fa Signed-off-by: Edwin Kempin --- .../google/gerrit/pgm/init/InitAdminUser.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitAdminUser.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitAdminUser.java index 7a19d7e4e2..e0cb4c46e6 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitAdminUser.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitAdminUser.java @@ -76,9 +76,9 @@ public class InitAdminUser implements InitStep { Account.Id id = new Account.Id(db.nextAccountId()); String username = ui.readString("admin", "username"); String name = ui.readString("Administrator", "name"); - String email = readEmail(); String httpPassword = ui.readString("secret", "HTTP password"); AccountSshKey sshKey = readSshKey(id); + String email = readEmail(sshKey); AccountExternalId extUser = new AccountExternalId(id, new AccountExternalId.Key( @@ -116,11 +116,22 @@ public class InitAdminUser implements InitStep { } } - private String readEmail() { - String email = ui.readString("admin@example.com", "email"); + private String readEmail(AccountSshKey sshKey) { + String defaultEmail = "admin@example.com"; + if (sshKey != null && sshKey.getComment() != null) { + String c = sshKey.getComment().trim(); + if (EmailValidator.getInstance().isValid(c)) { + defaultEmail = c; + } + } + return readEmail(defaultEmail); + } + + private String readEmail(String defaultEmail) { + String email = ui.readString(defaultEmail, "email"); if (email != null && !EmailValidator.getInstance().isValid(email)) { ui.message("error: invalid email address\n"); - return readEmail(); + return readEmail(defaultEmail); } return email; }