use of com.google.gerrit.extensions.common.GroupAuditEventInfo 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;
}
use of com.google.gerrit.extensions.common.GroupAuditEventInfo 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.extensions.common.GroupAuditEventInfo in project gerrit by GerritCodeReview.
the class GroupsIT method getAuditLog.
@Test
public void getAuditLog() throws Exception {
GroupApi g = gApi.groups().create(name("group"));
List<? extends GroupAuditEventInfo> auditEvents = g.auditLog();
assertThat(auditEvents).hasSize(1);
assertMemberAuditEvent(auditEvents.get(0), GroupAuditEventInfo.Type.ADD_USER, admin.id(), admin.id());
g.addMembers(user.username());
auditEvents = g.auditLog();
assertThat(auditEvents).hasSize(2);
assertMemberAuditEvent(auditEvents.get(0), GroupAuditEventInfo.Type.ADD_USER, admin.id(), user.id());
g.removeMembers(user.username());
auditEvents = g.auditLog();
assertThat(auditEvents).hasSize(3);
assertMemberAuditEvent(auditEvents.get(0), GroupAuditEventInfo.Type.REMOVE_USER, admin.id(), user.id());
String otherGroup = name("otherGroup");
gApi.groups().create(otherGroup);
g.addGroups(otherGroup);
auditEvents = g.auditLog();
assertThat(auditEvents).hasSize(4);
assertSubgroupAuditEvent(auditEvents.get(0), GroupAuditEventInfo.Type.ADD_GROUP, admin.id(), otherGroup);
g.removeGroups(otherGroup);
auditEvents = g.auditLog();
assertThat(auditEvents).hasSize(5);
assertSubgroupAuditEvent(auditEvents.get(0), GroupAuditEventInfo.Type.REMOVE_GROUP, admin.id(), otherGroup);
// Add a removed member back again.
g.addMembers(user.username());
auditEvents = g.auditLog();
assertThat(auditEvents).hasSize(6);
assertMemberAuditEvent(auditEvents.get(0), GroupAuditEventInfo.Type.ADD_USER, admin.id(), user.id());
// Add a removed group back again.
g.addGroups(otherGroup);
auditEvents = g.auditLog();
assertThat(auditEvents).hasSize(7);
assertSubgroupAuditEvent(auditEvents.get(0), GroupAuditEventInfo.Type.ADD_GROUP, admin.id(), otherGroup);
Timestamp lastDate = null;
for (GroupAuditEventInfo auditEvent : auditEvents) {
if (lastDate != null) {
assertThat(lastDate).isAtLeast(auditEvent.date);
}
lastDate = auditEvent.date;
}
}
Aggregations