Search in sources :

Example 1 with AccountGroupMemberAudit

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

the class Schema_151 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
    List<AccountGroup> accountGroups = db.accountGroups().all().toList();
    for (AccountGroup accountGroup : accountGroups) {
        ResultSet<AccountGroupMemberAudit> groupMemberAudits = db.accountGroupMembersAudit().byGroup(accountGroup.getId());
        Optional<Timestamp> firstTimeMentioned = Streams.stream(groupMemberAudits).map(AccountGroupMemberAudit::getKey).map(Key::getAddedOn).min(Comparator.naturalOrder());
        Timestamp createdOn = firstTimeMentioned.orElseGet(() -> AccountGroup.auditCreationInstantTs());
        accountGroup.setCreatedOn(createdOn);
    }
    db.accountGroups().update(accountGroups);
}
Also used : AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AccountGroupMemberAudit(com.google.gerrit.reviewdb.client.AccountGroupMemberAudit) Timestamp(java.sql.Timestamp)

Example 2 with AccountGroupMemberAudit

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

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

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

the class GetAuditLog method apply.

@Override
public List<? extends GroupAuditEventInfo> apply(GroupResource rsrc) throws AuthException, ResourceNotFoundException, MethodNotAllowedException, OrmException {
    if (rsrc.toAccountGroup() == null) {
        throw new MethodNotAllowedException();
    } else if (!rsrc.getControl().isOwner()) {
        throw new AuthException("Not group owner");
    }
    AccountGroup group = db.get().accountGroups().get(rsrc.toAccountGroup().getId());
    if (group == null) {
        throw new ResourceNotFoundException();
    }
    AccountLoader accountLoader = accountLoaderFactory.create(true);
    List<GroupAuditEventInfo> auditEvents = new ArrayList<>();
    for (AccountGroupMemberAudit auditEvent : db.get().accountGroupMembersAudit().byGroup(group.getId()).toList()) {
        AccountInfo member = accountLoader.get(auditEvent.getKey().getParentKey());
        auditEvents.add(GroupAuditEventInfo.createAddUserEvent(accountLoader.get(auditEvent.getAddedBy()), auditEvent.getKey().getAddedOn(), member));
        if (!auditEvent.isActive()) {
            auditEvents.add(GroupAuditEventInfo.createRemoveUserEvent(accountLoader.get(auditEvent.getRemovedBy()), auditEvent.getRemovedOn(), member));
        }
    }
    for (AccountGroupByIdAud auditEvent : db.get().accountGroupByIdAud().byGroup(group.getId()).toList()) {
        AccountGroup.UUID includedGroupUUID = auditEvent.getKey().getIncludeUUID();
        AccountGroup includedGroup = groupCache.get(includedGroupUUID);
        GroupInfo member;
        if (includedGroup != null) {
            member = groupJson.format(GroupDescriptions.forAccountGroup(includedGroup));
        } else {
            GroupDescription.Basic groupDescription = groupBackend.get(includedGroupUUID);
            member = new GroupInfo();
            member.id = Url.encode(includedGroupUUID.get());
            member.name = groupDescription.getName();
        }
        auditEvents.add(GroupAuditEventInfo.createAddGroupEvent(accountLoader.get(auditEvent.getAddedBy()), auditEvent.getKey().getAddedOn(), member));
        if (!auditEvent.isActive()) {
            auditEvents.add(GroupAuditEventInfo.createRemoveGroupEvent(accountLoader.get(auditEvent.getRemovedBy()), auditEvent.getRemovedOn(), member));
        }
    }
    accountLoader.fill();
    // sort by date in reverse order so that the newest audit event comes first
    Collections.sort(auditEvents, new Comparator<GroupAuditEventInfo>() {

        @Override
        public int compare(GroupAuditEventInfo e1, GroupAuditEventInfo e2) {
            return e2.date.compareTo(e1.date);
        }
    });
    return auditEvents;
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) GroupDescription(com.google.gerrit.common.data.GroupDescription) GroupAuditEventInfo(com.google.gerrit.extensions.common.GroupAuditEventInfo) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AccountGroupByIdAud(com.google.gerrit.reviewdb.client.AccountGroupByIdAud) AccountLoader(com.google.gerrit.server.account.AccountLoader) AccountGroupMemberAudit(com.google.gerrit.reviewdb.client.AccountGroupMemberAudit) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Aggregations

AccountGroupMemberAudit (com.google.gerrit.reviewdb.client.AccountGroupMemberAudit)4 ArrayList (java.util.ArrayList)3 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)2 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 OrmException (com.google.gwtorm.server.OrmException)2 GroupDescription (com.google.gerrit.common.data.GroupDescription)1 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)1 GroupAuditEventInfo (com.google.gerrit.extensions.common.GroupAuditEventInfo)1 GroupInfo (com.google.gerrit.extensions.common.GroupInfo)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 AccountGroupByIdAud (com.google.gerrit.reviewdb.client.AccountGroupByIdAud)1 AccountLoader (com.google.gerrit.server.account.AccountLoader)1 Timestamp (java.sql.Timestamp)1