Search in sources :

Example 1 with AccountGroupByIdAudit

use of com.google.gerrit.entities.AccountGroupByIdAudit in project gerrit by GerritCodeReview.

the class AuditLogReaderTest method addAndRemoveMembersAndSubgroups.

@Test
public void addAndRemoveMembersAndSubgroups() throws Exception {
    InternalGroup group = createGroupAsUser(1, "test-group");
    AccountGroup.Id groupId = group.getId();
    AccountGroup.UUID uuid = group.getGroupUUID();
    AccountGroupMemberAudit expMemberAudit = createExpMemberAudit(groupId, userId, userId, getTipTimestamp(uuid));
    assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expMemberAudit);
    Account.Id id1 = Account.id(100002);
    Account.Id id2 = Account.id(100003);
    Account.Id id3 = Account.id(100004);
    InternalGroup subgroup1 = createGroupAsUser(2, "test-group-2");
    InternalGroup subgroup2 = createGroupAsUser(3, "test-group-3");
    InternalGroup subgroup3 = createGroupAsUser(4, "test-group-4");
    AccountGroup.UUID subgroupUuid1 = subgroup1.getGroupUUID();
    AccountGroup.UUID subgroupUuid2 = subgroup2.getGroupUUID();
    AccountGroup.UUID subgroupUuid3 = subgroup3.getGroupUUID();
    // Add two accounts.
    addMembers(uuid, ImmutableSet.of(id1, id2));
    AccountGroupMemberAudit expMemberAudit1 = createExpMemberAudit(groupId, id1, userId, getTipTimestamp(uuid));
    AccountGroupMemberAudit expMemberAudit2 = createExpMemberAudit(groupId, id2, userId, getTipTimestamp(uuid));
    assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expMemberAudit, expMemberAudit1, expMemberAudit2).inOrder();
    // Add one subgroup.
    addSubgroups(uuid, ImmutableSet.of(subgroupUuid1));
    AccountGroupByIdAudit expGroupAudit1 = createExpGroupAudit(group.getId(), subgroupUuid1, userId, getTipTimestamp(uuid));
    assertThat(auditLogReader.getSubgroupsAudit(allUsersRepo, uuid)).containsExactly(expGroupAudit1);
    // Remove one account.
    removeMembers(uuid, ImmutableSet.of(id2));
    expMemberAudit2 = expMemberAudit2.toBuilder().removed(userId, getTipTimestamp(uuid)).build();
    assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expMemberAudit, expMemberAudit1, expMemberAudit2).inOrder();
    // Add two subgroups.
    addSubgroups(uuid, ImmutableSet.of(subgroupUuid2, subgroupUuid3));
    AccountGroupByIdAudit expGroupAudit2 = createExpGroupAudit(group.getId(), subgroupUuid2, userId, getTipTimestamp(uuid));
    AccountGroupByIdAudit expGroupAudit3 = createExpGroupAudit(group.getId(), subgroupUuid3, userId, getTipTimestamp(uuid));
    assertThat(auditLogReader.getSubgroupsAudit(allUsersRepo, uuid)).containsExactly(expGroupAudit1, expGroupAudit2, expGroupAudit3).inOrder();
    // Add two account, including a removed account.
    addMembers(uuid, ImmutableSet.of(id2, id3));
    AccountGroupMemberAudit expMemberAudit4 = createExpMemberAudit(groupId, id2, userId, getTipTimestamp(uuid));
    AccountGroupMemberAudit expMemberAudit3 = createExpMemberAudit(groupId, id3, userId, getTipTimestamp(uuid));
    assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expMemberAudit, expMemberAudit1, expMemberAudit2, expMemberAudit4, expMemberAudit3).inOrder();
    // Remove two subgroups.
    removeSubgroups(uuid, ImmutableSet.of(subgroupUuid1, subgroupUuid3));
    expGroupAudit1 = expGroupAudit1.toBuilder().removed(userId, getTipTimestamp(uuid)).build();
    expGroupAudit3 = expGroupAudit3.toBuilder().removed(userId, getTipTimestamp(uuid)).build();
    assertThat(auditLogReader.getSubgroupsAudit(allUsersRepo, uuid)).containsExactly(expGroupAudit1, expGroupAudit2, expGroupAudit3).inOrder();
    // Add back one removed subgroup.
    addSubgroups(uuid, ImmutableSet.of(subgroupUuid1));
    AccountGroupByIdAudit expGroupAudit4 = createExpGroupAudit(group.getId(), subgroupUuid1, userId, getTipTimestamp(uuid));
    assertThat(auditLogReader.getSubgroupsAudit(allUsersRepo, uuid)).containsExactly(expGroupAudit1, expGroupAudit2, expGroupAudit3, expGroupAudit4).inOrder();
}
Also used : Account(com.google.gerrit.entities.Account) AccountGroup(com.google.gerrit.entities.AccountGroup) AccountGroupMemberAudit(com.google.gerrit.entities.AccountGroupMemberAudit) AccountGroupByIdAudit(com.google.gerrit.entities.AccountGroupByIdAudit) InternalGroup(com.google.gerrit.entities.InternalGroup) Test(org.junit.Test)

Example 2 with AccountGroupByIdAudit

use of com.google.gerrit.entities.AccountGroupByIdAudit in project gerrit by GerritCodeReview.

the class GetAuditLog method apply.

@Override
public Response<List<? extends GroupAuditEventInfo>> apply(GroupResource rsrc) throws AuthException, NotInternalGroupException, IOException, ConfigInvalidException, PermissionBackendException {
    GroupDescription.Internal group = rsrc.asInternalGroup().orElseThrow(NotInternalGroupException::new);
    if (!rsrc.getControl().isOwner()) {
        throw new AuthException("Not group owner");
    }
    AccountLoader accountLoader = accountLoaderFactory.create(true);
    List<GroupAuditEventInfo> auditEvents = new ArrayList<>();
    try (Repository allUsersRepo = repoManager.openRepository(allUsers)) {
        for (AccountGroupMemberAudit auditEvent : groups.getMembersAudit(allUsersRepo, group.getGroupUUID())) {
            AccountInfo member = accountLoader.get(auditEvent.memberId());
            auditEvents.add(GroupAuditEventInfo.createAddUserEvent(accountLoader.get(auditEvent.addedBy()), auditEvent.addedOn(), member));
            if (!auditEvent.isActive()) {
                auditEvents.add(GroupAuditEventInfo.createRemoveUserEvent(accountLoader.get(auditEvent.removedBy().orElse(null)), auditEvent.removedOn().orElse(null), member));
            }
        }
        List<AccountGroupByIdAudit> subGroupsAudit = groups.getSubgroupsAudit(allUsersRepo, group.getGroupUUID());
        Map<AccountGroup.UUID, InternalGroup> groups = groupCache.get(subGroupsAudit.stream().map(a -> a.includeUuid()).collect(toImmutableList()));
        for (AccountGroupByIdAudit auditEvent : subGroupsAudit) {
            AccountGroup.UUID includedGroupUUID = auditEvent.includeUuid();
            InternalGroup includedGroup = groups.get(includedGroupUUID);
            GroupInfo member;
            if (includedGroup != null) {
                member = groupJson.format(new InternalGroupDescription(includedGroup));
            } else {
                member = new GroupInfo();
                member.id = Url.encode(includedGroupUUID.get());
                GroupDescription.Basic groupDescription = groupBackend.get(includedGroupUUID);
                if (groupDescription != null) {
                    member.name = groupDescription.getName();
                }
            }
            auditEvents.add(GroupAuditEventInfo.createAddGroupEvent(accountLoader.get(auditEvent.addedBy()), auditEvent.addedOn(), member));
            if (!auditEvent.isActive()) {
                auditEvents.add(GroupAuditEventInfo.createRemoveGroupEvent(accountLoader.get(auditEvent.removedBy().orElse(null)), auditEvent.removedOn().orElse(null), member));
            }
        }
    }
    accountLoader.fill();
    // sort by date and then reverse so that the newest audit event comes first
    auditEvents.sort(comparing((GroupAuditEventInfo a) -> a.date).reversed());
    return Response.ok(auditEvents);
}
Also used : InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) ArrayList(java.util.ArrayList) AccountGroupByIdAudit(com.google.gerrit.entities.AccountGroupByIdAudit) AuthException(com.google.gerrit.extensions.restapi.AuthException) InternalGroup(com.google.gerrit.entities.InternalGroup) GroupDescription(com.google.gerrit.entities.GroupDescription) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) Repository(org.eclipse.jgit.lib.Repository) GroupAuditEventInfo(com.google.gerrit.extensions.common.GroupAuditEventInfo) AccountGroup(com.google.gerrit.entities.AccountGroup) AccountLoader(com.google.gerrit.server.account.AccountLoader) AccountGroupMemberAudit(com.google.gerrit.entities.AccountGroupMemberAudit) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 3 with AccountGroupByIdAudit

use of com.google.gerrit.entities.AccountGroupByIdAudit in project gerrit by GerritCodeReview.

the class AuditLogReaderTest method addMultiSubgroups.

@Test
public void addMultiSubgroups() throws Exception {
    InternalGroup group = createGroupAsUser(1, "test-group");
    AccountGroup.UUID uuid = group.getGroupUUID();
    InternalGroup subgroup1 = createGroupAsUser(2, "test-group-2");
    InternalGroup subgroup2 = createGroupAsUser(3, "test-group-3");
    AccountGroup.UUID subgroupUuid1 = subgroup1.getGroupUUID();
    AccountGroup.UUID subgroupUuid2 = subgroup2.getGroupUUID();
    addSubgroups(uuid, ImmutableSet.of(subgroupUuid1, subgroupUuid2));
    AccountGroupByIdAudit expAudit1 = createExpGroupAudit(group.getId(), subgroupUuid1, userId, getTipTimestamp(uuid));
    AccountGroupByIdAudit expAudit2 = createExpGroupAudit(group.getId(), subgroupUuid2, userId, getTipTimestamp(uuid));
    assertThat(auditLogReader.getSubgroupsAudit(allUsersRepo, uuid)).containsExactly(expAudit1, expAudit2).inOrder();
}
Also used : AccountGroup(com.google.gerrit.entities.AccountGroup) AccountGroupByIdAudit(com.google.gerrit.entities.AccountGroupByIdAudit) InternalGroup(com.google.gerrit.entities.InternalGroup) Test(org.junit.Test)

Example 4 with AccountGroupByIdAudit

use of com.google.gerrit.entities.AccountGroupByIdAudit in project gerrit by GerritCodeReview.

the class AuditLogReaderTest method addAndRemoveSubgroups.

@Test
public void addAndRemoveSubgroups() throws Exception {
    InternalGroup group = createGroupAsUser(1, "test-group");
    AccountGroup.UUID uuid = group.getGroupUUID();
    InternalGroup subgroup = createGroupAsUser(2, "test-group-2");
    AccountGroup.UUID subgroupUuid = subgroup.getGroupUUID();
    addSubgroups(uuid, ImmutableSet.of(subgroupUuid));
    AccountGroupByIdAudit expAudit = createExpGroupAudit(group.getId(), subgroupUuid, userId, getTipTimestamp(uuid));
    assertThat(auditLogReader.getSubgroupsAudit(allUsersRepo, uuid)).containsExactly(expAudit);
    removeSubgroups(uuid, ImmutableSet.of(subgroupUuid));
    expAudit = expAudit.toBuilder().removed(userId, getTipTimestamp(uuid)).build();
    assertThat(auditLogReader.getSubgroupsAudit(allUsersRepo, uuid)).containsExactly(expAudit);
}
Also used : AccountGroup(com.google.gerrit.entities.AccountGroup) AccountGroupByIdAudit(com.google.gerrit.entities.AccountGroupByIdAudit) InternalGroup(com.google.gerrit.entities.InternalGroup) Test(org.junit.Test)

Aggregations

AccountGroup (com.google.gerrit.entities.AccountGroup)4 AccountGroupByIdAudit (com.google.gerrit.entities.AccountGroupByIdAudit)4 InternalGroup (com.google.gerrit.entities.InternalGroup)4 Test (org.junit.Test)3 AccountGroupMemberAudit (com.google.gerrit.entities.AccountGroupMemberAudit)2 Account (com.google.gerrit.entities.Account)1 GroupDescription (com.google.gerrit.entities.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 AccountLoader (com.google.gerrit.server.account.AccountLoader)1 InternalGroupDescription (com.google.gerrit.server.group.InternalGroupDescription)1 ArrayList (java.util.ArrayList)1 Repository (org.eclipse.jgit.lib.Repository)1