use of com.google.gerrit.extensions.common.AccountExternalIdInfo in project gerrit by GerritCodeReview.
the class AbstractQueryAccountsTest method rawDocument.
@Test
public void rawDocument() throws Exception {
AccountInfo userInfo = gApi.accounts().id(admin.getAccountId().get()).get();
Schema<AccountState> schema = indexes.getSearchIndex().getSchema();
Optional<FieldBundle> rawFields = indexes.getSearchIndex().getRaw(Account.id(userInfo._accountId), QueryOptions.create(IndexConfig.createDefault(), 0, 1, schema.getStoredFields().keySet()));
assertThat(rawFields).isPresent();
if (schema.useLegacyNumericFields()) {
assertThat(rawFields.get().getValue(AccountField.ID)).isEqualTo(userInfo._accountId);
} else {
assertThat(Integer.valueOf(rawFields.get().getValue(AccountField.ID_STR))).isEqualTo(userInfo._accountId);
}
// The field EXTERNAL_ID_STATE is only supported from schema version 6.
if (getSchemaVersion() < 6) {
return;
}
List<AccountExternalIdInfo> externalIdInfos = gApi.accounts().self().getExternalIds();
List<ByteArrayWrapper> blobs = new ArrayList<>();
for (AccountExternalIdInfo info : externalIdInfos) {
Optional<ExternalId> extId = externalIds.get(externalIdKeyFactory.parse(info.identity));
assertThat(extId).isPresent();
blobs.add(new ByteArrayWrapper(extId.get().toByteArray()));
}
assertThat(rawFields.get().getValue(AccountField.EXTERNAL_ID_STATE)).hasSize(blobs.size());
assertThat(Streams.stream(rawFields.get().getValue(AccountField.EXTERNAL_ID_STATE)).map(ByteArrayWrapper::new).collect(toList())).containsExactlyElementsIn(blobs);
}
use of com.google.gerrit.extensions.common.AccountExternalIdInfo in project gerrit by GerritCodeReview.
the class GetExternalIds method apply.
@Override
public Response<List<AccountExternalIdInfo>> apply(AccountResource resource) throws RestApiException, IOException, PermissionBackendException {
if (!self.get().hasSameAccountId(resource.getUser())) {
permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
}
Collection<ExternalId> ids = externalIds.byAccount(resource.getUser().getAccountId());
if (ids.isEmpty()) {
return Response.ok(ImmutableList.of());
}
List<AccountExternalIdInfo> result = Lists.newArrayListWithCapacity(ids.size());
for (ExternalId id : ids) {
AccountExternalIdInfo info = new AccountExternalIdInfo();
info.identity = id.key().get();
info.emailAddress = id.email();
info.trusted = toBoolean(authConfig.isIdentityTrustable(Collections.singleton(id)));
// actually used to establish this web session.
if (!id.isScheme(SCHEME_USERNAME)) {
Optional<ExternalId.Key> last = resource.getUser().getLastLoginExternalIdKey();
info.canDelete = toBoolean(!last.isPresent() || !last.get().get().equals(info.identity));
}
result.add(info);
}
return Response.ok(result);
}
use of com.google.gerrit.extensions.common.AccountExternalIdInfo in project gerrit by GerritCodeReview.
the class ExternalIdIT method deleteExternalIdsOfOtherUserWithAccessDatabase.
@Test
public void deleteExternalIdsOfOtherUserWithAccessDatabase() throws Exception {
allowGlobalCapabilities(REGISTERED_USERS, GlobalCapability.ACCESS_DATABASE);
List<AccountExternalIdInfo> externalIds = gApi.accounts().self().getExternalIds();
List<String> toDelete = new ArrayList<>();
List<AccountExternalIdInfo> expectedIds = new ArrayList<>();
for (AccountExternalIdInfo id : externalIds) {
if (id.canDelete != null && id.canDelete) {
toDelete.add(id.identity);
continue;
}
expectedIds.add(id);
}
assertThat(toDelete).hasSize(1);
setApiUser(user);
RestResponse response = userRestSession.post("/accounts/" + admin.id + "/external.ids:delete", toDelete);
response.assertNoContent();
List<AccountExternalIdInfo> results = gApi.accounts().id(admin.id.get()).getExternalIds();
// The external ID in WebSession will not be set for tests, resulting that
// "mailto:user@example.com" can be deleted while "username:user" can't.
assertThat(results).hasSize(1);
assertThat(results).containsExactlyElementsIn(expectedIds);
}
use of com.google.gerrit.extensions.common.AccountExternalIdInfo in project gerrit by GerritCodeReview.
the class GetExternalIds method apply.
@Override
public List<AccountExternalIdInfo> apply(AccountResource resource) throws RestApiException, IOException, OrmException {
if (self.get() != resource.getUser() && !self.get().getCapabilities().canAccessDatabase()) {
throw new AuthException("not allowed to get external IDs");
}
Collection<ExternalId> ids = externalIds.byAccount(resource.getUser().getAccountId());
if (ids.isEmpty()) {
return ImmutableList.of();
}
List<AccountExternalIdInfo> result = Lists.newArrayListWithCapacity(ids.size());
for (ExternalId id : ids) {
AccountExternalIdInfo info = new AccountExternalIdInfo();
info.identity = id.key().get();
info.emailAddress = id.email();
info.trusted = toBoolean(authConfig.isIdentityTrustable(Collections.singleton(id)));
// actually used to establish this web session.
if (!id.isScheme(SCHEME_USERNAME)) {
ExternalId.Key last = resource.getUser().getLastLoginExternalIdKey();
info.canDelete = toBoolean(last == null || !last.get().equals(info.identity));
}
result.add(info);
}
return result;
}
use of com.google.gerrit.extensions.common.AccountExternalIdInfo in project gerrit by GerritCodeReview.
the class ExternalIdIT method getExternalIdsOfOtherUserWithModifyAccount.
@Test
public void getExternalIdsOfOtherUserWithModifyAccount() throws Exception {
projectOperations.allProjectsForUpdate().add(allowCapability(GlobalCapability.MODIFY_ACCOUNT).group(REGISTERED_USERS)).update();
Collection<ExternalId> expectedIds = getAccountState(admin.id()).externalIds();
List<AccountExternalIdInfo> expectedIdInfos = toExternalIdInfos(expectedIds);
RestResponse response = userRestSession.get("/accounts/" + admin.id() + "/external.ids");
response.assertOK();
List<AccountExternalIdInfo> results = newGson().fromJson(response.getReader(), new TypeToken<List<AccountExternalIdInfo>>() {
}.getType());
assertThat(results).containsExactlyElementsIn(expectedIdInfos);
}
Aggregations