diff --git a/Documentation/cmd-index.txt b/Documentation/cmd-index.txt index a6296b4fa6..c3b165d386 100644 --- a/Documentation/cmd-index.txt +++ b/Documentation/cmd-index.txt @@ -54,6 +54,9 @@ see link:user-upload.html#test_ssh[Testing Your SSH Connection]. 'gerrit approve':: 'Deprecated alias for `gerrit review`.' +link:cmd-ls-groups.html[gerrit ls-groups]:: + List groups visible to the caller. + link:cmd-ls-projects.html[gerrit ls-projects]:: List projects visible to the caller. diff --git a/Documentation/cmd-ls-groups.txt b/Documentation/cmd-ls-groups.txt new file mode 100644 index 0000000000..842abfd738 --- /dev/null +++ b/Documentation/cmd-ls-groups.txt @@ -0,0 +1,44 @@ +gerrit ls-groups +================ + +NAME +---- +gerrit ls-groups - List groups visible to caller + +SYNOPSIS +-------- +[verse] +'ssh' -p 'gerrit ls-groups' + +DESCRIPTION +----------- +Displays the list of group names, one per line, that are visible to +the account of the calling user. + +If the caller is a member of the privileged 'Administrators' group, +all groups are listed. + +ACCESS +------ +Any user who has configured an SSH key. + +SCRIPTING +--------- +This command is intended to be used in scripts. + +EXAMPLES +-------- + +List visible groups: +===== + $ ssh -p 29418 review.example.com gerrit ls-groups + Administrators + Anonymous Users + MyProject_Committers + Project Owners + Registered Users +===== + +GERRIT +------ +Part of link:index.html[Gerrit Code Review] diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountModule.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountModule.java index 8b3468980b..d2e61186b0 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountModule.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountModule.java @@ -35,7 +35,7 @@ public class AccountModule extends RpcServletModule { factory(GroupDetailHandler.Factory.class); factory(MyGroupsFactory.Factory.class); factory(RenameGroup.Factory.class); - factory(VisibleGroups.Factory.class); + factory(VisibleGroupsHandler.Factory.class); } }); rpc(AccountSecurityImpl.class); diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/GroupAdminServiceImpl.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/GroupAdminServiceImpl.java index 6fac53ab94..448baf97b3 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/GroupAdminServiceImpl.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/GroupAdminServiceImpl.java @@ -63,7 +63,7 @@ class GroupAdminServiceImpl extends BaseServiceImplementation implements private final CreateGroup.Factory createGroupFactory; private final RenameGroup.Factory renameGroupFactory; private final GroupDetailHandler.Factory groupDetailFactory; - private final VisibleGroups.Factory visibleGroupsFactory; + private final VisibleGroupsHandler.Factory visibleGroupsFactory; @Inject GroupAdminServiceImpl(final Provider schema, @@ -76,7 +76,7 @@ class GroupAdminServiceImpl extends BaseServiceImplementation implements final CreateGroup.Factory createGroupFactory, final RenameGroup.Factory renameGroupFactory, final GroupDetailHandler.Factory groupDetailFactory, - final VisibleGroups.Factory visibleGroupsFactory) { + final VisibleGroupsHandler.Factory visibleGroupsFactory) { super(schema, currentUser); this.accountCache = accountCache; this.groupIncludeCache = groupIncludeCache; diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroupsHandler.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroupsHandler.java new file mode 100644 index 0000000000..54f91f7c86 --- /dev/null +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroupsHandler.java @@ -0,0 +1,39 @@ +// Copyright (C) 2011 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.httpd.rpc.account; + +import com.google.gerrit.common.data.GroupList; +import com.google.gerrit.httpd.rpc.Handler; +import com.google.gerrit.server.account.VisibleGroups; +import com.google.inject.Inject; + +public class VisibleGroupsHandler extends Handler { + + interface Factory { + VisibleGroupsHandler create(); + } + + private final VisibleGroups.Factory visibleGroupsFactory; + + @Inject + VisibleGroupsHandler(final VisibleGroups.Factory visibleGroupsFactory) { + this.visibleGroupsFactory = visibleGroupsFactory; + } + + @Override + public GroupList call() throws Exception { + return visibleGroupsFactory.create().get(); + } +} diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroups.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java similarity index 81% rename from gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroups.java rename to gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java index 163d7e39e2..c47c2317a5 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/VisibleGroups.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java @@ -10,17 +10,16 @@ // 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. +// limitations under the License -package com.google.gerrit.httpd.rpc.account; +package com.google.gerrit.server.account; import com.google.gerrit.common.data.GroupDetail; import com.google.gerrit.common.data.GroupList; -import com.google.gerrit.httpd.rpc.Handler; +import com.google.gerrit.common.errors.NoSuchGroupException; import com.google.gerrit.reviewdb.AccountGroup; import com.google.gerrit.server.IdentifiedUser; -import com.google.gerrit.server.account.GroupCache; -import com.google.gerrit.server.account.GroupControl; +import com.google.gwtorm.client.OrmException; import com.google.inject.Inject; import com.google.inject.Provider; @@ -29,30 +28,29 @@ import org.apache.commons.collections.CollectionUtils; import java.util.ArrayList; import java.util.List; -public class VisibleGroups extends Handler { +public class VisibleGroups { - interface Factory { + public interface Factory { VisibleGroups create(); } private final Provider identifiedUser; private final GroupCache groupCache; private final GroupControl.Factory groupControlFactory; - private final GroupDetailHandler.Factory groupDetailFactory; + private final GroupDetailFactory.Factory groupDetailFactory; @Inject VisibleGroups(final Provider currentUser, final GroupCache groupCache, final GroupControl.Factory groupControlFactory, - final GroupDetailHandler.Factory groupDetailFactory) { + final GroupDetailFactory.Factory groupDetailFactory) { this.identifiedUser = currentUser; this.groupCache = groupCache; this.groupControlFactory = groupControlFactory; this.groupDetailFactory = groupDetailFactory; } - @Override - public GroupList call() throws Exception { + public GroupList get() throws OrmException, NoSuchGroupException { final IdentifiedUser user = identifiedUser.get(); final List list = new ArrayList(); if (user.getCapabilities().canAdministrateServer()) { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritRequestModule.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritRequestModule.java index 42c2531de7..afd524cd94 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritRequestModule.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritRequestModule.java @@ -26,6 +26,7 @@ import com.google.gerrit.server.account.GroupDetailFactory; import com.google.gerrit.server.account.GroupMembersFactory; import com.google.gerrit.server.account.PerformCreateGroup; import com.google.gerrit.server.account.PerformRenameGroup; +import com.google.gerrit.server.account.VisibleGroups; import com.google.gerrit.server.git.CreateCodeReviewNotes; import com.google.gerrit.server.git.MergeOp; import com.google.gerrit.server.git.MetaDataUpdate; @@ -91,6 +92,7 @@ public class GerritRequestModule extends FactoryModule { factory(RegisterNewEmailSender.Factory.class); factory(PerformCreateGroup.Factory.class); factory(PerformRenameGroup.Factory.class); + factory(VisibleGroups.Factory.class); factory(GroupDetailFactory.Factory.class); factory(GroupMembersFactory.Factory.class); } diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/DefaultCommandModule.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/DefaultCommandModule.java index a369b86a42..5cee06ee96 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/DefaultCommandModule.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/DefaultCommandModule.java @@ -37,6 +37,7 @@ public class DefaultCommandModule extends CommandModule { command(gerrit).toProvider(new DispatchCommandProvider(gerrit)); command(gerrit, "flush-caches").to(FlushCaches.class); command(gerrit, "ls-projects").to(ListProjects.class); + command(gerrit, "ls-groups").to(ListGroupsCommand.class); command(gerrit, "query").to(Query.class); command(gerrit, "show-caches").to(ShowCaches.class); command(gerrit, "show-connections").to(ShowConnections.class); diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java new file mode 100644 index 0000000000..b25bee4334 --- /dev/null +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java @@ -0,0 +1,62 @@ +// Copyright (C) 2011 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.sshd.commands; + +import com.google.gerrit.common.data.GroupDetail; +import com.google.gerrit.common.data.GroupList; +import com.google.gerrit.common.errors.NoSuchGroupException; +import com.google.gerrit.server.account.VisibleGroups; +import com.google.gerrit.sshd.BaseCommand; +import com.google.gwtorm.client.OrmException; +import com.google.inject.Inject; + +import org.apache.sshd.server.Environment; + +import java.io.IOException; +import java.io.PrintWriter; + +public class ListGroupsCommand extends BaseCommand { + + @Inject + private VisibleGroups.Factory visibleGroupsFactory; + + @Override + public void start(final Environment env) throws IOException { + startThread(new CommandRunnable() { + @Override + public void run() throws Exception { + parseCommandLine(); + ListGroupsCommand.this.display(); + } + }); + } + + private void display() throws Failure { + final PrintWriter stdout = toPrintWriter(out); + try { + final GroupList visibleGroups = + visibleGroupsFactory.create().get(); + for (final GroupDetail groupDetail : visibleGroups.getGroups()) { + stdout.print(groupDetail.group.getName() + "\n"); + } + } catch (OrmException e) { + throw die(e); + } catch (NoSuchGroupException e) { + throw die(e); + } finally { + stdout.flush(); + } + } +}