Search in sources :

Example 6 with AccountExternalIdInfo

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);
}
Also used : FieldBundle(com.google.gerrit.index.query.FieldBundle) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AccountState(com.google.gerrit.server.account.AccountState) AccountExternalIdInfo(com.google.gerrit.extensions.common.AccountExternalIdInfo) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) Test(org.junit.Test)

Example 7 with AccountExternalIdInfo

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);
}
Also used : ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountExternalIdInfo(com.google.gerrit.extensions.common.AccountExternalIdInfo)

Example 8 with AccountExternalIdInfo

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);
}
Also used : RestResponse(com.google.gerrit.acceptance.RestResponse) ArrayList(java.util.ArrayList) AccountExternalIdInfo(com.google.gerrit.extensions.common.AccountExternalIdInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 9 with AccountExternalIdInfo

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;
}
Also used : ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AuthException(com.google.gerrit.extensions.restapi.AuthException) AccountExternalIdInfo(com.google.gerrit.extensions.common.AccountExternalIdInfo)

Example 10 with AccountExternalIdInfo

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);
}
Also used : RestResponse(com.google.gerrit.acceptance.RestResponse) TypeToken(com.google.gson.reflect.TypeToken) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountExternalIdInfo(com.google.gerrit.extensions.common.AccountExternalIdInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

AccountExternalIdInfo (com.google.gerrit.extensions.common.AccountExternalIdInfo)15 Test (org.junit.Test)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)11 RestResponse (com.google.gerrit.acceptance.RestResponse)10 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)9 ArrayList (java.util.ArrayList)8 TypeToken (com.google.gson.reflect.TypeToken)5 TestAccount (com.google.gerrit.acceptance.TestAccount)3 AuthException (com.google.gerrit.extensions.restapi.AuthException)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Iterables (com.google.common.collect.Iterables)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Truth8.assertThat (com.google.common.truth.Truth8.assertThat)2 GitUtil.fetch (com.google.gerrit.acceptance.GitUtil.fetch)2 GitUtil.pushHead (com.google.gerrit.acceptance.GitUtil.pushHead)2 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)2 ProjectOperations (com.google.gerrit.acceptance.testsuite.project.ProjectOperations)2 TestProjectUpdate.allow (com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.allow)2 TestProjectUpdate.allowCapability (com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.allowCapability)2