use of com.google.gerrit.entities.AccountGroupMemberAudit in project gerrit by GerritCodeReview.
the class AuditLogReaderTest method createGroupAsUserIdent.
@Test
public void createGroupAsUserIdent() throws Exception {
InternalGroup group = createGroupAsUser(1, "test-group");
AccountGroup.UUID uuid = group.getGroupUUID();
AccountGroupMemberAudit expAudit = createExpMemberAudit(group.getId(), userId, userId, getTipTimestamp(uuid));
assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expAudit);
}
use of com.google.gerrit.entities.AccountGroupMemberAudit 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();
}
use of com.google.gerrit.entities.AccountGroupMemberAudit 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);
}
use of com.google.gerrit.entities.AccountGroupMemberAudit in project gerrit by GerritCodeReview.
the class AuditLogReaderTest method addMultiMembers.
@Test
public void addMultiMembers() throws Exception {
InternalGroup group = createGroupAsUser(1, "test-group");
AccountGroup.Id groupId = group.getId();
AccountGroup.UUID uuid = group.getGroupUUID();
AccountGroupMemberAudit expAudit1 = createExpMemberAudit(groupId, userId, userId, getTipTimestamp(uuid));
assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expAudit1);
Account.Id id1 = Account.id(100002);
Account.Id id2 = Account.id(100003);
addMembers(uuid, ImmutableSet.of(id1, id2));
AccountGroupMemberAudit expAudit2 = createExpMemberAudit(groupId, id1, userId, getTipTimestamp(uuid));
AccountGroupMemberAudit expAudit3 = createExpMemberAudit(groupId, id2, userId, getTipTimestamp(uuid));
assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expAudit1, expAudit2, expAudit3).inOrder();
}
use of com.google.gerrit.entities.AccountGroupMemberAudit in project gerrit by GerritCodeReview.
the class AuditLogReaderTest method addAndRemoveMember.
@Test
public void addAndRemoveMember() throws Exception {
InternalGroup group = createGroupAsUser(1, "test-group");
AccountGroup.UUID uuid = group.getGroupUUID();
AccountGroupMemberAudit expAudit1 = createExpMemberAudit(group.getId(), userId, userId, getTipTimestamp(uuid));
assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expAudit1);
// User adds account 100002 to the group.
Account.Id id = Account.id(100002);
addMembers(uuid, ImmutableSet.of(id));
AccountGroupMemberAudit expAudit2 = createExpMemberAudit(group.getId(), id, userId, getTipTimestamp(uuid));
assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expAudit1, expAudit2).inOrder();
// User removes account 100002 from the group.
removeMembers(uuid, ImmutableSet.of(id));
expAudit2 = expAudit2.toBuilder().removed(userId, getTipTimestamp(uuid)).build();
assertThat(auditLogReader.getMembersAudit(allUsersRepo, uuid)).containsExactly(expAudit1, expAudit2).inOrder();
}
Aggregations