Search in sources :

Example 71 with Account

use of com.google.gerrit.reviewdb.client.Account 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 72 with Account

use of com.google.gerrit.reviewdb.client.Account 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)

Example 73 with Account

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

the class EventFactory method addSubmitRecordLabels.

private void addSubmitRecordLabels(SubmitRecord submitRecord, SubmitRecordAttribute sa) {
    if (submitRecord.labels != null && !submitRecord.labels.isEmpty()) {
        sa.labels = new ArrayList<>();
        for (SubmitRecord.Label lbl : submitRecord.labels) {
            SubmitLabelAttribute la = new SubmitLabelAttribute();
            la.label = lbl.label;
            la.status = lbl.status.name();
            if (lbl.appliedBy != null) {
                Account a = accountCache.get(lbl.appliedBy).getAccount();
                la.by = asAccountAttribute(a);
            }
            sa.labels.add(la);
        }
    }
}
Also used : SubmitRecord(com.google.gerrit.common.data.SubmitRecord) Account(com.google.gerrit.reviewdb.client.Account) SubmitLabelAttribute(com.google.gerrit.server.data.SubmitLabelAttribute)

Example 74 with Account

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

the class PatchScriptFactory method loadCommentsAndHistory.

private void loadCommentsAndHistory(ChangeNotes notes, ChangeType changeType, String oldName, String newName) throws OrmException {
    Map<Patch.Key, Patch> byKey = new HashMap<>();
    if (loadHistory) {
        // This seems like a cheap trick. It doesn't properly account for a
        // file that gets renamed between patch set 1 and patch set 2. We
        // will wind up packing the wrong Patch object because we didn't do
        // proper rename detection between the patch sets.
        //
        history = new ArrayList<>();
        for (PatchSet ps : psUtil.byChange(db, notes)) {
            if (!control.isPatchVisible(ps, db)) {
                continue;
            }
            String name = fileName;
            if (psa != null) {
                switch(changeType) {
                    case COPIED:
                    case RENAMED:
                        if (ps.getId().equals(psa)) {
                            name = oldName;
                        }
                        break;
                    case MODIFIED:
                    case DELETED:
                    case ADDED:
                    case REWRITE:
                        break;
                }
            }
            Patch p = new Patch(new Patch.Key(ps.getId(), name));
            history.add(p);
            byKey.put(p.getKey(), p);
        }
        if (edit != null && edit.isPresent()) {
            Patch p = new Patch(new Patch.Key(new PatchSet.Id(psb.getParentKey(), 0), fileName));
            history.add(p);
            byKey.put(p.getKey(), p);
        }
    }
    if (loadComments && edit == null) {
        comments = new CommentDetail(psa, psb);
        switch(changeType) {
            case ADDED:
            case MODIFIED:
                loadPublished(byKey, newName);
                break;
            case DELETED:
                loadPublished(byKey, newName);
                break;
            case COPIED:
            case RENAMED:
                if (psa != null) {
                    loadPublished(byKey, oldName);
                }
                loadPublished(byKey, newName);
                break;
            case REWRITE:
                break;
        }
        CurrentUser user = control.getUser();
        if (user.isIdentifiedUser()) {
            Account.Id me = user.getAccountId();
            switch(changeType) {
                case ADDED:
                case MODIFIED:
                    loadDrafts(byKey, me, newName);
                    break;
                case DELETED:
                    loadDrafts(byKey, me, newName);
                    break;
                case COPIED:
                case RENAMED:
                    if (psa != null) {
                        loadDrafts(byKey, me, oldName);
                    }
                    loadDrafts(byKey, me, newName);
                    break;
                case REWRITE:
                    break;
            }
        }
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) CurrentUser(com.google.gerrit.server.CurrentUser) HashMap(java.util.HashMap) CommentDetail(com.google.gerrit.common.data.CommentDetail) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ObjectId(org.eclipse.jgit.lib.ObjectId) Patch(com.google.gerrit.reviewdb.client.Patch)

Example 75 with Account

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

the class GerritGSSAuthenticator method validateIdentity.

@Override
public boolean validateIdentity(final ServerSession session, final String identity) {
    final SshSession sd = session.getAttribute(SshSession.KEY);
    int at = identity.indexOf('@');
    String username;
    if (at == -1) {
        username = identity;
    } else {
        username = identity.substring(0, at);
    }
    if (config.getBoolean("auth", "userNameToLowerCase", false)) {
        username = username.toLowerCase(Locale.US);
    }
    AccountState state = accounts.getByUsername(username);
    Account account = state == null ? null : state.getAccount();
    boolean active = account != null && account.isActive();
    if (active) {
        return SshUtil.success(username, session, sshScope, sshLog, sd, SshUtil.createUser(sd, userFactory, account.getId()));
    }
    return false;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) AccountState(com.google.gerrit.server.account.AccountState)

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