Search in sources :

Example 1 with AccountGroupMember

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

the class GroupMembers method getGroupMembers.

private Set<Account> getGroupMembers(final AccountGroup group, final Project.NameKey project, final Set<AccountGroup.UUID> seen) throws NoSuchGroupException, OrmException, NoSuchProjectException, IOException {
    seen.add(group.getGroupUUID());
    final GroupDetail groupDetail = groupDetailFactory.create(group.getId()).call();
    final Set<Account> members = new HashSet<>();
    if (groupDetail.members != null) {
        for (final AccountGroupMember member : groupDetail.members) {
            members.add(accountCache.get(member.getAccountId()).getAccount());
        }
    }
    if (groupDetail.includes != null) {
        for (final AccountGroupById groupInclude : groupDetail.includes) {
            final AccountGroup includedGroup = groupCache.get(groupInclude.getIncludeUUID());
            if (includedGroup != null && !seen.contains(includedGroup.getGroupUUID())) {
                members.addAll(listAccounts(includedGroup.getGroupUUID(), project, seen));
            }
        }
    }
    return members;
}
Also used : GroupDetail(com.google.gerrit.common.data.GroupDetail) Account(com.google.gerrit.reviewdb.client.Account) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AccountGroupById(com.google.gerrit.reviewdb.client.AccountGroupById) HashSet(java.util.HashSet)

Example 2 with AccountGroupMember

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

the class DbGroupMemberAuditListener method onDeleteAccountsFromGroup.

@Override
public void onDeleteAccountsFromGroup(Account.Id me, Collection<AccountGroupMember> removed) {
    List<AccountGroupMemberAudit> auditInserts = new ArrayList<>();
    List<AccountGroupMemberAudit> auditUpdates = new ArrayList<>();
    try (ReviewDb db = schema.open()) {
        for (AccountGroupMember m : removed) {
            AccountGroupMemberAudit audit = null;
            for (AccountGroupMemberAudit a : db.accountGroupMembersAudit().byGroupAccount(m.getAccountGroupId(), m.getAccountId())) {
                if (a.isActive()) {
                    audit = a;
                    break;
                }
            }
            if (audit != null) {
                audit.removed(me, TimeUtil.nowTs());
                auditUpdates.add(audit);
            } else {
                audit = new AccountGroupMemberAudit(m, me, TimeUtil.nowTs());
                audit.removedLegacy();
                auditInserts.add(audit);
            }
        }
        db.accountGroupMembersAudit().update(auditUpdates);
        db.accountGroupMembersAudit().insert(auditInserts);
    } catch (OrmException e) {
        logOrmExceptionForAccounts("Cannot log delete accounts from group event performed by user", me, removed, e);
    }
}
Also used : AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) OrmException(com.google.gwtorm.server.OrmException) AccountGroupMemberAudit(com.google.gerrit.reviewdb.client.AccountGroupMemberAudit) ArrayList(java.util.ArrayList) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 3 with AccountGroupMember

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

the class DbGroupMemberAuditListener method onAddAccountsToGroup.

@Override
public void onAddAccountsToGroup(Account.Id me, Collection<AccountGroupMember> added) {
    List<AccountGroupMemberAudit> auditInserts = new ArrayList<>();
    for (AccountGroupMember m : added) {
        AccountGroupMemberAudit audit = new AccountGroupMemberAudit(m, me, TimeUtil.nowTs());
        auditInserts.add(audit);
    }
    try (ReviewDb db = schema.open()) {
        db.accountGroupMembersAudit().insert(auditInserts);
    } catch (OrmException e) {
        logOrmExceptionForAccounts("Cannot log add accounts to group event performed by user", me, added, e);
    }
}
Also used : AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) OrmException(com.google.gwtorm.server.OrmException) AccountGroupMemberAudit(com.google.gerrit.reviewdb.client.AccountGroupMemberAudit) ArrayList(java.util.ArrayList) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 4 with AccountGroupMember

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

the class DeleteMembers method apply.

@Override
public Response<?> apply(GroupResource resource, Input input) throws AuthException, MethodNotAllowedException, UnprocessableEntityException, OrmException, IOException {
    AccountGroup internalGroup = resource.toAccountGroup();
    if (internalGroup == null) {
        throw new MethodNotAllowedException();
    }
    input = Input.init(input);
    final GroupControl control = resource.getControl();
    final Map<Account.Id, AccountGroupMember> members = getMembers(internalGroup.getId());
    final List<AccountGroupMember> toRemove = new ArrayList<>();
    for (final String nameOrEmail : input.members) {
        Account a = accounts.parse(nameOrEmail).getAccount();
        if (!control.canRemoveMember()) {
            throw new AuthException("Cannot delete member: " + a.getFullName());
        }
        final AccountGroupMember m = members.remove(a.getId());
        if (m != null) {
            toRemove.add(m);
        }
    }
    writeAudits(toRemove);
    db.get().accountGroupMembers().delete(toRemove);
    for (final AccountGroupMember m : toRemove) {
        accountCache.evict(m.getAccountId());
    }
    return Response.none();
}
Also used : GroupControl(com.google.gerrit.server.account.GroupControl) Account(com.google.gerrit.reviewdb.client.Account) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException)

Example 5 with AccountGroupMember

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

the class AccountCreator method create.

public synchronized TestAccount create(@Nullable String username, @Nullable String email, @Nullable String fullName, String... groups) throws Exception {
    TestAccount account = accounts.get(username);
    if (account != null) {
        return account;
    }
    try (ReviewDb db = reviewDbProvider.open()) {
        Account.Id id = new Account.Id(db.nextAccountId());
        List<ExternalId> extIds = new ArrayList<>(2);
        String httpPass = null;
        if (username != null) {
            httpPass = "http-pass";
            extIds.add(ExternalId.createUsername(username, id, httpPass));
        }
        if (email != null) {
            extIds.add(ExternalId.createEmail(id, email));
        }
        externalIdsUpdate.create().insert(extIds);
        Account a = new Account(id, TimeUtil.nowTs());
        a.setFullName(fullName);
        a.setPreferredEmail(email);
        accountsUpdate.create().insert(db, a);
        if (groups != null) {
            for (String n : groups) {
                AccountGroup.NameKey k = new AccountGroup.NameKey(n);
                AccountGroup g = groupCache.get(k);
                checkArgument(g != null, "group not found: %s", n);
                AccountGroupMember m = new AccountGroupMember(new AccountGroupMember.Key(id, g.getId()));
                db.accountGroupMembers().insert(Collections.singleton(m));
            }
        }
        KeyPair sshKey = null;
        if (SshMode.useSsh() && username != null) {
            sshKey = genSshKey();
            authorizedKeys.addKey(id, publicKey(sshKey, email));
            sshKeyCache.evict(username);
        }
        if (username != null) {
            accountCache.evictByUsername(username);
        }
        byEmailCache.evict(email);
        indexer.index(id);
        account = new TestAccount(id, username, email, fullName, sshKey, httpPass);
        if (username != null) {
            accounts.put(username, account);
        }
        return account;
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) KeyPair(com.jcraft.jsch.KeyPair) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Aggregations

AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)11 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)8 Account (com.google.gerrit.reviewdb.client.Account)7 ArrayList (java.util.ArrayList)7 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)4 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)3 OrmException (com.google.gwtorm.server.OrmException)3 GroupDetail (com.google.gerrit.common.data.GroupDetail)2 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)2 AccountGroupById (com.google.gerrit.reviewdb.client.AccountGroupById)2 AccountGroupMemberAudit (com.google.gerrit.reviewdb.client.AccountGroupMemberAudit)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 GroupDescription (com.google.gerrit.common.data.GroupDescription)1 Permission (com.google.gerrit.common.data.Permission)1 InvalidSshKeyException (com.google.gerrit.common.errors.InvalidSshKeyException)1 NameAlreadyUsedException (com.google.gerrit.common.errors.NameAlreadyUsedException)1 NoSuchGroupException (com.google.gerrit.common.errors.NoSuchGroupException)1 AccountInput (com.google.gerrit.extensions.api.accounts.AccountInput)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1