Search in sources :

Example 31 with ExternalId

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

the class LocalUsernamesToLowerCase method convertLocalUserToLowerCase.

private void convertLocalUserToLowerCase(ExternalId extId) {
    if (extId.isScheme(SCHEME_GERRIT)) {
        String localUser = extId.key().id();
        String localUserLowerCase = localUser.toLowerCase(Locale.US);
        if (!localUser.equals(localUserLowerCase)) {
            ExternalId extIdLowerCase = ExternalId.create(SCHEME_GERRIT, localUserLowerCase, extId.accountId(), extId.email(), extId.password());
            externalIdsBatchUpdate.replace(extId, extIdLowerCase);
        }
    }
}
Also used : ExternalId(com.google.gerrit.server.account.externalids.ExternalId)

Example 32 with ExternalId

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

the class AbstractRealm method getEmailAddresses.

@Override
public Set<String> getEmailAddresses(IdentifiedUser user) {
    Collection<ExternalId> ids = user.state().getExternalIds();
    Set<String> emails = Sets.newHashSetWithExpectedSize(ids.size());
    for (ExternalId ext : ids) {
        if (!Strings.isNullOrEmpty(ext.email())) {
            emails.add(ext.email());
        }
    }
    return emails;
}
Also used : ExternalId(com.google.gerrit.server.account.externalids.ExternalId)

Example 33 with ExternalId

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

the class AccountManager method unlink.

/**
   * Unlink an authentication identity from an existing account.
   *
   * @param from account to unlink the identity from.
   * @param who the identity to delete
   * @return the result of unlinking the identity from the user.
   * @throws AccountException the identity belongs to a different account, or it cannot be unlinked
   *     at this time.
   */
public AuthResult unlink(Account.Id from, AuthRequest who) throws AccountException, OrmException, IOException, ConfigInvalidException {
    try (ReviewDb db = schema.open()) {
        ExternalId extId = findExternalId(who.getExternalIdKey());
        if (extId != null) {
            if (!extId.accountId().equals(from)) {
                throw new AccountException("Identity '" + who.getExternalIdKey().get() + "' in use by another account");
            }
            externalIdsUpdateFactory.create().delete(extId);
            if (who.getEmailAddress() != null) {
                Account a = db.accounts().get(from);
                if (a.getPreferredEmail() != null && a.getPreferredEmail().equals(who.getEmailAddress())) {
                    a.setPreferredEmail(null);
                    db.accounts().update(Collections.singleton(a));
                }
                byEmailCache.evict(who.getEmailAddress());
                byIdCache.evict(from);
            }
        } else {
            throw new AccountException("Identity '" + who.getExternalIdKey().get() + "' not found");
        }
        return new AuthResult(from, who.getExternalIdKey(), false);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 34 with ExternalId

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

the class AccountManager method authenticate.

/**
   * Authenticate the user, potentially creating a new account if they are new.
   *
   * @param who identity of the user, with any details we received about them.
   * @return the result of authenticating the user.
   * @throws AccountException the account does not exist, and cannot be created, or exists, but
   *     cannot be located, or is inactive.
   */
public AuthResult authenticate(AuthRequest who) throws AccountException, IOException {
    who = realm.authenticate(who);
    try {
        try (ReviewDb db = schema.open()) {
            ExternalId id = findExternalId(who.getExternalIdKey());
            if (id == null) {
                //
                return create(db, who);
            }
            // Account exists
            Account act = byIdCache.get(id.accountId()).getAccount();
            if (!act.isActive()) {
                throw new AccountException("Authentication error, account inactive");
            }
            // return the identity to the caller.
            update(db, who, id);
            return new AuthResult(id.accountId(), who.getExternalIdKey(), false);
        }
    } catch (OrmException | ConfigInvalidException e) {
        throw new AccountException("Authentication error", e);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OrmException(com.google.gwtorm.server.OrmException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Aggregations

ExternalId (com.google.gerrit.server.account.externalids.ExternalId)34 Account (com.google.gerrit.reviewdb.client.Account)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)8 Test (org.junit.Test)8 OrmException (com.google.gwtorm.server.OrmException)7 ArrayList (java.util.ArrayList)7 ObjectId (org.eclipse.jgit.lib.ObjectId)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)6 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)6 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)6 NoteMap (org.eclipse.jgit.notes.NoteMap)6 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)5 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)5 ExternalIdsUpdate (com.google.gerrit.server.account.externalids.ExternalIdsUpdate)5 HashSet (java.util.HashSet)5 AccountExternalIdInfo (com.google.gerrit.extensions.common.AccountExternalIdInfo)4 AuthException (com.google.gerrit.extensions.restapi.AuthException)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)4 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)4