Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.sshd.commands; 16 : 17 : import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; 18 : 19 : import com.google.common.base.MoreObjects; 20 : import com.google.common.base.Strings; 21 : import com.google.gerrit.entities.AccountGroup; 22 : import com.google.gerrit.entities.InternalGroup; 23 : import com.google.gerrit.extensions.common.AccountInfo; 24 : import com.google.gerrit.server.DynamicOptions; 25 : import com.google.gerrit.server.account.AccountLoader; 26 : import com.google.gerrit.server.account.GroupCache; 27 : import com.google.gerrit.server.account.GroupControl; 28 : import com.google.gerrit.server.ioutil.ColumnFormatter; 29 : import com.google.gerrit.server.permissions.PermissionBackendException; 30 : import com.google.gerrit.server.restapi.group.ListMembers; 31 : import com.google.gerrit.sshd.CommandMetaData; 32 : import com.google.gerrit.sshd.SshCommand; 33 : import com.google.inject.Inject; 34 : import java.io.PrintWriter; 35 : import java.util.List; 36 : import java.util.Optional; 37 : import org.kohsuke.args4j.Argument; 38 : 39 : /** Implements a command that allows the user to see the members of a account. */ 40 : @CommandMetaData( 41 : name = "ls-members", 42 : description = "List the members of a given group", 43 : runsAt = MASTER_OR_SLAVE) 44 1 : public class ListMembersCommand extends SshCommand { 45 : @Inject ListMembersCommandImpl impl; 46 : 47 : @Override 48 : public void run() throws Exception { 49 0 : enableGracefulStop(); 50 0 : impl.display(stdout); 51 0 : } 52 : 53 : @Override 54 : protected void parseCommandLine(DynamicOptions pluginOptions) throws UnloggedFailure { 55 0 : parseCommandLine(impl, pluginOptions); 56 0 : } 57 : 58 : private static class ListMembersCommandImpl extends ListMembers { 59 : @Argument(required = true, usage = "the name of the group", metaVar = "GROUPNAME") 60 : private String name; 61 : 62 : private final GroupCache groupCache; 63 : 64 : @Inject 65 : protected ListMembersCommandImpl( 66 : GroupCache groupCache, 67 : GroupControl.Factory groupControlFactory, 68 : AccountLoader.Factory accountLoaderFactory) { 69 1 : super(groupCache, groupControlFactory, accountLoaderFactory); 70 1 : this.groupCache = groupCache; 71 1 : } 72 : 73 : void display(PrintWriter writer) throws PermissionBackendException { 74 0 : Optional<InternalGroup> group = groupCache.get(AccountGroup.nameKey(name)); 75 0 : String errorText = "Group not found or not visible\n"; 76 : 77 0 : if (!group.isPresent()) { 78 0 : writer.write(errorText); 79 0 : writer.flush(); 80 0 : return; 81 : } 82 : 83 0 : List<AccountInfo> members = getMembers(group.get()); 84 0 : ColumnFormatter formatter = new ColumnFormatter(writer, '\t'); 85 0 : formatter.addColumn("id"); 86 0 : formatter.addColumn("username"); 87 0 : formatter.addColumn("full name"); 88 0 : formatter.addColumn("email"); 89 0 : formatter.nextLine(); 90 0 : for (AccountInfo member : members) { 91 0 : if (member == null) { 92 0 : continue; 93 : } 94 : 95 0 : formatter.addColumn(Integer.toString(member._accountId)); 96 0 : formatter.addColumn(MoreObjects.firstNonNull(member.username, "n/a")); 97 0 : formatter.addColumn(MoreObjects.firstNonNull(Strings.emptyToNull(member.name), "n/a")); 98 0 : formatter.addColumn(MoreObjects.firstNonNull(member.email, "n/a")); 99 0 : formatter.nextLine(); 100 0 : } 101 : 102 0 : formatter.finish(); 103 0 : } 104 : } 105 : }