Add REST endpoint to retrieve groups
The '/groups/' REST endpoint lists the groups accessible by the caller. This is the same as using the ls-groups command over SSH, and accepts the same options as query parameters. Information for a certain group can be accessed by '/groups/uuid-<group-uuid>' or '/groups/<group-id>'. Change-Id: I8c21cd1618bdf593ebff7e55f25f31f1e32ae9c1 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
		| @@ -14,93 +14,27 @@ | ||||
|  | ||||
| package com.google.gerrit.sshd.commands; | ||||
|  | ||||
| import com.google.gerrit.common.data.GroupList; | ||||
| import com.google.gerrit.common.errors.NoSuchGroupException; | ||||
| import com.google.gerrit.reviewdb.client.Account; | ||||
| import com.google.gerrit.reviewdb.client.AccountGroup; | ||||
| import com.google.gerrit.server.IdentifiedUser; | ||||
| import com.google.gerrit.server.account.GroupCache; | ||||
| import com.google.gerrit.server.account.VisibleGroups; | ||||
| import com.google.gerrit.server.ioutil.ColumnFormatter; | ||||
| import com.google.gerrit.server.project.ProjectControl; | ||||
| import com.google.gerrit.sshd.SshCommand; | ||||
| import com.google.gwtorm.client.KeyUtil; | ||||
| import com.google.gerrit.server.group.ListGroups; | ||||
| import com.google.gerrit.sshd.BaseCommand; | ||||
| import com.google.inject.Inject; | ||||
|  | ||||
| import org.kohsuke.args4j.Option; | ||||
| import org.apache.sshd.server.Environment; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| public class ListGroupsCommand extends SshCommand { | ||||
| public class ListGroupsCommand extends BaseCommand { | ||||
|   @Inject | ||||
|   private GroupCache groupCache; | ||||
|  | ||||
|   @Inject | ||||
|   private VisibleGroups.Factory visibleGroupsFactory; | ||||
|  | ||||
|   @Inject | ||||
|   private IdentifiedUser.GenericFactory userFactory; | ||||
|  | ||||
|   @Option(name = "--project", aliases = {"-p"}, | ||||
|       usage = "projects for which the groups should be listed") | ||||
|   private final List<ProjectControl> projects = new ArrayList<ProjectControl>(); | ||||
|  | ||||
|   @Option(name = "--visible-to-all", usage = "to list only groups that are visible to all registered users") | ||||
|   private boolean visibleToAll; | ||||
|  | ||||
|   @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; | ||||
|  | ||||
|   @Option(name = "--verbose", aliases = {"-v"}, | ||||
|       usage = "verbose output format with tab-separated columns for the " + | ||||
|           "group name, UUID, description, type, owner group name, " + | ||||
|           "owner group UUID, and whether the group is visible to all") | ||||
|   private boolean verboseOutput; | ||||
|   private ListGroups impl; | ||||
|  | ||||
|   @Override | ||||
|   protected void run() throws Failure { | ||||
|     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(); | ||||
|       } | ||||
|  | ||||
|       final ColumnFormatter formatter = new ColumnFormatter(stdout, '\t'); | ||||
|       for (final AccountGroup g : groupList.getGroups()) { | ||||
|         formatter.addColumn(g.getName()); | ||||
|         if (verboseOutput) { | ||||
|           formatter.addColumn(KeyUtil.decode(g.getGroupUUID().toString())); | ||||
|           formatter.addColumn( | ||||
|               g.getDescription() != null ? g.getDescription() : ""); | ||||
|           formatter.addColumn(g.getType().toString()); | ||||
|           final AccountGroup owningGroup = | ||||
|               groupCache.get(g.getOwnerGroupUUID()); | ||||
|           formatter.addColumn( | ||||
|               owningGroup != null ? owningGroup.getName() : "n/a"); | ||||
|           formatter.addColumn(KeyUtil.decode(g.getOwnerGroupUUID().toString())); | ||||
|           formatter.addColumn(Boolean.toString(g.isVisibleToAll())); | ||||
|   public void start(final Environment env) { | ||||
|     startThread(new CommandRunnable() { | ||||
|       @Override | ||||
|       public void run() throws Exception { | ||||
|         parseCommandLine(impl); | ||||
|         if (impl.getUser() != null && !impl.getProjects().isEmpty()) { | ||||
|           throw new UnloggedFailure(1, "fatal: --user and --project options are not compatible."); | ||||
|         } | ||||
|         formatter.nextLine(); | ||||
|         impl.display(out); | ||||
|       } | ||||
|       formatter.finish(); | ||||
|     } catch (NoSuchGroupException e) { | ||||
|       throw die(e); | ||||
|     } | ||||
|     }); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Edwin Kempin
					Edwin Kempin