Add REST endpoint to get details of an account

In addition to the AccountInfo fields the new REST endpoint returns
the registration date of the account and the timestamp of when contact
information was filed for this account. So far this information was
not available via REST, but clients may want to show this information
for the currently signed in user.

Change-Id: I6211c13d26290d1c0f3c31a1b52408c58983f08b
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2015-07-28 11:02:40 +02:00
parent 13f92cd40b
commit cf77a6955d
4 changed files with 158 additions and 0 deletions

View File

@@ -117,6 +117,36 @@ returned that describes the created account.
}
----
[[get-detail]]
=== Get Account Details
--
'GET /accounts/link:#account-id[\{account-id\}]/detail'
--
Retrieves the details of an account as an link:account-detail-info[
AccountDetailInfo] entity.
.Request
----
GET /accounts/self/detail HTTP/1.0
----
.Response
----
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Type: application/json; charset=UTF-8
)]}'
{
"registered_on": "2015-07-23 07:01:09.296000000",
"_account_id": 1000096,
"name": "John Doe",
"email": "john.doe@example.com",
"username": "john"
}
----
[[get-account-name]]
=== Get Account Name
--
@@ -1242,6 +1272,26 @@ The sequence number of the SSH key.
[[json-entities]]
== JSON Entities
[[account-detail-info]]
=== AccountDetailInfo
The `AccountDetailInfo` entity contains detailled information about an
account.
`AccountDetailInfo` has the same fields as link:#account-info[
AccountInfo]. In addition `AccountDetailInfo` has the following fields:
[options="header",cols="1,^1,5"]
|=================================
|Field Name ||Description
|`registered_on` ||
The link:rest-api.html#timestamp[timestamp] of when the account was
registered.
|`contact_filed_on` |optional|
The link:rest-api.html#timestamp[timestamp] of when contact information
for this account was filed. Not set if no contact information was
filed.
|=================================
[[account-info]]
=== AccountInfo
The `AccountInfo` entity contains information about an account.

View File

@@ -0,0 +1,42 @@
// Copyright (C) 2015 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.acceptance.rest.account;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.acceptance.rest.account.AccountAssert.assertAccountInfo;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.GetDetail.AccountDetailInfo;
import com.google.inject.Inject;
import org.junit.Test;
public class GetAccountDetailIT extends AbstractDaemonTest {
@Inject
private AccountCache accountCache;
@Test
public void getDetail() throws Exception {
RestResponse r = adminSession.get("/accounts/" + admin.username + "/detail/");
AccountDetailInfo info = newGson().fromJson(r.getReader(), AccountDetailInfo.class);
assertAccountInfo(admin, info);
Account account = accountCache.get(admin.getId()).getAccount();
assertThat(info.registeredOn).isEqualTo(account.getRegisteredOn());
assertThat(info.contactFiledOn).isEqualTo(account.getContactFiledOn());
}
}

View File

@@ -0,0 +1,65 @@
// Copyright (C) 2015 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.base.Throwables;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.account.AccountDirectory.DirectoryException;
import com.google.gerrit.server.account.AccountDirectory.FillOptions;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.EnumSet;
@Singleton
public class GetDetail implements RestReadView<AccountResource> {
private final InternalAccountDirectory directory;
@Inject
public GetDetail(InternalAccountDirectory directory) {
this.directory = directory;
}
@Override
public AccountDetailInfo apply(AccountResource rsrc) throws OrmException {
Account a = rsrc.getUser().getAccount();
AccountDetailInfo info = new AccountDetailInfo(a.getId().get());
info.registeredOn = a.getRegisteredOn();
info.contactFiledOn = a.getContactFiledOn();
try {
directory.fillAccountInfo(Collections.singleton(info),
EnumSet.allOf(FillOptions.class));
} catch (DirectoryException e) {
Throwables.propagateIfPossible(e.getCause(), OrmException.class);
throw new OrmException(e);
}
return info;
}
public static class AccountDetailInfo extends AccountInfo {
public Timestamp registeredOn;
public Timestamp contactFiledOn;
public AccountDetailInfo(Integer id) {
super(id);
}
}
}

View File

@@ -38,6 +38,7 @@ public class Module extends RestApiModule {
put(ACCOUNT_KIND).to(PutAccount.class);
get(ACCOUNT_KIND).to(GetAccount.class);
get(ACCOUNT_KIND, "detail").to(GetDetail.class);
get(ACCOUNT_KIND, "name").to(GetName.class);
put(ACCOUNT_KIND, "name").to(PutName.class);
delete(ACCOUNT_KIND, "name").to(PutName.class);