Search in sources :

Example 1 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class BecomeAnyAccountLoginServlet method prepareHtmlOutput.

private byte[] prepareHtmlOutput() throws IOException {
    final String pageName = "BecomeAnyAccount.html";
    Document doc = headers.parse(getClass(), pageName);
    if (doc == null) {
        throw new FileNotFoundException("No " + pageName + " in webapp");
    }
    Element userlistElement = HtmlDomUtil.find(doc, "userlist");
    for (Account.Id accountId : accounts.firstNIds(100)) {
        Optional<AccountState> accountState = accountCache.get(accountId);
        if (!accountState.isPresent()) {
            continue;
        }
        Account account = accountState.get().account();
        String displayName;
        if (accountState.get().userName().isPresent()) {
            displayName = accountState.get().userName().get();
        } else if (account.fullName() != null && !account.fullName().isEmpty()) {
            displayName = account.fullName();
        } else if (account.preferredEmail() != null) {
            displayName = account.preferredEmail();
        } else {
            displayName = accountId.toString();
        }
        Element linkElement = doc.createElement("a");
        linkElement.setAttribute("href", "?account_id=" + account.id().toString());
        linkElement.setTextContent(displayName);
        userlistElement.appendChild(linkElement);
        userlistElement.appendChild(doc.createElement("br"));
    }
    return HtmlDomUtil.toUTF8(doc);
}
Also used : Account(com.google.gerrit.entities.Account) Element(org.w3c.dom.Element) FileNotFoundException(java.io.FileNotFoundException) AccountState(com.google.gerrit.server.account.AccountState) Document(org.w3c.dom.Document)

Example 2 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountOperationsImpl method createAccount.

private Account.Id createAccount(TestAccountCreation testAccountCreation) throws Exception {
    Account.Id accountId = Account.id(seq.nextAccountId());
    Consumer<AccountDelta.Builder> accountCreation = deltaBuilder -> initAccountDelta(deltaBuilder, testAccountCreation, accountId);
    AccountState createdAccount = accountsUpdate.insert("Create Test Account", accountId, accountCreation);
    return createdAccount.account().id();
}
Also used : ConfigureDeltaFromState(com.google.gerrit.server.account.AccountsUpdate.ConfigureDeltaFromState) ImmutableSet(com.google.common.collect.ImmutableSet) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Sequences(com.google.gerrit.server.notedb.Sequences) Inject(com.google.inject.Inject) Account(com.google.gerrit.entities.Account) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) AccountDelta(com.google.gerrit.server.account.AccountDelta) Accounts(com.google.gerrit.server.account.Accounts) Consumer(java.util.function.Consumer) ServerInitiated(com.google.gerrit.server.ServerInitiated) ExternalIdFactory(com.google.gerrit.server.account.externalids.ExternalIdFactory) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Optional(java.util.Optional) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountState(com.google.gerrit.server.account.AccountState) AccountsUpdate(com.google.gerrit.server.account.AccountsUpdate) Account(com.google.gerrit.entities.Account) AccountState(com.google.gerrit.server.account.AccountState)

Example 3 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class CommentsUtil method byPatchSet.

public List<HumanComment> byPatchSet(ChangeNotes notes, PatchSet.Id psId) {
    List<HumanComment> comments = new ArrayList<>();
    comments.addAll(publishedByPatchSet(notes, psId));
    for (Ref ref : getDraftRefs(notes.getChangeId())) {
        Account.Id account = Account.Id.fromRefSuffix(ref.getName());
        if (account != null) {
            comments.addAll(draftByPatchSetAuthor(psId, account, notes));
        }
    }
    return sort(comments);
}
Also used : Account(com.google.gerrit.entities.Account) Ref(org.eclipse.jgit.lib.Ref) ArrayList(java.util.ArrayList) HumanComment(com.google.gerrit.entities.HumanComment)

Example 4 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class IdentifiedUser method newCommitterIdent.

public PersonIdent newCommitterIdent(Instant when, TimeZone tz) {
    final Account ua = getAccount();
    String name = ua.fullName();
    String email = ua.preferredEmail();
    if (email == null || email.isEmpty()) {
        // No preferred email is configured. Use a generic identity so we
        // don't leak an address the user may have given us, but doesn't
        // necessarily want to publish through Git records.
        // 
        String user = getUserName().orElseGet(() -> "account-" + ua.id().toString());
        String host;
        if (canonicalUrl.get() != null) {
            try {
                host = new URL(canonicalUrl.get()).getHost();
            } catch (MalformedURLException e) {
                host = SystemReader.getInstance().getHostname();
            }
        } else {
            host = SystemReader.getInstance().getHostname();
        }
        email = user + "@" + host;
    }
    if (name == null || name.isEmpty()) {
        final int at = email.indexOf('@');
        if (0 < at) {
            name = email.substring(0, at);
        } else {
            name = anonymousCowardName;
        }
    }
    return newPersonIdent(name, email, when, tz);
}
Also used : Account(com.google.gerrit.entities.Account) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Example 5 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountManager method create.

private AuthResult create(AuthRequest who) throws AccountException, IOException, ConfigInvalidException {
    Account.Id newId = Account.id(sequences.nextAccountId());
    logger.atFine().log("Assigning new Id %s to account", newId);
    ExternalId extId = externalIdFactory.createWithEmail(who.getExternalIdKey(), newId, who.getEmailAddress());
    logger.atFine().log("Created external Id: %s", extId);
    checkEmailNotUsed(newId, extId);
    ExternalId userNameExtId = who.getUserName().isPresent() ? createUsername(newId, who.getUserName().get()) : null;
    boolean isFirstAccount = awaitsFirstAccountCheck.getAndSet(false) && !accounts.hasAnyAccount();
    AccountState accountState;
    try {
        accountState = accountsUpdateProvider.get().insert("Create Account on First Login", newId, u -> {
            u.setFullName(who.getDisplayName()).setPreferredEmail(extId.email()).addExternalId(extId);
            if (userNameExtId != null) {
                u.addExternalId(userNameExtId);
            }
        });
    } catch (DuplicateExternalIdKeyException e) {
        throw new AccountException("Cannot assign external ID \"" + e.getDuplicateKey().get() + "\" to account " + newId + "; external ID already in use.");
    } finally {
        // If adding the account failed, it may be that it actually was the
        // first account. So we reset the 'check for first account'-guard, as
        // otherwise the first account would not get administration permissions.
        awaitsFirstAccountCheck.set(isFirstAccount);
    }
    if (userNameExtId != null) {
        who.getUserName().ifPresent(sshKeyCache::evict);
    }
    IdentifiedUser user = userFactory.create(newId);
    if (isFirstAccount) {
        // This is the first user account on our site. Assume this user
        // is going to be the site's administrator and just make them that
        // to bootstrap the authentication database.
        // 
        Permission admin = projectCache.getAllProjects().getConfig().getAccessSection(AccessSection.GLOBAL_CAPABILITIES).orElseThrow(() -> new IllegalStateException("access section does not exist")).getPermission(GlobalCapability.ADMINISTRATE_SERVER);
        AccountGroup.UUID adminGroupUuid = admin.getRules().get(0).getGroup().getUUID();
        addGroupMember(adminGroupUuid, user);
    }
    realm.onCreateAccount(who, accountState.account());
    return new AuthResult(newId, extId.key(), true);
}
Also used : ExternalIdKeyFactory(com.google.gerrit.server.account.externalids.ExternalIdKeyFactory) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) ProjectCache(com.google.gerrit.server.project.ProjectCache) Inject(com.google.inject.Inject) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) GroupsUpdate(com.google.gerrit.server.group.db.GroupsUpdate) Strings(com.google.common.base.Strings) Config(org.eclipse.jgit.lib.Config) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) SCHEME_USERNAME(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME) ExternalIdFactory(com.google.gerrit.server.account.externalids.ExternalIdFactory) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) NoSuchUserException(com.google.gerrit.server.auth.NoSuchUserException) AccountGroup(com.google.gerrit.entities.AccountGroup) ImmutableSet(com.google.common.collect.ImmutableSet) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) Sequences(com.google.gerrit.server.notedb.Sequences) SshKeyCache(com.google.gerrit.server.ssh.SshKeyCache) AccessSection(com.google.gerrit.entities.AccessSection) StorageException(com.google.gerrit.exceptions.StorageException) Collection(java.util.Collection) Permission(com.google.gerrit.entities.Permission) Account(com.google.gerrit.entities.Account) Set(java.util.Set) AccountFieldName(com.google.gerrit.extensions.client.AccountFieldName) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Provider(com.google.inject.Provider) List(java.util.List) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ServerInitiated(com.google.gerrit.server.ServerInitiated) Optional(java.util.Optional) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DuplicateExternalIdKeyException(com.google.gerrit.server.account.externalids.DuplicateExternalIdKeyException) GroupDelta(com.google.gerrit.server.group.db.GroupDelta) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) Account(com.google.gerrit.entities.Account) DuplicateExternalIdKeyException(com.google.gerrit.server.account.externalids.DuplicateExternalIdKeyException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) AccountGroup(com.google.gerrit.entities.AccountGroup) Permission(com.google.gerrit.entities.Permission)

Aggregations

Account (com.google.gerrit.entities.Account)124 Test (org.junit.Test)59 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)37 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)35 AccountState (com.google.gerrit.server.account.AccountState)35 IOException (java.io.IOException)31 Repository (org.eclipse.jgit.lib.Repository)31 Change (com.google.gerrit.entities.Change)28 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)26 Inject (com.google.inject.Inject)25 PersonIdent (org.eclipse.jgit.lib.PersonIdent)25 List (java.util.List)24 ArrayList (java.util.ArrayList)23 HashSet (java.util.HashSet)23 Set (java.util.Set)22 RefNames (com.google.gerrit.entities.RefNames)21 AuthRequest (com.google.gerrit.server.account.AuthRequest)21 Map (java.util.Map)21 ObjectId (org.eclipse.jgit.lib.ObjectId)21 ImmutableList (com.google.common.collect.ImmutableList)20