From 27ae9095639bb228a1b7ff86a3ebe4264abf05be Mon Sep 17 00:00:00 2001
From: mschaefers <mschaefers@scoop-gmbh.de>
Date: Thu, 29 Nov 2012 12:33:09 -0500
Subject: [PATCH] feature: when using LdapUserService one can configure Gitblit to fetch all users from ldap that can possibly login. This allows to see newly generated LDAP users instantly in Gitblit. By now an LDAP user had to log in once to appear in GitBlit.

---
 src/com/gitblit/utils/RpcUtils.java |  243 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 236 insertions(+), 7 deletions(-)

diff --git a/src/com/gitblit/utils/RpcUtils.java b/src/com/gitblit/utils/RpcUtils.java
index e584151..ed23dab 100644
--- a/src/com/gitblit/utils/RpcUtils.java
+++ b/src/com/gitblit/utils/RpcUtils.java
@@ -24,12 +24,16 @@
 
 import com.gitblit.Constants;
 import com.gitblit.Constants.RpcRequest;
+import com.gitblit.GitBlitException.UnknownRequestException;
+import com.gitblit.models.RegistrantAccessPermission;
 import com.gitblit.models.FederationModel;
 import com.gitblit.models.FederationProposal;
 import com.gitblit.models.FederationSet;
+import com.gitblit.models.FeedModel;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.ServerSettings;
 import com.gitblit.models.ServerStatus;
+import com.gitblit.models.TeamModel;
 import com.gitblit.models.UserModel;
 import com.google.gson.reflect.TypeToken;
 
@@ -53,6 +57,9 @@
 	private static final Type USERS_TYPE = new TypeToken<Collection<UserModel>>() {
 	}.getType();
 
+	private static final Type TEAMS_TYPE = new TypeToken<Collection<TeamModel>>() {
+	}.getType();
+
 	private static final Type REGISTRATIONS_TYPE = new TypeToken<Collection<FederationModel>>() {
 	}.getType();
 
@@ -60,6 +67,12 @@
 	}.getType();
 
 	private static final Type SETS_TYPE = new TypeToken<Collection<FederationSet>>() {
+	}.getType();
+
+	private static final Type BRANCHES_TYPE = new TypeToken<Map<String, Collection<String>>>() {
+	}.getType();
+
+	public static final Type REGISTRANT_PERMISSIONS_TYPE = new TypeToken<Collection<RegistrantAccessPermission>>() {
 	}.getType();
 
 	/**
@@ -92,7 +105,28 @@
 			req = RpcRequest.LIST_REPOSITORIES;
 		}
 		return remoteURL + Constants.RPC_PATH + "?req=" + req.name().toLowerCase()
-				+ (name == null ? "" : ("&name=" + name));
+				+ (name == null ? "" : ("&name=" + StringUtils.encodeURL(name)));
+	}
+
+	/**
+	 * Returns the version of the RPC protocol on the server.
+	 * 
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return the protocol version
+	 * @throws IOException
+	 */
+	public static int getProtocolVersion(String serverUrl, String account, char[] password)
+			throws IOException {
+		String url = asLink(serverUrl, RpcRequest.GET_PROTOCOL);
+		int protocol = 1;
+		try {
+			protocol = JsonUtils.retrieveJson(url, Integer.class, account, password);
+		} catch (UnknownRequestException e) {
+			// v0.7.0 (protocol 1) did not have this request type 
+		}
+		return protocol;
 	}
 
 	/**
@@ -131,6 +165,24 @@
 	}
 
 	/**
+	 * Tries to pull the gitblit team definitions from the remote gitblit
+	 * instance.
+	 * 
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return a collection of UserModel objects
+	 * @throws IOException
+	 */
+	public static List<TeamModel> getTeams(String serverUrl, String account, char[] password)
+			throws IOException {
+		String url = asLink(serverUrl, RpcRequest.LIST_TEAMS);
+		Collection<TeamModel> models = JsonUtils.retrieveJson(url, TEAMS_TYPE, account, password);
+		List<TeamModel> list = new ArrayList<TeamModel>(models);
+		return list;
+	}
+
+	/**
 	 * Create a repository on the Gitblit server.
 	 * 
 	 * @param repository
@@ -142,6 +194,10 @@
 	 */
 	public static boolean createRepository(RepositoryModel repository, String serverUrl,
 			String account, char[] password) throws IOException {
+		// ensure repository name ends with .git
+		if (!repository.name.endsWith(".git")) {
+			repository.name += ".git";
+		}
 		return doAction(RpcRequest.CREATE_REPOSITORY, null, repository, serverUrl, account,
 				password);
 
@@ -178,6 +234,21 @@
 		return doAction(RpcRequest.DELETE_REPOSITORY, null, repository, serverUrl, account,
 				password);
 
+	}
+	
+	/**
+	 * Clears the repository cache on the Gitblit server.
+	 * 
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return true if the action succeeded
+	 * @throws IOException
+	 */
+	public static boolean clearRepositoryCache(String serverUrl, String account, 
+			char[] password) throws IOException {
+		return doAction(RpcRequest.CLEAR_REPOSITORY_CACHE, null, null, serverUrl, account,
+				password);
 	}
 
 	/**
@@ -228,6 +299,53 @@
 	}
 
 	/**
+	 * Create a team on the Gitblit server.
+	 * 
+	 * @param team
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return true if the action succeeded
+	 * @throws IOException
+	 */
+	public static boolean createTeam(TeamModel team, String serverUrl, String account,
+			char[] password) throws IOException {
+		return doAction(RpcRequest.CREATE_TEAM, null, team, serverUrl, account, password);
+
+	}
+
+	/**
+	 * Send a revised version of the team model to the Gitblit server.
+	 * 
+	 * @param team
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return true if the action succeeded
+	 * @throws IOException
+	 */
+	public static boolean updateTeam(String teamname, TeamModel team, String serverUrl,
+			String account, char[] password) throws IOException {
+		return doAction(RpcRequest.EDIT_TEAM, teamname, team, serverUrl, account, password);
+
+	}
+
+	/**
+	 * Deletes a team from the Gitblit server.
+	 * 
+	 * @param team
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return true if the action succeeded
+	 * @throws IOException
+	 */
+	public static boolean deleteTeam(TeamModel team, String serverUrl, String account,
+			char[] password) throws IOException {
+		return doAction(RpcRequest.DELETE_TEAM, null, team, serverUrl, account, password);
+	}
+
+	/**
 	 * Retrieves the list of users that can access the specified repository.
 	 * 
 	 * @param repository
@@ -243,25 +361,94 @@
 		Collection<String> list = JsonUtils.retrieveJson(url, NAMES_TYPE, account, password);
 		return new ArrayList<String>(list);
 	}
-
+	
 	/**
-	 * Sets the repository membership list.
+	 * Retrieves the list of user access permissions for the specified repository.
 	 * 
 	 * @param repository
-	 * @param memberships
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return list of User-AccessPermission tuples
+	 * @throws IOException
+	 */
+	public static List<RegistrantAccessPermission> getRepositoryMemberPermissions(RepositoryModel repository, 
+			String serverUrl, String account, char [] password) throws IOException {
+		String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORY_MEMBER_PERMISSIONS, repository.name);
+		Collection<RegistrantAccessPermission> list = JsonUtils.retrieveJson(url, REGISTRANT_PERMISSIONS_TYPE, account, password);
+		return new ArrayList<RegistrantAccessPermission>(list);
+	}
+
+	/**
+	 * Sets the repository user access permissions
+	 * 
+	 * @param repository
+	 * @param permissions
 	 * @param serverUrl
 	 * @param account
 	 * @param password
 	 * @return true if the action succeeded
 	 * @throws IOException
 	 */
-	public static boolean setRepositoryMembers(RepositoryModel repository,
-			List<String> memberships, String serverUrl, String account, char[] password)
+	public static boolean setRepositoryMemberPermissions(RepositoryModel repository,
+			List<RegistrantAccessPermission> permissions, String serverUrl, String account, char[] password)
 			throws IOException {
-		return doAction(RpcRequest.SET_REPOSITORY_MEMBERS, repository.name, memberships, serverUrl,
+		return doAction(RpcRequest.SET_REPOSITORY_MEMBER_PERMISSIONS, repository.name, permissions, serverUrl,
 				account, password);
 	}
+	
+	/**
+	 * Retrieves the list of teams that can access the specified repository.
+	 * 
+	 * @param repository
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return list of teams
+	 * @throws IOException
+	 */
+	public static List<String> getRepositoryTeams(RepositoryModel repository, String serverUrl,
+			String account, char[] password) throws IOException {
+		String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORY_TEAMS, repository.name);
+		Collection<String> list = JsonUtils.retrieveJson(url, NAMES_TYPE, account, password);
+		return new ArrayList<String>(list);
+	}
+	
+	/**
+	 * Retrieves the list of team access permissions for the specified repository.
+	 * 
+	 * @param repository
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return list of Team-AccessPermission tuples
+	 * @throws IOException
+	 */
+	public static List<RegistrantAccessPermission> getRepositoryTeamPermissions(RepositoryModel repository, 
+			String serverUrl, String account, char [] password) throws IOException {
+		String url = asLink(serverUrl, RpcRequest.LIST_REPOSITORY_TEAM_PERMISSIONS, repository.name);
+		Collection<RegistrantAccessPermission> list = JsonUtils.retrieveJson(url, REGISTRANT_PERMISSIONS_TYPE, account, password);
+		return new ArrayList<RegistrantAccessPermission>(list);
+	}
 
+	/**
+	 * Sets the repository team access permissions
+	 * 
+	 * @param repository
+	 * @param permissions
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return true if the action succeeded
+	 * @throws IOException
+	 */
+	public static boolean setRepositoryTeamPermissions(RepositoryModel repository,
+			List<RegistrantAccessPermission> permissions, String serverUrl, String account, char[] password)
+			throws IOException {
+		return doAction(RpcRequest.SET_REPOSITORY_TEAM_PERMISSIONS, repository.name, permissions, serverUrl,
+				account, password);
+	}
+	
 	/**
 	 * Retrieves the list of federation registrations. These are the list of
 	 * registrations that this Gitblit instance is pulling from.
@@ -386,6 +573,48 @@
 	}
 
 	/**
+	 * Retrieves a map of local branches in the Gitblit server keyed by
+	 * repository.
+	 * 
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return
+	 * @throws IOException
+	 */
+	public static Map<String, Collection<String>> getBranches(String serverUrl, String account,
+			char[] password) throws IOException {
+		String url = asLink(serverUrl, RpcRequest.LIST_BRANCHES);
+		Map<String, Collection<String>> branches = JsonUtils.retrieveJson(url, BRANCHES_TYPE,
+				account, password);
+		return branches;
+	}
+
+	/**
+	 * Retrieves a list of available branch feeds in the Gitblit server.
+	 * 
+	 * @param serverUrl
+	 * @param account
+	 * @param password
+	 * @return
+	 * @throws IOException
+	 */
+	public static List<FeedModel> getBranchFeeds(String serverUrl, String account, char[] password)
+			throws IOException {
+		List<FeedModel> feeds = new ArrayList<FeedModel>();
+		Map<String, Collection<String>> allBranches = getBranches(serverUrl, account, password);
+		for (Map.Entry<String, Collection<String>> entry : allBranches.entrySet()) {
+			for (String branch : entry.getValue()) {
+				FeedModel feed = new FeedModel();
+				feed.repository = entry.getKey();
+				feed.branch = branch;
+				feeds.add(feed);
+			}
+		}
+		return feeds;
+	}
+
+	/**
 	 * Do the specified administrative action on the Gitblit server.
 	 * 
 	 * @param request

--
Gitblit v1.9.1