Merge "Support changing of group options via REST"
This commit is contained in:
		| @@ -16,6 +16,7 @@ package com.google.gerrit.client.admin; | ||||
|  | ||||
| import com.google.gerrit.client.Dispatcher; | ||||
| import com.google.gerrit.client.Gerrit; | ||||
| import com.google.gerrit.client.VoidResult; | ||||
| import com.google.gerrit.client.groups.GroupApi; | ||||
| import com.google.gerrit.client.rpc.GerritCallback; | ||||
| import com.google.gerrit.client.ui.AccountGroupSuggestOracle; | ||||
| @@ -23,7 +24,6 @@ import com.google.gerrit.client.ui.OnEditEnabler; | ||||
| import com.google.gerrit.client.ui.RPCSuggestOracle; | ||||
| import com.google.gerrit.client.ui.SmallHeading; | ||||
| import com.google.gerrit.common.data.GroupDetail; | ||||
| import com.google.gerrit.common.data.GroupOptions; | ||||
| import com.google.gerrit.reviewdb.client.AccountGroup; | ||||
| import com.google.gwt.event.dom.client.ClickEvent; | ||||
| import com.google.gwt.event.dom.client.ClickHandler; | ||||
| @@ -34,7 +34,6 @@ import com.google.gwt.user.client.ui.VerticalPanel; | ||||
| import com.google.gwtexpui.clippy.client.CopyableLabel; | ||||
| import com.google.gwtexpui.globalkey.client.NpTextArea; | ||||
| import com.google.gwtexpui.globalkey.client.NpTextBox; | ||||
| import com.google.gwtjsonrpc.common.VoidResult; | ||||
|  | ||||
| public class AccountGroupInfoScreen extends AccountGroupScreen { | ||||
|   private CopyableLabel groupUUIDLabel; | ||||
| @@ -130,8 +129,8 @@ public class AccountGroupInfoScreen extends AccountGroupScreen { | ||||
|         final String newOwner = ownerTxt.getText().trim(); | ||||
|         if (newOwner.length() > 0) { | ||||
|           GroupApi.setGroupOwner(getGroupUUID(), newOwner, | ||||
|               new GerritCallback<com.google.gerrit.client.VoidResult>() { | ||||
|                 public void onSuccess(final com.google.gerrit.client.VoidResult result) { | ||||
|               new GerritCallback<VoidResult>() { | ||||
|                 public void onSuccess(final VoidResult result) { | ||||
|                   saveOwner.setEnabled(false); | ||||
|                 } | ||||
|               }); | ||||
| @@ -161,8 +160,8 @@ public class AccountGroupInfoScreen extends AccountGroupScreen { | ||||
|       public void onClick(final ClickEvent event) { | ||||
|         final String txt = descTxt.getText().trim(); | ||||
|         GroupApi.setGroupDescription(getGroupUUID(), txt, | ||||
|             new GerritCallback<com.google.gerrit.client.VoidResult>() { | ||||
|               public void onSuccess(final com.google.gerrit.client.VoidResult result) { | ||||
|             new GerritCallback<VoidResult>() { | ||||
|               public void onSuccess(final VoidResult result) { | ||||
|                 saveDesc.setEnabled(false); | ||||
|               } | ||||
|             }); | ||||
| @@ -190,10 +189,8 @@ public class AccountGroupInfoScreen extends AccountGroupScreen { | ||||
|     saveGroupOptions.addClickHandler(new ClickHandler() { | ||||
|       @Override | ||||
|       public void onClick(final ClickEvent event) { | ||||
|         final GroupOptions groupOptions = | ||||
|             new GroupOptions(visibleToAllCheckBox.getValue()); | ||||
|         Util.GROUP_SVC.changeGroupOptions(getGroupId(), groupOptions, | ||||
|             new GerritCallback<VoidResult>() { | ||||
|         GroupApi.setGroupOptions(getGroupUUID(), | ||||
|             visibleToAllCheckBox.getValue(), new GerritCallback<VoidResult>() { | ||||
|               public void onSuccess(final VoidResult result) { | ||||
|                 saveGroupOptions.setEnabled(false); | ||||
|               } | ||||
|   | ||||
| @@ -119,7 +119,7 @@ public class GroupTable extends NavigationTable<GroupInfo> { | ||||
|       table.setHTML(row, 1, Util.highlight(k.name(), toHighlight)); | ||||
|     } | ||||
|     table.setText(row, 2, k.description()); | ||||
|     if (k.isVisibleToAll()) { | ||||
|     if (k.options().isVisibleToAll()) { | ||||
|       table.setWidget(row, 3, new Image(Gerrit.RESOURCES.greenCheck())); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -64,6 +64,14 @@ public class GroupApi { | ||||
|     group(group).view("owner").put(in, cb); | ||||
|   } | ||||
|  | ||||
|   /** Set the options for a group */ | ||||
|   public static void setGroupOptions(AccountGroup.UUID group, | ||||
|       boolean isVisibleToAll, AsyncCallback<VoidResult> cb) { | ||||
|     GroupOptionsInput in = GroupOptionsInput.create(); | ||||
|     in.isVisibleToAll(isVisibleToAll); | ||||
|     group(group).view("options").put(in, cb); | ||||
|   } | ||||
|  | ||||
|   /** Add member to a group. */ | ||||
|   public static void addMember(AccountGroup.UUID group, String member, | ||||
|       AsyncCallback<MemberInfo> cb) { | ||||
| @@ -185,6 +193,16 @@ public class GroupApi { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   private static class GroupOptionsInput extends JavaScriptObject { | ||||
|     final native void isVisibleToAll(boolean v) /*-{ if(v)this.is_visible_to_all=v; }-*/; | ||||
|  | ||||
|     static GroupOptionsInput create() { | ||||
|       return (GroupOptionsInput) createObject(); | ||||
|     } | ||||
|  | ||||
|     protected GroupOptionsInput() { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   private static class MemberInput extends JavaScriptObject { | ||||
|     final native void init() /*-{ this.members = []; }-*/; | ||||
|   | ||||
| @@ -29,7 +29,7 @@ public class GroupInfo extends JavaScriptObject { | ||||
|  | ||||
|   public final native String id() /*-{ return this.id; }-*/; | ||||
|   public final native String name() /*-{ return this.name; }-*/; | ||||
|   public final native boolean isVisibleToAll() /*-{ return this['visible_to_all'] ? true : false; }-*/; | ||||
|   public final native GroupOptionsInfo options() /*-{ return this.options; }-*/; | ||||
|   public final native String description() /*-{ return this.description; }-*/; | ||||
|   public final native String url() /*-{ return this.url; }-*/; | ||||
|  | ||||
| @@ -46,4 +46,11 @@ public class GroupInfo extends JavaScriptObject { | ||||
|  | ||||
|   protected GroupInfo() { | ||||
|   } | ||||
|  | ||||
|   public static class GroupOptionsInfo extends JavaScriptObject { | ||||
|     public final native boolean isVisibleToAll() /*-{ return this['is_visible_to_all'] ? true : false; }-*/; | ||||
|  | ||||
|     protected GroupOptionsInfo() { | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,25 @@ | ||||
| // 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.extensions.restapi.RestReadView; | ||||
|  | ||||
| public class GetOptions implements RestReadView<GroupResource> { | ||||
|  | ||||
|   @Override | ||||
|   public GroupOptionsInfo apply(GroupResource resource) { | ||||
|     return new GroupOptionsInfo(resource.getGroup()); | ||||
|   } | ||||
| } | ||||
| @@ -25,7 +25,7 @@ public class GroupInfo { | ||||
|   public String id; | ||||
|   public String name; | ||||
|   public String url; | ||||
|   public Boolean visibleToAll; | ||||
|   public GroupOptionsInfo options; | ||||
|  | ||||
|   // These fields are only supplied for internal groups. | ||||
|   public String description; | ||||
| @@ -36,7 +36,7 @@ public class GroupInfo { | ||||
|     id = Url.encode(group.getGroupUUID().get()); | ||||
|     name = Strings.emptyToNull(group.getName()); | ||||
|     url = Strings.emptyToNull(group.getUrl()); | ||||
|     visibleToAll = group.isVisibleToAll() ? true : null; | ||||
|     options = new GroupOptionsInfo(group); | ||||
|  | ||||
|     AccountGroup internalGroup = GroupDescriptions.toAccountGroup(group); | ||||
|     if (internalGroup != null) { | ||||
|   | ||||
| @@ -0,0 +1,31 @@ | ||||
| // 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.data.GroupDescription; | ||||
| import com.google.gerrit.reviewdb.client.AccountGroup; | ||||
|  | ||||
| public class GroupOptionsInfo { | ||||
|   final String kind = "gerritcodereview#groupoptions"; | ||||
|   public Boolean isVisibleToAll; | ||||
|  | ||||
|   public GroupOptionsInfo(GroupDescription.Basic group) { | ||||
|     isVisibleToAll = group.isVisibleToAll() ? true : null; | ||||
|   } | ||||
|  | ||||
|   public GroupOptionsInfo(AccountGroup group) { | ||||
|     isVisibleToAll = group.isVisibleToAll() ? true : null; | ||||
|   } | ||||
| } | ||||
| @@ -49,6 +49,8 @@ public class Module extends RestApiModule { | ||||
|     put(GROUP_KIND, "name").to(PutName.class); | ||||
|     get(GROUP_KIND, "owner").to(GetOwner.class); | ||||
|     put(GROUP_KIND, "owner").to(PutOwner.class); | ||||
|     get(GROUP_KIND, "options").to(GetOptions.class); | ||||
|     put(GROUP_KIND, "options").to(PutOptions.class); | ||||
|  | ||||
|     child(GROUP_KIND, "members").to(MembersCollection.class); | ||||
|     get(MEMBER_KIND).to(GetMember.class); | ||||
|   | ||||
| @@ -0,0 +1,74 @@ | ||||
| // 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.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.RestModifyView; | ||||
| import com.google.gerrit.reviewdb.client.AccountGroup; | ||||
| import com.google.gerrit.reviewdb.server.ReviewDb; | ||||
| import com.google.gerrit.server.account.GroupCache; | ||||
| import com.google.gerrit.server.group.PutOptions.Input; | ||||
| import com.google.gwtorm.server.OrmException; | ||||
| import com.google.inject.Inject; | ||||
|  | ||||
| import java.util.Collections; | ||||
|  | ||||
| public class PutOptions implements RestModifyView<GroupResource, Input> { | ||||
|   static class Input { | ||||
|     Boolean isVisibleToAll; | ||||
|   } | ||||
|  | ||||
|   private final GroupCache groupCache; | ||||
|   private final ReviewDb db; | ||||
|  | ||||
|   @Inject | ||||
|   PutOptions(GroupCache groupCache, ReviewDb db) { | ||||
|     this.groupCache = groupCache; | ||||
|     this.db = db; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public GroupOptionsInfo apply(GroupResource resource, Input input) | ||||
|       throws MethodNotAllowedException, AuthException, BadRequestException, | ||||
|       ResourceNotFoundException, OrmException { | ||||
|     if (resource.toAccountGroup() == null) { | ||||
|       throw new MethodNotAllowedException(); | ||||
|     } else if (!resource.getControl().isOwner()) { | ||||
|       throw new AuthException("Not group owner"); | ||||
|     } | ||||
|  | ||||
|     if (input == null) { | ||||
|       throw new BadRequestException("options are required"); | ||||
|     } | ||||
|     if (input.isVisibleToAll == null) { | ||||
|       input.isVisibleToAll = false; | ||||
|     } | ||||
|  | ||||
|     AccountGroup group = db.accountGroups().get( | ||||
|         resource.toAccountGroup().getId()); | ||||
|     if (group == null) { | ||||
|       throw new ResourceNotFoundException(); | ||||
|     } | ||||
|  | ||||
|     group.setVisibleToAll(input.isVisibleToAll); | ||||
|     db.accountGroups().update(Collections.singleton(group)); | ||||
|     groupCache.evict(group); | ||||
|  | ||||
|     return new GroupOptionsInfo(group); | ||||
|   } | ||||
| } | ||||
| @@ -88,8 +88,8 @@ public class ListGroupsCommand extends BaseCommand { | ||||
|           formatter.addColumn(Strings.nullToEmpty(info.description)); | ||||
|           formatter.addColumn(o != null ? o.getName() : "n/a"); | ||||
|           formatter.addColumn(o != null ? o.getGroupUUID().get() : ""); | ||||
|           formatter.addColumn(Boolean.toString( | ||||
|               Objects.firstNonNull(info.visibleToAll, Boolean.FALSE))); | ||||
|           formatter.addColumn(Boolean.toString(Objects.firstNonNull( | ||||
|               info.options.isVisibleToAll, Boolean.FALSE))); | ||||
|         } | ||||
|         formatter.nextLine(); | ||||
|       } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 David Pursehouse
					David Pursehouse