Search in sources :

Example 16 with ExternalIdNotes

use of com.google.gerrit.server.account.externalids.ExternalIdNotes in project gerrit by GerritCodeReview.

the class LocalUsernamesToLowerCase method replaceIfNotExists.

private void replaceIfNotExists(ExternalIdNotes extIdNotes, ExternalId extId, ExternalId extIdLowerCase) throws IOException {
    try {
        Optional<ExternalId> existingExternalId = extIdNotes.get(extIdLowerCase.key()).filter(eid -> eid.accountId().equals(extIdLowerCase.accountId())).filter(eid -> StringUtils.equalsIgnoreCase(eid.email(), extId.email())).filter(eid -> StringUtils.equalsIgnoreCase(eid.password(), extId.password()));
        if (existingExternalId.isPresent()) {
            System.err.println("WARNING: external-id " + extIdLowerCase + " already exists with the same account-id " + extId.accountId() + " :" + "removing the duplicate external-id " + extId.key());
            extIdNotes.delete(extId);
        } else {
            extIdNotes.replace(extId, extIdLowerCase);
        }
    } catch (ConfigInvalidException e) {
        throw new IOException("Unable to parse external id definition when looking for current external-id " + extIdLowerCase, e);
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Inject(com.google.inject.Inject) AccountSchemaDefinitions(com.google.gerrit.server.index.account.AccountSchemaDefinitions) StringUtils(org.apache.commons.lang3.StringUtils) DisabledExternalIdCache(com.google.gerrit.server.account.externalids.DisabledExternalIdCache) SCHEME_GERRIT(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GERRIT) NoteDbSchemaVersionCheck(com.google.gerrit.server.schema.NoteDbSchemaVersionCheck) Locale(java.util.Locale) ProgressMonitor(org.eclipse.jgit.lib.ProgressMonitor) ExternalIdFactory(com.google.gerrit.server.account.externalids.ExternalIdFactory) Collection(java.util.Collection) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) IOException(java.io.IOException) SiteProgram(com.google.gerrit.pgm.util.SiteProgram) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) Injector(com.google.inject.Injector) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException) LifecycleManager(com.google.gerrit.lifecycle.LifecycleManager) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) Optional(java.util.Optional) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) Repository(org.eclipse.jgit.lib.Repository) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) IOException(java.io.IOException)

Example 17 with ExternalIdNotes

use of com.google.gerrit.server.account.externalids.ExternalIdNotes in project gerrit by GerritCodeReview.

the class ExternalIdsOnInit method insert.

public synchronized void insert(String commitMessage, Collection<ExternalId> extIds) throws IOException, ConfigInvalidException {
    File path = getPath();
    if (path != null) {
        try (Repository allUsersRepo = new FileRepository(path)) {
            ExternalIdNotes extIdNotes = ExternalIdNotes.load(allUsers, allUsersRepo, externalIdFactory, authConfig.isUserNameCaseInsensitiveMigrationMode());
            extIdNotes.insert(extIds);
            try (MetaDataUpdate metaDataUpdate = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsers, allUsersRepo)) {
                PersonIdent serverIdent = new GerritPersonIdentProvider(flags.cfg).get();
                metaDataUpdate.getCommitBuilder().setAuthor(serverIdent);
                metaDataUpdate.getCommitBuilder().setCommitter(serverIdent);
                metaDataUpdate.getCommitBuilder().setMessage(commitMessage);
                extIdNotes.commit(metaDataUpdate);
            }
        }
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) GerritPersonIdentProvider(com.google.gerrit.server.GerritPersonIdentProvider) File(java.io.File) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 18 with ExternalIdNotes

use of com.google.gerrit.server.account.externalids.ExternalIdNotes in project gerrit by GerritCodeReview.

the class ChangeExternalIdCaseSensitivityIT method getExternalIdNotes.

private static ExternalIdNotes getExternalIdNotes(ServerContext ctx, Project.NameKey allUsers) throws Exception {
    GitRepositoryManager repoManager = ctx.getInjector().getInstance(GitRepositoryManager.class);
    ExternalIdNotes.FactoryNoReindex extIdNotesFactory = ctx.getInjector().getInstance(ExternalIdNotes.FactoryNoReindex.class);
    return extIdNotesFactory.load(repoManager.openRepository(allUsers));
}
Also used : ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager)

Example 19 with ExternalIdNotes

use of com.google.gerrit.server.account.externalids.ExternalIdNotes in project gerrit by GerritCodeReview.

the class ExternalIdIT method createCaseInsensitiveMigrationModeExternalIdBeforeTheMigration.

@Test
@GerritConfig(name = "auth.userNameCaseInsensitive", value = "true")
@GerritConfig(name = "auth.userNameCaseInsensitiveMigrationMode", value = "true")
public void createCaseInsensitiveMigrationModeExternalIdBeforeTheMigration() throws Exception {
    Account.Id accountId = Account.id(66);
    boolean isUserNameCaseInsensitive = false;
    try (Repository allUsersRepo = repoManager.openRepository(allUsers);
        MetaDataUpdate md = metaDataUpdateFactory.create(allUsers)) {
        ExternalIdNotes extIdNotes = externalIdNotesFactory.load(allUsersRepo);
        createExternalId(md, extIdNotes, SCHEME_GERRIT, "JaneDoe", accountId, isUserNameCaseInsensitive);
        createExternalId(md, extIdNotes, SCHEME_USERNAME, "JaneDoe", accountId, isUserNameCaseInsensitive);
        assertThat(getAccountId(extIdNotes, SCHEME_GERRIT, "JaneDoe")).isEqualTo(accountId.get());
        assertThat(getExternalId(extIdNotes, SCHEME_GERRIT, "janedoe").isPresent()).isFalse();
        assertThat(getAccountId(extIdNotes, SCHEME_USERNAME, "JaneDoe")).isEqualTo(accountId.get());
        assertThat(getExternalId(extIdNotes, SCHEME_USERNAME, "janedoe").isPresent()).isFalse();
    }
}
Also used : TestAccount(com.google.gerrit.acceptance.TestAccount) Account(com.google.gerrit.entities.Account) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 20 with ExternalIdNotes

use of com.google.gerrit.server.account.externalids.ExternalIdNotes in project gerrit by GerritCodeReview.

the class ExternalIdNotesUpsertPreprocessorIT method blockUpsert_replace.

@Test
public void blockUpsert_replace() throws Exception {
    Account.Id id = Account.id(sequences.nextAccountId());
    ExternalId extId1 = extIdFactory.create("foo", "bar", id, "email1@foo", "hash");
    ExternalId extId2 = extIdFactory.create("foo", "bar", id, "email2@foo", "hash");
    accountsUpdateProvider.get().insert("test", id, u -> u.addExternalId(extId1));
    assertThat(accounts.get(id).get().externalIds()).containsExactly(extId1);
    testPreprocessor.reset();
    testPreprocessor.throwException = true;
    try (Repository allUsersRepo = repoManager.openRepository(allUsers);
        MetaDataUpdate md = metaDataUpdateFactory.create(allUsers)) {
        ExternalIdNotes extIdNotes = extIdNotesFactory.load(allUsersRepo);
        extIdNotes.replace(ImmutableSet.of(extId1), ImmutableSet.of(extId2));
        StorageException e = assertThrows(StorageException.class, () -> extIdNotes.commit(md));
        assertThat(e).hasMessageThat().contains("upsert not good");
    }
    assertThat(testPreprocessor.upserted).isEmpty();
    assertThat(accounts.get(id).get().externalIds()).containsExactly(extId1);
}
Also used : Account(com.google.gerrit.entities.Account) Repository(org.eclipse.jgit.lib.Repository) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) StorageException(com.google.gerrit.exceptions.StorageException) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Test(org.junit.Test) LightweightPluginDaemonTest(com.google.gerrit.acceptance.LightweightPluginDaemonTest)

Aggregations

ExternalIdNotes (com.google.gerrit.server.account.externalids.ExternalIdNotes)31 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)29 Repository (org.eclipse.jgit.lib.Repository)28 Test (org.junit.Test)23 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)18 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)14 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)13 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)12 TestRepository (org.eclipse.jgit.junit.TestRepository)12 Account (com.google.gerrit.entities.Account)11 LightweightPluginDaemonTest (com.google.gerrit.acceptance.LightweightPluginDaemonTest)5 TestAccount (com.google.gerrit.acceptance.TestAccount)5 FactoryModule (com.google.gerrit.extensions.config.FactoryModule)2 GitReferenceUpdated (com.google.gerrit.server.extensions.events.GitReferenceUpdated)2 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)2 Injector (com.google.inject.Injector)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 DuplicateKeyException (com.google.gerrit.exceptions.DuplicateKeyException)1 StorageException (com.google.gerrit.exceptions.StorageException)1 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)1