Merge "Modify account command: Add option to set preferred email"

This commit is contained in:
David Pursehouse 2014-08-20 07:33:10 +00:00 committed by Gerrit Code Review
commit 0928ce07b3
2 changed files with 35 additions and 0 deletions

View File

@ -7,6 +7,7 @@ gerrit set-account - Change an account's settings.
--
set-account [--full-name <FULLNAME>] [--active|--inactive] \
[--add-email <EMAIL>] [--delete-email <EMAIL> | ALL] \
[--preferred-email <EMAIL>] \
[--add-ssh-key - | <KEY>] \
[--delete-ssh-key - | <KEY> | ALL] \
[--http-password <PASSWORD>] \
@ -67,6 +68,13 @@ This most likely requires double quoting the value, for example
Maybe supplied more than once to remove multiple emails
from an account in a single command execution.
--preferred-email::
Sets the preferred email address for the user's account.
The email address must already have been registered
with the user's account before it can be set.
May be supplied with the delete-email option as long as
the emails are not the same.
--add-ssh-key::
Content of the public SSH key to add to the account's
keyring. If `-` the key is read from stdin, rather than

View File

@ -36,6 +36,7 @@ import com.google.gerrit.server.account.GetSshKeys;
import com.google.gerrit.server.account.GetSshKeys.SshKeyInfo;
import com.google.gerrit.server.account.PutActive;
import com.google.gerrit.server.account.PutHttpPassword;
import com.google.gerrit.server.account.PutPreferred;
import com.google.gerrit.server.account.PutName;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
@ -78,6 +79,9 @@ final class SetAccountCommand extends SshCommand {
@Option(name = "--delete-email", metaVar = "EMAIL", usage = "email addresses to delete from the account")
private List<String> deleteEmails = new ArrayList<>();
@Option(name = "--preferred-email", metaVar = "EMAIL", usage = "a registered email address from the account")
private String preferredEmail;
@Option(name = "--add-ssh-key", metaVar = "-|KEY", usage = "public keys to add to the account")
private List<String> addSshKeys = new ArrayList<>();
@ -102,6 +106,9 @@ final class SetAccountCommand extends SshCommand {
@Inject
private DeleteEmail deleteEmail;
@Inject
private PutPreferred putPreferred;
@Inject
private PutName putName;
@ -151,6 +158,11 @@ final class SetAccountCommand extends SshCommand {
if (deleteEmails.contains("ALL")) {
deleteEmails = Collections.singletonList("ALL");
}
if (deleteEmails.contains(preferredEmail)) {
throw new UnloggedFailure(1,
"--preferred-email and --delete-email options are mutually " +
"exclusive for the same email address.");
}
}
private void setAccount() throws OrmException, IOException, UnloggedFailure {
@ -165,6 +177,10 @@ final class SetAccountCommand extends SshCommand {
deleteEmail(email);
}
if (preferredEmail != null) {
putPreferred(preferredEmail);
}
if (fullName != null) {
PutName.Input in = new PutName.Input();
in.name = fullName;
@ -277,6 +293,17 @@ final class SetAccountCommand extends SshCommand {
}
}
private void putPreferred(String email) throws RestApiException,
OrmException {
for (EmailInfo e : getEmails.apply(rsrc)) {
if (e.email.equals(email)) {
putPreferred.apply(new AccountResource.Email(user, email), null);
return;
}
}
stderr.println("preferred email not found: " + email);
}
private List<String> readSshKey(final List<String> sshKeys)
throws UnsupportedEncodingException, IOException {
if (!sshKeys.isEmpty()) {