Merge "Support listing the email addresses of an account via REST"
diff --git a/Documentation/rest-api-accounts.txt b/Documentation/rest-api-accounts.txt
index 332fe7f..8a3b24f 100644
--- a/Documentation/rest-api-accounts.txt
+++ b/Documentation/rest-api-accounts.txt
@@ -161,6 +161,40 @@
HTTP/1.1 204 No Content
----
+[[list-account-emails]]
+List Account Emails
+~~~~~~~~~~~~~~~~~~~
+[verse]
+'GET /accounts/link:#account-id[\{account-id\}]/emails'
+
+Returns the email addresses that are configured for the specified user.
+
+.Request
+----
+ GET /accounts/self/emails HTTP/1.0
+----
+
+As response the email addresses of the user are returned as a list of
+link:#email-info[EmailInfo] entities.
+
+.Response
+----
+ HTTP/1.1 200 OK
+ Content-Disposition: attachment
+ Content-Type: application/json;charset=UTF-8
+
+ )]}'
+ [
+ {
+ "email": "john.doe@example.com",
+ "preferred": true
+ },
+ {
+ "email": "j.doe@example.com"
+ }
+ ]
+----
+
[[list-account-capabilities]]
List Account Capabilities
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -717,6 +751,20 @@
Number of spaces that should be used to display one tab.
|=====================================
+[[email-info]]
+EmailInfo
+~~~~~~~~~
+The `EmailInfo` entity contains information about an email address of a
+user.
+
+[options="header",width="50%",cols="1,^1,5"]
+|========================
+|Field Name ||Description
+|`email` ||The email address.
+|`preferred`|not set if `false`|
+Whether this is the preferred email address of the user.
+|========================
+
[[query-limit-info]]
QueryLimitInfo
~~~~~~~~~~~~~~
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountResource.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountResource.java
index 9dc423a..91294c1 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountResource.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/AccountResource.java
@@ -26,6 +26,9 @@
public static final TypeLiteral<RestView<Capability>> CAPABILITY_KIND =
new TypeLiteral<RestView<Capability>>() {};
+ public static final TypeLiteral<RestView<Email>> EMAIL_KIND =
+ new TypeLiteral<RestView<Email>>() {};
+
private final IdentifiedUser user;
public AccountResource(IdentifiedUser user) {
@@ -57,4 +60,17 @@
return user.getCapabilities().canPerform(getCapability());
}
}
+
+ static class Email extends AccountResource {
+ private final String email;
+
+ public Email(IdentifiedUser user, String email) {
+ super(user);
+ this.email = email;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+ }
}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/Emails.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/Emails.java
new file mode 100644
index 0000000..30e232f
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/Emails.java
@@ -0,0 +1,53 @@
+// Copyright (C) 2013 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.server.account;
+
+import com.google.gerrit.extensions.registration.DynamicMap;
+import com.google.gerrit.extensions.restapi.ChildCollection;
+import com.google.gerrit.extensions.restapi.IdString;
+import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
+import com.google.gerrit.extensions.restapi.RestView;
+import com.google.gerrit.server.account.AccountResource.Email;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+public class Emails implements
+ ChildCollection<AccountResource, AccountResource.Email> {
+ private final DynamicMap<RestView<AccountResource.Email>> views;
+ private final Provider<GetEmails> get;
+
+ @Inject
+ Emails(DynamicMap<RestView<AccountResource.Email>> views,
+ Provider<GetEmails> get) {
+ this.views = views;
+ this.get = get;
+ }
+
+ @Override
+ public RestView<AccountResource> list() {
+ return get.get();
+ }
+
+ @Override
+ public AccountResource.Email parse(AccountResource parent, IdString id)
+ throws ResourceNotFoundException {
+ throw new ResourceNotFoundException();
+ }
+
+ @Override
+ public DynamicMap<RestView<Email>> views() {
+ return views;
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/GetEmails.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/GetEmails.java
new file mode 100644
index 0000000..dcd95bb
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/GetEmails.java
@@ -0,0 +1,79 @@
+// Copyright (C) 2013 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.server.account;
+
+import com.google.common.collect.Lists;
+import com.google.gerrit.extensions.restapi.AuthException;
+import com.google.gerrit.extensions.restapi.RestReadView;
+import com.google.gerrit.reviewdb.client.AccountExternalId;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.CurrentUser;
+import com.google.gerrit.server.IdentifiedUser;
+import com.google.gwtorm.server.OrmException;
+import com.google.gwtorm.server.ResultSet;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+import java.util.List;
+
+public class GetEmails implements RestReadView<AccountResource> {
+
+ private final Provider<CurrentUser> self;
+ private final Provider<ReviewDb> dbProvider;
+
+ @Inject
+ public GetEmails(Provider<CurrentUser> self, Provider<ReviewDb> dbProvider) {
+ this.self = self;
+ this.dbProvider = dbProvider;
+ }
+
+ @Override
+ public List<EmailInfo> apply(AccountResource rsrc) throws AuthException,
+ OrmException {
+ if (!(self.get() instanceof IdentifiedUser)) {
+ throw new AuthException("Authentication required");
+ }
+ IdentifiedUser s = (IdentifiedUser) self.get();
+ if (s.getAccountId().get() != rsrc.getUser().getAccountId().get()
+ && !s.getCapabilities().canAdministrateServer()) {
+ throw new AuthException("not allowed to list email addresses");
+ }
+
+ List<EmailInfo> emails = Lists.newArrayList();
+ ResultSet<AccountExternalId> ids =
+ dbProvider.get().accountExternalIds()
+ .byAccount(rsrc.getUser().getAccountId());
+ for (AccountExternalId extId : ids) {
+ String email = extId.getEmailAddress();
+ if (email != null) {
+ EmailInfo e = new EmailInfo();
+ e.email = email;
+ e.setPreferred(email.equals(rsrc.getUser().getAccount()
+ .getPreferredEmail()));
+ emails.add(e);
+ }
+ }
+ return emails;
+ }
+
+ public static class EmailInfo {
+ public String email;
+ public Boolean preferred;
+
+ void setPreferred(boolean preferred) {
+ this.preferred = preferred ? true : null;
+ }
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/Module.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/Module.java
index 534a2cb..eaca51f 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/account/Module.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/Module.java
@@ -16,6 +16,7 @@
import static com.google.gerrit.server.account.AccountResource.ACCOUNT_KIND;
import static com.google.gerrit.server.account.AccountResource.CAPABILITY_KIND;
+import static com.google.gerrit.server.account.AccountResource.EMAIL_KIND;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.RestApiModule;
@@ -28,6 +29,7 @@
bind(Capabilities.class);
DynamicMap.mapOf(binder(), ACCOUNT_KIND);
+ DynamicMap.mapOf(binder(), EMAIL_KIND);
DynamicMap.mapOf(binder(), CAPABILITY_KIND);
put(ACCOUNT_KIND).to(PutAccount.class);
@@ -35,6 +37,7 @@
get(ACCOUNT_KIND, "name").to(GetName.class);
put(ACCOUNT_KIND, "name").to(PutName.class);
delete(ACCOUNT_KIND, "name").to(PutName.class);
+ child(ACCOUNT_KIND, "emails").to(Emails.class);
get(ACCOUNT_KIND, "avatar").to(GetAvatar.class);
get(ACCOUNT_KIND, "avatar.change.url").to(GetAvatarChangeUrl.class);
child(ACCOUNT_KIND, "capabilities").to(Capabilities.class);