Search in sources :

Example 16 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class AccountManager method update.

private void update(ReviewDb db, AuthRequest who, ExternalId extId) throws OrmException, IOException, ConfigInvalidException {
    IdentifiedUser user = userFactory.create(extId.accountId());
    Account toUpdate = null;
    // If the email address was modified by the authentication provider,
    // update our records to match the changed email.
    //
    String newEmail = who.getEmailAddress();
    String oldEmail = extId.email();
    if (newEmail != null && !newEmail.equals(oldEmail)) {
        if (oldEmail != null && oldEmail.equals(user.getAccount().getPreferredEmail())) {
            toUpdate = load(toUpdate, user.getAccountId(), db);
            toUpdate.setPreferredEmail(newEmail);
        }
        externalIdsUpdateFactory.create().replace(extId, ExternalId.create(extId.key(), extId.accountId(), newEmail, extId.password()));
    }
    if (!realm.allowsEdit(AccountFieldName.FULL_NAME) && !Strings.isNullOrEmpty(who.getDisplayName()) && !eq(user.getAccount().getFullName(), who.getDisplayName())) {
        toUpdate = load(toUpdate, user.getAccountId(), db);
        toUpdate.setFullName(who.getDisplayName());
    }
    if (!realm.allowsEdit(AccountFieldName.USER_NAME) && who.getUserName() != null && !eq(user.getUserName(), who.getUserName())) {
        log.warn(String.format("Not changing already set username %s to %s", user.getUserName(), who.getUserName()));
    }
    if (toUpdate != null) {
        db.accounts().update(Collections.singleton(toUpdate));
    }
    if (newEmail != null && !newEmail.equals(oldEmail)) {
        byEmailCache.evict(oldEmail);
        byEmailCache.evict(newEmail);
    }
    if (toUpdate != null) {
        byIdCache.evict(toUpdate.getId());
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 17 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class IdentifiedUser method newCommitterIdent.

public PersonIdent newCommitterIdent(final Date when, final TimeZone tz) {
    final Account ua = getAccount();
    String name = ua.getFullName();
    String email = ua.getPreferredEmail();
    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();
        if (user == null || user.isEmpty()) {
            user = "account-" + ua.getId().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 new PersonIdent(name, email, when, tz);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) MalformedURLException(java.net.MalformedURLException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) URL(java.net.URL)

Example 18 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class IdentifiedUser method newRefLogIdent.

public PersonIdent newRefLogIdent(final Date when, final TimeZone tz) {
    final Account ua = getAccount();
    String name = ua.getFullName();
    if (name == null || name.isEmpty()) {
        name = ua.getPreferredEmail();
    }
    if (name == null || name.isEmpty()) {
        name = anonymousCowardName;
    }
    String user = getUserName();
    if (user == null) {
        user = "";
    }
    user = user + "|account-" + ua.getId().toString();
    return new PersonIdent(name, user + "@" + guessHost(), when, tz);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) PersonIdent(org.eclipse.jgit.lib.PersonIdent)

Example 19 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class NotifyUtil method find.

private List<Account.Id> find(ReviewDb db, List<String> nameOrEmails) throws OrmException, BadRequestException {
    List<String> missing = new ArrayList<>(nameOrEmails.size());
    List<Account.Id> r = new ArrayList<>(nameOrEmails.size());
    for (String nameOrEmail : nameOrEmails) {
        Account a = accountResolver.find(db, nameOrEmail);
        if (a != null) {
            r.add(a.getId());
        } else {
            missing.add(nameOrEmail);
        }
    }
    if (!missing.isEmpty()) {
        throw new BadRequestException("The following accounts that should be notified could not be resolved: " + missing.stream().distinct().sorted().collect(joining(", ")));
    }
    return r;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ArrayList(java.util.ArrayList) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 20 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class PostReviewers method addWholeGroup.

@Nullable
private Addition addWholeGroup(String reviewer, ChangeResource rsrc, ReviewerState state, NotifyHandling notify, ListMultimap<RecipientType, Account.Id> accountsToNotify, boolean confirmed, boolean allowGroup, boolean allowByEmail) throws OrmException, IOException, PermissionBackendException {
    if (!allowGroup) {
        return null;
    }
    GroupDescription.Basic group = null;
    try {
        group = groupsCollection.parseInternal(reviewer);
    } catch (UnprocessableEntityException e) {
        if (!allowByEmail) {
            return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerNotFoundUserOrGroup, reviewer));
        }
        return null;
    }
    if (!isLegalReviewerGroup(group.getGroupUUID())) {
        return fail(reviewer, MessageFormat.format(ChangeMessages.get().groupIsNotAllowed, group.getName()));
    }
    Set<Account.Id> reviewers = new HashSet<>();
    ChangeControl control = rsrc.getControl();
    Set<Account> members;
    try {
        members = groupMembersFactory.create(control.getUser()).listAccounts(group.getGroupUUID(), control.getProject().getNameKey());
    } catch (NoSuchGroupException e) {
        return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerNotFoundUserOrGroup, group.getName()));
    } catch (NoSuchProjectException e) {
        return fail(reviewer, e.getMessage());
    }
    // if maxAllowed is set to 0, it is allowed to add any number of
    // reviewers
    int maxAllowed = cfg.getInt("addreviewer", "maxAllowed", DEFAULT_MAX_REVIEWERS);
    if (maxAllowed > 0 && members.size() > maxAllowed) {
        return fail(reviewer, MessageFormat.format(ChangeMessages.get().groupHasTooManyMembers, group.getName()));
    }
    // if maxWithoutCheck is set to 0, we never ask for confirmation
    int maxWithoutConfirmation = cfg.getInt("addreviewer", "maxWithoutConfirmation", DEFAULT_MAX_REVIEWERS_WITHOUT_CHECK);
    if (!confirmed && maxWithoutConfirmation > 0 && members.size() > maxWithoutConfirmation) {
        return fail(reviewer, true, MessageFormat.format(ChangeMessages.get().groupManyMembersConfirmation, group.getName(), members.size()));
    }
    PermissionBackend.ForRef perm = permissionBackend.user(rsrc.getUser()).ref(rsrc.getChange().getDest());
    for (Account member : members) {
        if (isValidReviewer(member, perm)) {
            reviewers.add(member.getId());
        }
    }
    return new Addition(reviewer, rsrc, reviewers, null, state, notify, accountsToNotify);
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Account(com.google.gerrit.reviewdb.client.Account) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) NoSuchGroupException(com.google.gerrit.common.errors.NoSuchGroupException) GroupDescription(com.google.gerrit.common.data.GroupDescription) ChangeControl(com.google.gerrit.server.project.ChangeControl) HashSet(java.util.HashSet) Nullable(com.google.gerrit.common.Nullable)

Aggregations

Account (com.google.gerrit.reviewdb.client.Account)75 ArrayList (java.util.ArrayList)13 OrmException (com.google.gwtorm.server.OrmException)11 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)10 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)10 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)8 HashSet (java.util.HashSet)8 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)7 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)7 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)7 PersonIdent (org.eclipse.jgit.lib.PersonIdent)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)6 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)6 CurrentUser (com.google.gerrit.server.CurrentUser)6 Ref (org.eclipse.jgit.lib.Ref)6 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)4