From 29a371c063588f958ccf75dc7fb0423d8a69aa84 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 27 Jun 2013 11:42:01 -0400
Subject: [PATCH] Disabled SparkleShare menu

---
 src/main/java/com/gitblit/GitBlit.java |  118 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 106 insertions(+), 12 deletions(-)

diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java
index ceb40c1..bcfebb5 100644
--- a/src/main/java/com/gitblit/GitBlit.java
+++ b/src/main/java/com/gitblit/GitBlit.java
@@ -102,6 +102,7 @@
 import com.gitblit.models.GitClientApplication;
 import com.gitblit.models.Metric;
 import com.gitblit.models.ProjectModel;
+import com.gitblit.models.RefModel;
 import com.gitblit.models.RegistrantAccessPermission;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.RepositoryUrl;
@@ -114,6 +115,7 @@
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.Base64;
 import com.gitblit.utils.ByteFormat;
+import com.gitblit.utils.CommitCache;
 import com.gitblit.utils.ContainerUtils;
 import com.gitblit.utils.DeepCopier;
 import com.gitblit.utils.FederationUtils;
@@ -509,7 +511,7 @@
 		if (user == null) {
 			user = UserModel.ANONYMOUS;
 		}
-		String username = UserModel.ANONYMOUS.equals(user) ? "" : user.username;
+		String username = encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username);
 
 		List<RepositoryUrl> list = new ArrayList<RepositoryUrl>();
 		// http/https url
@@ -681,7 +683,15 @@
 	 * @return true if the user service supports credential changes
 	 */
 	public boolean supportsCredentialChanges(UserModel user) {
-		return (user != null && user.isLocalAccount()) || userService.supportsCredentialChanges();
+		if (user == null) {
+			return false;
+		} else if (!Constants.EXTERNAL_ACCOUNT.equals(user.password)) {
+			// credentials likely maintained by Gitblit
+			return userService.supportsCredentialChanges();
+		} else {
+			// credentials are externally maintained
+			return false;
+		}
 	}
 
 	/**
@@ -727,6 +737,7 @@
 			// can not authenticate empty username
 			return null;
 		}
+		String usernameDecoded = decodeUsername(username);
 		String pw = new String(password);
 		if (StringUtils.isEmpty(pw)) {
 			// can not authenticate empty password
@@ -735,7 +746,7 @@
 
 		// check to see if this is the federation user
 		if (canFederate()) {
-			if (username.equalsIgnoreCase(Constants.FEDERATION_USER)) {
+			if (usernameDecoded.equalsIgnoreCase(Constants.FEDERATION_USER)) {
 				List<String> tokens = getFederationTokens();
 				if (tokens.contains(pw)) {
 					// the federation user is an administrator
@@ -750,7 +761,7 @@
 		if (userService == null) {
 			return null;
 		}
-		return userService.authenticate(username, password);
+		return userService.authenticate(usernameDecoded, password);
 	}
 
 	/**
@@ -827,11 +838,22 @@
 		Principal principal = httpRequest.getUserPrincipal();
 		if (principal != null) {
 			String username = principal.getName();
-			if (StringUtils.isEmpty(username)) {
+			if (!StringUtils.isEmpty(username)) {
 				UserModel user = getUserModel(username);
 				if (user != null) {
+					// existing user
 					flagWicketSession(AuthenticationType.CONTAINER);
 					logger.debug(MessageFormat.format("{0} authenticated by servlet container principal from {1}",
+							user.username, httpRequest.getRemoteAddr()));
+					return user;
+				} else if (settings.getBoolean(Keys.realm.container.autoCreateAccounts, true)) {
+					// auto-create user from an authenticated container principal
+					user = new UserModel(username.toLowerCase());
+					user.displayName = username;
+					user.password = Constants.EXTERNAL_ACCOUNT;
+					userService.updateUserModel(user);
+					flagWicketSession(AuthenticationType.CONTAINER);
+					logger.debug(MessageFormat.format("{0} authenticated and created by servlet container principal from {1}",
 							user.username, httpRequest.getRemoteAddr()));
 					return user;
 				} else {
@@ -910,7 +932,10 @@
 		if (userService == null) {
 			return;
 		}
-		if (userService.supportsCookies()) {
+		GitBlitWebSession session = GitBlitWebSession.get();
+		boolean standardLogin = session.authenticationType.isStandard();
+
+		if (userService.supportsCookies() && standardLogin) {
 			Cookie userCookie;
 			if (user == null) {
 				// clear cookie for logout
@@ -945,6 +970,26 @@
 	}
 
 	/**
+	 * Encode the username for user in an url.
+	 * 
+	 * @param name
+	 * @return the encoded name
+	 */
+	protected String encodeUsername(String name) {
+		return name.replace("@", "%40").replace(" ", "%20").replace("\\", "%5C");	
+	}
+
+	/**
+	 * Decode a username from an encoded url.
+	 * 
+	 * @param name
+	 * @return the decoded name
+	 */
+	protected String decodeUsername(String name) {
+		return name.replace("%40", "@").replace("%20", " ").replace("%5C", "\\");
+	}
+	
+	/**
 	 * Returns the list of all users available to the login service.
 	 * 
 	 * @see IUserService.getAllUsernames()
@@ -977,7 +1022,8 @@
 		if (StringUtils.isEmpty(username)) {
 			return false;
 		}
-		return userService.deleteUser(username);
+		String usernameDecoded = decodeUsername(username);
+		return userService.deleteUser(usernameDecoded);
 	}
 
 	/**
@@ -991,7 +1037,8 @@
 		if (StringUtils.isEmpty(username)) {
 			return null;
 		}
-		UserModel user = userService.getUserModel(username);		
+		String usernameDecoded = decodeUsername(username);
+		UserModel user = userService.getUserModel(usernameDecoded);		
 		return user;
 	}
 	
@@ -1428,7 +1475,10 @@
 		}
 		
 		// return sorted copy of cached list
-		List<String> list = new ArrayList<String>(repositoryListCache.keySet());		
+		List<String> list = new ArrayList<String>();
+		for (RepositoryModel model : repositoryListCache.values()) {
+			list.add(model.name);
+		}
 		StringUtils.sortRepositorynames(list);
 		return list;
 	}
@@ -1939,6 +1989,8 @@
 					Constants.CONFIG_GITBLIT, null, "mailingList")));
 			model.indexedBranches = new ArrayList<String>(Arrays.asList(config.getStringList(
 					Constants.CONFIG_GITBLIT, null, "indexBranch")));
+			model.metricAuthorExclusions = new ArrayList<String>(Arrays.asList(config.getStringList(
+					Constants.CONFIG_GITBLIT, null, "metricAuthorExclusions")));
 			
 			// Custom defined properties
 			model.customFields = new LinkedHashMap<String, String>();
@@ -2465,6 +2517,7 @@
 		updateList(config, "postReceiveScript", repository.postReceiveScripts);
 		updateList(config, "mailingList", repository.mailingLists);
 		updateList(config, "indexBranch", repository.indexedBranches);
+		updateList(config, "metricAuthorExclusions", repository.metricAuthorExclusions);
 		
 		// User Defined Properties
 		if (repository.customFields != null) {
@@ -3006,7 +3059,9 @@
 		if (repository != null) {
 			for (String teamname : userService.getTeamnamesForRepositoryRole(repository.name)) {
 				TeamModel team = userService.getTeamModel(teamname);
-				scripts.addAll(team.preReceiveScripts);
+				if (!ArrayUtils.isEmpty(team.preReceiveScripts)) {
+					scripts.addAll(team.preReceiveScripts);
+				}
 			}
 		}
 		return new ArrayList<String>(scripts);
@@ -3056,7 +3111,9 @@
 		if (repository != null) {
 			for (String teamname : userService.getTeamnamesForRepositoryRole(repository.name)) {
 				TeamModel team = userService.getTeamModel(teamname);
-				scripts.addAll(team.postReceiveScripts);
+				if (!ArrayUtils.isEmpty(team.postReceiveScripts)) {
+					scripts.addAll(team.postReceiveScripts);
+				}
 			}
 		}
 		return new ArrayList<String>(scripts);
@@ -3349,7 +3406,8 @@
 		configureJGit();
 		configureFanout();
 		configureGitDaemon();
-		
+		configureCommitCache();
+
 		ContainerUtils.CVE_2007_0450.test();
 	}
 	
@@ -3459,6 +3517,42 @@
 		}
 	}
 	
+	protected void configureCommitCache() {
+		int daysToCache = settings.getInteger(Keys.web.activityCacheDays, 14);
+		if (daysToCache <= 0) {
+			logger.info("commit cache disabled");
+		} else {
+			long start = System.nanoTime();
+			long repoCount = 0;
+			long commitCount = 0;
+			logger.info(MessageFormat.format("preparing {0} day commit cache. please wait...", daysToCache));
+			CommitCache.instance().setCacheDays(daysToCache);
+			Date cutoff = CommitCache.instance().getCutoffDate();
+			for (String repositoryName : getRepositoryList()) {
+				RepositoryModel model = getRepositoryModel(repositoryName);
+				if (model.hasCommits && model.lastChange.after(cutoff)) {
+					repoCount++;
+					Repository repository = getRepository(repositoryName);
+					for (RefModel ref : JGitUtils.getLocalBranches(repository, true, -1)) {
+						if (!ref.getDate().after(cutoff)) {
+							// branch not recently updated
+							continue;
+						}
+						List<?> commits = CommitCache.instance().getCommits(repositoryName, repository, ref.getName());
+						if (commits.size() > 0) {
+							logger.info(MessageFormat.format("  cached {0} commits for {1}:{2}",
+									commits.size(), repositoryName, ref.getName()));
+							commitCount += commits.size();
+						}
+					}
+					repository.close();
+				}
+			}
+			logger.info(MessageFormat.format("built {0} day commit cache of {1} commits across {2} repositories in {3} msecs",
+					daysToCache, commitCount, repoCount, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
+		}
+	}
+	
 	protected final Logger getLogger() {
 		return logger;
 	}

--
Gitblit v1.9.1