ls-groups: Support listing of groups for a user

Add a new option to the 'ls-groups' command that allows to list the
groups for a certain user.

Change-Id: Ie3833c7441b399466a9881e901e3540342e6b3f9
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2011-11-21 10:18:58 +01:00
parent a77f153b00
commit 54eb3530e6
2 changed files with 28 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ SYNOPSIS
[verse]
'ssh' -p <port> <host> 'gerrit ls-groups'
[--project <NAME>]
[--user <NAME>]
[--visible-to-all]
[--type {internal | ldap | system}]
@@ -39,6 +40,19 @@ OPTIONS
Multiple --project options may be specified to specify additional
projects. In this case all groups are listed that have a
permission for any of the specified projects.
+
This option can't be used together with the '--user' option.
--user::
-u::
User for which the groups should be listed. Only groups are
listed that contain this user as a member.
+
The calling user can list the groups for the own user or must be a
member of the privileged 'Administrators' group to list the groups
for other users.
+
This option can't be used together with the '--project' option.
--visible-to-all::
Displays only groups that are visible to all registered users

View File

@@ -18,7 +18,9 @@ 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.reviewdb.Account;
import com.google.gerrit.reviewdb.AccountGroup;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gwtorm.client.OrmException;
@@ -37,6 +39,8 @@ public class ListGroupsCommand extends BaseCommand {
@Inject
private VisibleGroups.Factory visibleGroupsFactory;
@Inject
private IdentifiedUser.GenericFactory userFactory;
@Option(name = "--project", aliases = {"-p"},
usage = "projects for which the groups should be listed")
@@ -48,6 +52,10 @@ public class ListGroupsCommand extends BaseCommand {
@Option(name = "--type", usage = "type of group")
private AccountGroup.Type groupType;
@Option(name = "--user", aliases = {"-u"},
usage = "user for which the groups should be listed")
private Account.Id user;
@Override
public void start(final Environment env) throws IOException {
startThread(new CommandRunnable() {
@@ -62,12 +70,18 @@ public class ListGroupsCommand extends BaseCommand {
private void display() throws Failure {
final PrintWriter stdout = toPrintWriter(out);
try {
if (user != null && !projects.isEmpty()) {
throw new UnloggedFailure(1, "fatal: --user and --project options are not compatible.");
}
final VisibleGroups visibleGroups = visibleGroupsFactory.create();
visibleGroups.setOnlyVisibleToAll(visibleToAll);
visibleGroups.setGroupType(groupType);
final GroupList groupList;
if (!projects.isEmpty()) {
groupList = visibleGroups.get(projects);
} else if (user != null) {
groupList = visibleGroups.get(userFactory.create(user));
} else {
groupList = visibleGroups.get();
}