Support changing the group owner via REST

A new owner group for a group can now be set by PUT on
'/groups/<group>/owner'.

The WebUI was adapted to use this new REST endpoint.

Change-Id: I1c921d65855f9ade91aa9b5db91a6001570d1c6d
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2013-01-30 14:11:42 +01:00
committed by Shawn Pearce
parent f2ed5a5c9b
commit 018caa2d58
5 changed files with 156 additions and 3 deletions

View File

@@ -140,9 +140,9 @@ public class AccountGroupInfoScreen extends AccountGroupScreen {
public void onClick(final ClickEvent event) {
final String newOwner = ownerTxt.getText().trim();
if (newOwner.length() > 0) {
Util.GROUP_SVC.changeGroupOwner(getGroupId(), newOwner,
new GerritCallback<VoidResult>() {
public void onSuccess(final VoidResult result) {
GroupApi.setGroupOwner(getGroupUUID(), newOwner,
new GerritCallback<com.google.gerrit.client.VoidResult>() {
public void onSuccess(final com.google.gerrit.client.VoidResult result) {
saveOwner.setEnabled(false);
}
});

View File

@@ -48,6 +48,14 @@ public class GroupApi {
}
}
/** Set owner for a group */
public static void setGroupOwner(AccountGroup.UUID group,
String owner, AsyncCallback<VoidResult> cb) {
GroupInput in = GroupInput.create();
in.owner(owner);
group(group).view("owner").put(in, cb);
}
/** Add member to a group. */
public static void addMember(AccountGroup.UUID group, String member,
AsyncCallback<MemberInfo> cb) {
@@ -143,6 +151,7 @@ public class GroupApi {
private static class GroupInput extends JavaScriptObject {
final native void description(String d) /*-{ if(d)this.description=d; }-*/;
final native void owner(String o) /*-{ if(o)this.owner=o; }-*/;
static GroupInput create() {
return (GroupInput) createObject();

View File

@@ -0,0 +1,46 @@
// 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.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.account.GroupControl;
import com.google.inject.Inject;
public class GetOwner implements RestReadView<GroupResource> {
private final GroupControl.Factory controlFactory;
@Inject
GetOwner(GroupControl.Factory controlFactory) {
this.controlFactory = controlFactory;
}
@Override
public Object apply(GroupResource resource) throws ResourceNotFoundException {
AccountGroup group = resource.toAccountGroup();
if (group == null) {
throw new ResourceNotFoundException();
}
try {
GroupControl c = controlFactory.validateFor(group.getOwnerGroupUUID());
return new GroupInfo(c.getGroup());
} catch (NoSuchGroupException e) {
throw new ResourceNotFoundException();
}
}
}

View File

@@ -43,6 +43,8 @@ public class Module extends RestApiModule {
get(GROUP_KIND, "description").to(GetDescription.class);
put(GROUP_KIND, "description").to(PutDescription.class);
delete(GROUP_KIND, "description").to(PutDescription.class);
get(GROUP_KIND, "owner").to(GetOwner.class);
put(GROUP_KIND, "owner").to(PutOwner.class);
child(GROUP_KIND, "members").to(MembersCollection.class);
get(MEMBER_KIND).to(GetMember.class);

View File

@@ -0,0 +1,96 @@
// 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.base.Strings;
import com.google.gerrit.common.data.GroupReference;
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.DefaultInput;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.GroupBackend;
import com.google.gerrit.server.account.GroupBackends;
import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.account.GroupControl;
import com.google.gerrit.server.group.PutOwner.Input;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import java.util.Collections;
public class PutOwner implements RestModifyView<GroupResource, Input> {
static class Input {
@DefaultInput
String owner;
}
private final GroupBackend groupBackend;
private final GroupCache groupCache;
private final GroupControl.Factory controlFactory;
private final ReviewDb db;
@Inject
PutOwner(GroupBackend groupBackend, GroupCache groupCache,
GroupControl.Factory controlFactory, ReviewDb db) {
this.groupBackend = groupBackend;
this.groupCache = groupCache;
this.controlFactory = controlFactory;
this.db = db;
}
@Override
public GroupInfo apply(GroupResource resource, Input input)
throws MethodNotAllowedException, AuthException, BadRequestException,
ResourceNotFoundException, OrmException {
AccountGroup group = resource.toAccountGroup();
if (group == null) {
throw new MethodNotAllowedException();
} else if (!resource.getControl().isOwner()) {
throw new AuthException("Not group owner");
}
if (input == null || Strings.isNullOrEmpty(input.owner)) {
throw new BadRequestException("owner is required");
}
GroupReference owner =
GroupBackends.findExactSuggestion(groupBackend, input.owner);
if (owner == null) {
throw new BadRequestException(String.format("No such group: %s", input.owner));
}
try {
GroupControl c = controlFactory.validateFor(owner.getUUID());
group = db.accountGroups().get(group.getId());
if (group == null) {
throw new ResourceNotFoundException();
}
if (!group.getOwnerGroupUUID().equals(owner.getUUID())) {
group.setOwnerGroupUUID(owner.getUUID());
db.accountGroups().update(Collections.singleton(group));
groupCache.evict(group);
}
return new GroupInfo(c.getGroup());
} catch (NoSuchGroupException e) {
throw new BadRequestException(String.format("No such group: %s", input.owner));
}
}
}