From 6da45ac87e39c2efeeb2849271ac3c555f106ab9 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 10 Apr 2014 18:58:10 -0400
Subject: [PATCH] Delete ssh public keys when user is deleted

---
 src/main/java/com/gitblit/transport/ssh/gitblit/KeysDispatcher.java |   23 +++++++++++++----------
 1 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/KeysDispatcher.java b/src/main/java/com/gitblit/transport/ssh/gitblit/KeysDispatcher.java
index b8c18a0..61764c4 100644
--- a/src/main/java/com/gitblit/transport/ssh/gitblit/KeysDispatcher.java
+++ b/src/main/java/com/gitblit/transport/ssh/gitblit/KeysDispatcher.java
@@ -30,6 +30,7 @@
 import com.gitblit.transport.ssh.commands.CommandMetaData;
 import com.gitblit.transport.ssh.commands.DispatchCommand;
 import com.gitblit.transport.ssh.commands.SshCommand;
+import com.gitblit.transport.ssh.commands.UsageExample;
 import com.gitblit.utils.FlipTable;
 import com.gitblit.utils.FlipTable.Borders;
 
@@ -50,11 +51,12 @@
 	}
 
 	@CommandMetaData(name = "add", description = "Add an SSH public key to your account")
+	@UsageExample(syntax = "cat ~/.ssh/id_rsa.pub | ${ssh} ${cmd} -", description = "Upload your SSH public key and add it to your account")
 	public static class AddKey extends BaseKeyCommand {
 
 		protected final Logger log = LoggerFactory.getLogger(getClass());
 
-		@Argument(metaVar = "<stdin>|KEY", usage = "the key to add")
+		@Argument(metaVar = "-|<KEY>", usage = "the key(s) to add", required = true)
 		private List<String> addKeys = new ArrayList<String>();
 
 		@Override
@@ -70,6 +72,7 @@
 	}
 
 	@CommandMetaData(name = "remove", aliases = { "rm" }, description = "Remove an SSH public key from your account")
+	@UsageExample(syntax = "${cmd} 2", description = "Remove the SSH key identified as #2 in `keys list`")
 	public static class RemoveKey extends BaseKeyCommand {
 
 		protected final Logger log = LoggerFactory.getLogger(getClass());
@@ -131,7 +134,7 @@
 		}
 	}
 
-	@CommandMetaData(name = "list", aliases = { "ls" }, description = "List your registered public keys")
+	@CommandMetaData(name = "list", aliases = { "ls" }, description = "List your registered SSH public keys")
 	public static class ListKeys extends SshCommand {
 
 		@Option(name = "-L", usage = "list complete public key parameters")
@@ -142,10 +145,6 @@
 			IPublicKeyManager keyManager = getContext().getGitblit().getPublicKeyManager();
 			String username = getContext().getClient().getUsername();
 			List<SshKey> keys = keyManager.getKeys(username);
-			if (keys == null || keys.isEmpty()) {
-				stdout.println("You have not registered any public keys for ssh authentication.");
-				return;
-			}
 
 			if (showRaw) {
 				asRaw(keys);
@@ -156,6 +155,9 @@
 
 		/* output in the same format as authorized_keys */
 		protected void asRaw(List<SshKey> keys) {
+			if (keys == null) {
+				return;
+			}
 			for (SshKey key : keys) {
 				stdout.println(key.getRawData());
 			}
@@ -163,15 +165,16 @@
 
 		protected void asTable(List<SshKey> keys) {
 			String[] headers = { "#", "Fingerprint", "Comment", "Type" };
-			String[][] data = new String[keys.size()][];
-			for (int i = 0; i < keys.size(); i++) {
+			int len = keys == null ? 0 : keys.size();
+			Object[][] data = new Object[len][];
+			for (int i = 0; i < len; i++) {
 				// show 1-based index numbers with the fingerprint
 				// this is useful for comparing with "ssh-add -l"
 				SshKey k = keys.get(i);
-				data[i] = new String[] { "" + (i + 1), k.getFingerprint(), k.getComment(), k.getAlgorithm() };
+				data[i] = new Object[] { (i + 1), k.getFingerprint(), k.getComment(), k.getAlgorithm() };
 			}
 
-			stdout.println(FlipTable.of(headers, data, Borders.BODY_COLS));
+			stdout.println(FlipTable.of(headers, data, Borders.BODY_HCOLS));
 		}
 	}
 }

--
Gitblit v1.9.1