From d65f712ea3d8941f4b9145c0630c30c20af80d13 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 11 Nov 2011 17:22:21 -0500
Subject: [PATCH] Documentation. Add javadoc and source jars to the gbapi download.

---
 src/com/gitblit/RpcServlet.java |   85 +++++++++++++++++++++++++++++++++++++-----
 1 files changed, 74 insertions(+), 11 deletions(-)

diff --git a/src/com/gitblit/RpcServlet.java b/src/com/gitblit/RpcServlet.java
index 7cf3a59..068562e 100644
--- a/src/com/gitblit/RpcServlet.java
+++ b/src/com/gitblit/RpcServlet.java
@@ -27,11 +27,15 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.eclipse.jgit.lib.Repository;
+
 import com.gitblit.Constants.RpcRequest;
+import com.gitblit.models.RefModel;
 import com.gitblit.models.RepositoryModel;
-import com.gitblit.models.SettingModel;
+import com.gitblit.models.ServerSettings;
 import com.gitblit.models.UserModel;
 import com.gitblit.utils.HttpUtils;
+import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.RpcUtils;
 
 /**
@@ -66,6 +70,12 @@
 
 		UserModel user = (UserModel) request.getUserPrincipal();
 
+		boolean allowManagement = user != null && user.canAdmin
+				&& GitBlit.getBoolean(Keys.web.enableRpcManagement, false);
+
+		boolean allowAdmin = user != null && user.canAdmin
+				&& GitBlit.getBoolean(Keys.web.enableRpcAdministration, false);
+
 		Object result = null;
 		if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
 			// Determine the Gitblit clone url
@@ -84,6 +94,32 @@
 				repositories.put(url, model);
 			}
 			result = repositories;
+		} else if (RpcRequest.LIST_BRANCHES.equals(reqType)) {
+			// list all local branches in all repositories accessible to user
+			Map<String, List<String>> localBranches = new HashMap<String, List<String>>();
+			List<RepositoryModel> models = GitBlit.self().getRepositoryModels(user);
+			for (RepositoryModel model : models) {
+				if (!model.hasCommits) {
+					// skip empty repository
+					continue;
+				}
+				// get local branches
+				Repository repository = GitBlit.self().getRepository(model.name);
+				List<RefModel> refs = JGitUtils.getLocalBranches(repository, false, -1);
+				if (model.showRemoteBranches) {
+					// add remote branches if repository displays them
+					refs.addAll(JGitUtils.getRemoteBranches(repository, false, -1));
+				}
+				if (refs.size() > 0) {
+					List<String> branches = new ArrayList<String>();
+					for (RefModel ref : refs) {
+						branches.add(ref.getName());
+					}
+					localBranches.put(model.name, branches);
+				}
+				repository.close();
+			}
+			result = localBranches;
 		} else if (RpcRequest.LIST_USERS.equals(reqType)) {
 			// list users
 			List<String> names = GitBlit.self().getAllUsernames();
@@ -158,24 +194,28 @@
 			}
 		} else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
 			// return the list of federation registrations
-			result = GitBlit.self().getFederationRegistrations();
+			if (allowAdmin) {
+				result = GitBlit.self().getFederationRegistrations();
+			} else {
+				response.sendError(notAllowedCode);
+			}
 		} else if (RpcRequest.LIST_FEDERATION_RESULTS.equals(reqType)) {
 			// return the list of federation result registrations
-			if (GitBlit.canFederate()) {
+			if (allowAdmin && GitBlit.canFederate()) {
 				result = GitBlit.self().getFederationResultRegistrations();
 			} else {
 				response.sendError(notAllowedCode);
 			}
 		} else if (RpcRequest.LIST_FEDERATION_PROPOSALS.equals(reqType)) {
 			// return the list of federation proposals
-			if (GitBlit.canFederate()) {
+			if (allowAdmin && GitBlit.canFederate()) {
 				result = GitBlit.self().getPendingFederationProposals();
 			} else {
 				response.sendError(notAllowedCode);
 			}
 		} else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
 			// return the list of federation sets
-			if (GitBlit.canFederate()) {
+			if (allowAdmin && GitBlit.canFederate()) {
 				String gitblitUrl = HttpUtils.getGitblitURL(request);
 				result = GitBlit.self().getFederationSets(gitblitUrl);
 			} else {
@@ -183,15 +223,34 @@
 			}
 		} else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {
 			// return the server's settings
-			if (GitBlit.getBoolean(Keys.web.enableRpcAdministration, false)) {
-				result = GitBlit.self().getSettingsModel();
+			ServerSettings settings = GitBlit.self().getSettingsModel();
+			if (allowAdmin) {
+				// return all settings
+				result = settings;
 			} else {
-				response.sendError(notAllowedCode);
+				// anonymous users get a few settings to allow browser launching
+				List<String> keys = new ArrayList<String>();
+				keys.add(Keys.web.siteName);
+				keys.add(Keys.web.mountParameters);
+				keys.add(Keys.web.syndicationEntries);
+				
+				if (allowManagement) {
+					// keys necessary for repository and/or user management
+					keys.add(Keys.realm.minPasswordLength);
+					keys.add(Keys.realm.passwordStorage);
+					keys.add(Keys.federation.sets);
+				}
+				// build the settings
+				ServerSettings managementSettings = new ServerSettings();
+				for (String key : keys) {
+					managementSettings.add(settings.get(key));
+				}
+				result = managementSettings;
 			}
 		} else if (RpcRequest.EDIT_SETTINGS.equals(reqType)) {
 			// update settings on the server
-			if (GitBlit.getBoolean(Keys.web.enableRpcAdministration, false)) {
-				Collection<SettingModel> settings = deserialize(request, response,
+			if (allowAdmin) {
+				Map<String, String> settings = deserialize(request, response,
 						RpcUtils.SETTINGS_TYPE);
 				GitBlit.self().updateSettings(settings);
 			} else {
@@ -199,7 +258,11 @@
 			}
 		} else if (RpcRequest.LIST_STATUS.equals(reqType)) {
 			// return the server's status information
-			result = GitBlit.self().getStatus();
+			if (allowAdmin) {
+				result = GitBlit.self().getStatus();
+			} else {
+				response.sendError(notAllowedCode);
+			}
 		}
 
 		// send the result of the request

--
Gitblit v1.9.1