Support removing of groups from a group via REST

A group can be removed from a group by DELETE on
'/groups/*/groups/<group>'.

In addition multiple groups can be removed from a group by
POST on '/groups/*/groups.delete'.

This is consistent with how members can be removed from a group.

The WebUI was adapted to use the new REST API for removing groups
from a group.

Change-Id: I924611b6c413b65d666166758b7d446b782379c7
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2013-01-29 10:32:16 +01:00
committed by Shawn Pearce
parent 9dd764a380
commit d051f95685
4 changed files with 186 additions and 4 deletions

View File

@@ -0,0 +1,163 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.group;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.AccountGroupIncludeByUuid;
import com.google.gerrit.reviewdb.client.AccountGroupIncludeByUuidAudit;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.BadRequestHandler;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.GroupControl;
import com.google.gerrit.server.account.GroupIncludeCache;
import com.google.gerrit.server.group.AddIncludedGroups.Input;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.List;
import java.util.Map;
public class DeleteIncludedGroups implements RestModifyView<GroupResource, Input> {
private final Provider<GroupsCollection> groupsCollection;
private final GroupIncludeCache groupIncludeCache;
private final ReviewDb db;
private final Provider<CurrentUser> self;
@Inject
DeleteIncludedGroups(Provider<GroupsCollection> groupsCollection,
GroupIncludeCache groupIncludeCache, ReviewDb db,
Provider<CurrentUser> self) {
this.groupsCollection = groupsCollection;
this.groupIncludeCache = groupIncludeCache;
this.db = db;
this.self = self;
}
@Override
public Object apply(GroupResource resource, Input input)
throws MethodNotAllowedException, AuthException, BadRequestException,
OrmException {
AccountGroup internalGroup = resource.toAccountGroup();
if (internalGroup == null) {
throw new MethodNotAllowedException();
}
input = Input.init(input);
final GroupControl control = resource.getControl();
final Map<AccountGroup.UUID, AccountGroupIncludeByUuid> includedGroups = getIncludedGroups(internalGroup.getId());
final List<AccountGroupIncludeByUuid> toRemove = Lists.newLinkedList();
final BadRequestHandler badRequest = new BadRequestHandler("removing included groups");
for (final String includedGroup : input.groups) {
try {
final GroupResource includedGroupResource = groupsCollection.get().parse(includedGroup);
if (!control.canRemoveGroup(includedGroupResource.getGroupUUID())) {
throw new AuthException(String.format("Cannot delete group: %s",
includedGroupResource.getName()));
}
final AccountGroupIncludeByUuid g =
includedGroups.remove(includedGroupResource.getGroupUUID());
if (g != null) {
toRemove.add(g);
}
} catch (ResourceNotFoundException e) {
badRequest.addError(new NoSuchGroupException(includedGroup));
}
}
badRequest.failOnError();
if (!toRemove.isEmpty()) {
writeAudits(toRemove);
db.accountGroupIncludesByUuid().delete(toRemove);
for (final AccountGroupIncludeByUuid g : toRemove) {
groupIncludeCache.evictMemberIn(g.getIncludeUUID());
}
groupIncludeCache.evictMembersOf(internalGroup.getGroupUUID());
}
return Response.none();
}
private Map<AccountGroup.UUID, AccountGroupIncludeByUuid> getIncludedGroups(
final AccountGroup.Id groupId) throws OrmException {
final Map<AccountGroup.UUID, AccountGroupIncludeByUuid> groups =
Maps.newHashMap();
for (final AccountGroupIncludeByUuid g : db.accountGroupIncludesByUuid().byGroup(groupId)) {
groups.put(g.getIncludeUUID(), g);
}
return groups;
}
private void writeAudits(final List<AccountGroupIncludeByUuid> toBeRemoved)
throws OrmException {
final Account.Id me = ((IdentifiedUser) self.get()).getAccountId();
final List<AccountGroupIncludeByUuidAudit> auditUpdates = Lists.newLinkedList();
for (final AccountGroupIncludeByUuid g : toBeRemoved) {
AccountGroupIncludeByUuidAudit audit = null;
for (AccountGroupIncludeByUuidAudit a : db
.accountGroupIncludesByUuidAudit().byGroupInclude(g.getGroupId(),
g.getIncludeUUID())) {
if (a.isActive()) {
audit = a;
break;
}
}
if (audit != null) {
audit.removed(me);
auditUpdates.add(audit);
}
}
db.accountGroupIncludesByUuidAudit().update(auditUpdates);
}
static class DeleteIncludedGroup implements
RestModifyView<IncludedGroupResource, DeleteIncludedGroup.Input> {
static class Input {
}
private final Provider<DeleteIncludedGroups> delete;
@Inject
DeleteIncludedGroup(final Provider<DeleteIncludedGroups> delete) {
this.delete = delete;
}
@Override
public Object apply(IncludedGroupResource resource, Input input)
throws MethodNotAllowedException, AuthException, BadRequestException,
OrmException {
AddIncludedGroups.Input in = new AddIncludedGroups.Input();
in.groups = ImmutableList.of(resource.getMember().toString());
return delete.get().apply(resource, in);
}
}
}

View File

@@ -22,6 +22,7 @@ import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.RestApiModule;
import com.google.gerrit.server.group.AddIncludedGroups.UpdateIncludedGroup;
import com.google.gerrit.server.group.AddMembers.UpdateMember;
import com.google.gerrit.server.group.DeleteIncludedGroups.DeleteIncludedGroup;
import com.google.gerrit.server.group.DeleteMembers.DeleteMember;
import com.google.inject.assistedinject.FactoryModuleBuilder;
@@ -40,6 +41,7 @@ public class Module extends RestApiModule {
post(GROUP_KIND, "members.delete").to(DeleteMembers.class);
post(GROUP_KIND, "groups").to(AddIncludedGroups.class);
post(GROUP_KIND, "groups.add").to(AddIncludedGroups.class);
post(GROUP_KIND, "groups.delete").to(DeleteIncludedGroups.class);
get(GROUP_KIND, "description").to(GetDescription.class);
put(GROUP_KIND, "description").to(PutDescription.class);
delete(GROUP_KIND, "description").to(PutDescription.class);
@@ -52,6 +54,7 @@ public class Module extends RestApiModule {
child(GROUP_KIND, "groups").to(IncludedGroupsCollection.class);
get(INCLUDED_GROUP_KIND).to(GetIncludedGroup.class);
put(INCLUDED_GROUP_KIND).to(UpdateIncludedGroup.class);
delete(INCLUDED_GROUP_KIND).to(DeleteIncludedGroup.class);
install(new FactoryModuleBuilder().build(CreateGroup.Factory.class));
}