From 7569e8f542ffcc049797f0cf5b79562d5852fb30 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 10 Apr 2014 18:58:09 -0400
Subject: [PATCH] Improve Sparkleshare integration, but leave disabled for now

---
 src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java |   47 +++++++++++++++++++++++++++++++++--------------
 src/main/distrib/data/clientapps.json                            |    2 +-
 src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java  |   10 +++++-----
 3 files changed, 39 insertions(+), 20 deletions(-)

diff --git a/src/main/distrib/data/clientapps.json b/src/main/distrib/data/clientapps.json
index 31e53ef..a19cbcc 100644
--- a/src/main/distrib/data/clientapps.json
+++ b/src/main/distrib/data/clientapps.json
@@ -82,7 +82,7 @@
 		"title": "SparkleShare\u2122",
 		"description": "an open source collaboration and sharing tool",
 		"legal": "released under the GPLv3 open source license",
-		"cloneUrl": "sparkleshare://addProject/${baseUrl}/sparkleshare/${repoUrl}.xml",
+		"cloneUrl": "sparkleshare://addProject/${baseUrl}/sparkleshare/${username}@${repository}.xml",
 		"productUrl": "http://sparkleshare.org",
 		"transports": [ "ssh" ],
 		"platforms": [ "windows", "macintosh", "linux" ],
diff --git a/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java b/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java
index d7f00c6..150dd68 100644
--- a/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java
+++ b/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java
@@ -16,13 +16,13 @@
 package com.gitblit.servlet;
 
 import java.io.IOException;
+import java.net.URL;
 import java.text.MessageFormat;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import com.gitblit.Constants;
 import com.gitblit.IStoredSettings;
 import com.gitblit.Keys;
 import com.gitblit.dagger.DaggerServlet;
@@ -77,6 +77,12 @@
 			javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
 			java.io.IOException {
 
+		int sshPort = settings.getInteger(Keys.git.sshPort, 0);
+		if (sshPort == 0) {
+			response.setStatus(HttpServletResponse.SC_FORBIDDEN);
+			response.getWriter().append("SSH is not active on this server!");
+			return;
+		}
 		// extract repo name from request
 		String repoUrl = request.getPathInfo().substring(1);
 
@@ -85,25 +91,32 @@
 			repoUrl = repoUrl.substring(0, repoUrl.length() - 4);
 		}
 
-		String servletPath =  Constants.R_PATH;
-
-		int schemeIndex = repoUrl.indexOf("://") + 3;
-		String host = repoUrl.substring(0, repoUrl.indexOf('/', schemeIndex));
-		String path = repoUrl.substring(repoUrl.indexOf(servletPath) + servletPath.length());
 		String username = null;
+		String path;
 		int fetchIndex = repoUrl.indexOf('@');
 		if (fetchIndex > -1) {
-			username = repoUrl.substring(schemeIndex, fetchIndex);
+			username = repoUrl.substring(0, fetchIndex);
+			path = repoUrl.substring(fetchIndex + 1);
+		} else {
+			path = repoUrl;
 		}
+
+		String host = request.getServerName();
+		String url = settings.getString(Keys.web.canonicalUrl, "https://localhost:8443");
+		if (!StringUtils.isEmpty(url) && url.indexOf("localhost") == -1) {
+			host = new URL(url).getHost();
+		}
+
 		UserModel user;
 		if (StringUtils.isEmpty(username)) {
 			user = authenticationManager.authenticate(request);
 		} else {
 			user = userManager.getUserModel(username);
 		}
-		if (user == null) {
-			user = UserModel.ANONYMOUS;
-			username = "";
+		if (user == null || user.disabled) {
+			response.setStatus(HttpServletResponse.SC_FORBIDDEN);
+			response.getWriter().append("Access is not permitted!");
+			return;
 		}
 
 		// ensure that the requested repository exists
@@ -114,14 +127,20 @@
 			return;
 		}
 
+		if (!user.canRewindRef(model)) {
+			response.setStatus(HttpServletResponse.SC_FORBIDDEN);
+			response.getWriter().append(MessageFormat.format("{0} does not have RW+ permissions to \"{1}\"!", user.username, model.name));
+		}
+
 		StringBuilder sb = new StringBuilder();
 		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
 		sb.append("<sparkleshare><invite>\n");
-		sb.append(MessageFormat.format("<address>{0}</address>\n", host));
-		sb.append(MessageFormat.format("<remote_path>{0}{1}</remote_path>\n", servletPath, model.name));
-		if (settings.getInteger(Keys.fanout.port, 0) > 0) {
+		sb.append(MessageFormat.format("<address>ssh://{0}@{1}:{2,number,0}/</address>\n", user.username, host, sshPort));
+		sb.append(MessageFormat.format("<remote_path>/{0}</remote_path>\n", model.name));
+		int fanoutPort = settings.getInteger(Keys.fanout.port, 0);
+		if (fanoutPort > 0) {
 			// Gitblit is running it's own fanout service for pubsub notifications
-			sb.append(MessageFormat.format("<announcements_url>tcp://{0}:{1}</announcements_url>\n", request.getServerName(), settings.getString(Keys.fanout.port, "")));
+			sb.append(MessageFormat.format("<announcements_url>tcp://{0}:{1,number,0}</announcements_url>\n", request.getServerName(), fanoutPort));
 		}
 		sb.append("</invite></sparkleshare>\n");
 
diff --git a/src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java b/src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java
index 0f31b31..938226a 100644
--- a/src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java
+++ b/src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java
@@ -210,7 +210,7 @@
 		return urlPanel;
 	}
 
-	protected Fragment createApplicationMenus(String wicketId, UserModel user, final RepositoryModel repository, final List<RepositoryUrl> repositoryUrls) {
+	protected Fragment createApplicationMenus(String wicketId, final UserModel user, final RepositoryModel repository, final List<RepositoryUrl> repositoryUrls) {
 		final List<GitClientApplication> displayedApps = new ArrayList<GitClientApplication>();
 		final String userAgent = ((WebClientInfo) GitBlitWebSession.get().getClientInfo()).getUserAgent();
 
@@ -309,13 +309,13 @@
 
 						if (!StringUtils.isEmpty(clientApp.cloneUrl)) {
 							// custom registered url
-							String url = substitute(clientApp.cloneUrl, repoUrl.url, baseURL);
+							String url = substitute(clientApp.cloneUrl, repoUrl.url, baseURL, user.username, repository.name);
 							fragment.add(new LinkPanel("content", "applicationMenuItem", getString("gb.clone") + " " + repoUrl.url, url));
 							repoLinkItem.add(fragment);
 							fragment.add(new Label("copyFunction").setVisible(false));
 						} else if (!StringUtils.isEmpty(clientApp.command)) {
 							// command-line
-							String command = substitute(clientApp.command, repoUrl.url, baseURL);
+							String command = substitute(clientApp.command, repoUrl.url, baseURL, user.username, repository.name);
 							Label content = new Label("content", command);
 							WicketUtils.setCssClass(content, "commandMenuItem");
 							fragment.add(content);
@@ -334,8 +334,8 @@
 		return applicationMenus;
 	}
 
-	protected String substitute(String pattern, String repoUrl, String baseUrl) {
-		return pattern.replace("${repoUrl}", repoUrl).replace("${baseUrl}", baseUrl);
+	protected String substitute(String pattern, String repoUrl, String baseUrl, String username, String repository) {
+		return pattern.replace("${repoUrl}", repoUrl).replace("${baseUrl}", baseUrl).replace("${username}", username).replace("${repository}", repository);
 	}
 
 	protected Label createPermissionBadge(String wicketId, RepositoryUrl repoUrl) {

--
Gitblit v1.9.1