From 7e8873a14ccc2cb25213489d7d7ba97f09673831 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 07 Dec 2011 19:14:34 -0500
Subject: [PATCH] Unit testing overhaul.

---
 src/com/gitblit/FileUserService.java |   66 ++++++++++++++++++++++++--------
 1 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/src/com/gitblit/FileUserService.java b/src/com/gitblit/FileUserService.java
index 12164ce..a98e417 100644
--- a/src/com/gitblit/FileUserService.java
+++ b/src/com/gitblit/FileUserService.java
@@ -34,14 +34,19 @@
 import com.gitblit.utils.StringUtils;
 
 /**
- * FileUserService is Gitblit's default user service implementation.
+ * FileUserService is Gitblit's original default user service implementation.
  * 
  * Users and their repository memberships are stored in a simple properties file
  * which is cached and dynamically reloaded when modified.
  * 
+ * This class was deprecated in Gitblit 0.8.0 in favor of ConfigUserService
+ * which is still a human-readable, editable, plain-text file but it is more
+ * flexible for storing additional fields.
+ * 
  * @author James Moger
  * 
  */
+@Deprecated
 public class FileUserService extends FileSettings implements IUserService {
 
 	private final Logger logger = LoggerFactory.getLogger(FileUserService.class);
@@ -50,6 +55,16 @@
 
 	public FileUserService(File realmFile) {
 		super(realmFile.getAbsolutePath());
+	}
+
+	/**
+	 * Setup the user service.
+	 * 
+	 * @param settings
+	 * @since 0.6.1
+	 */
+	@Override
+	public void setup(IStoredSettings settings) {
 	}
 
 	/**
@@ -116,11 +131,20 @@
 		UserModel returnedUser = null;
 		UserModel user = getUserModel(username);
 		if (user.password.startsWith(StringUtils.MD5_TYPE)) {
+			// password digest
 			String md5 = StringUtils.MD5_TYPE + StringUtils.getMD5(new String(password));
 			if (user.password.equalsIgnoreCase(md5)) {
 				returnedUser = user;
 			}
+		} else if (user.password.startsWith(StringUtils.COMBINED_MD5_TYPE)) {
+			// username+password digest
+			String md5 = StringUtils.COMBINED_MD5_TYPE
+					+ StringUtils.getMD5(username.toLowerCase() + new String(password));
+			if (user.password.equalsIgnoreCase(md5)) {
+				returnedUser = user;
+			}
 		} else if (user.password.equals(new String(password))) {
+			// plain-text password
 			returnedUser = user;
 		}
 		return returnedUser;
@@ -149,6 +173,8 @@
 				// Permissions
 				if (role.equalsIgnoreCase(Constants.ADMIN_ROLE)) {
 					model.canAdmin = true;
+				} else if (role.equalsIgnoreCase(Constants.NOT_FEDERATED_ROLE)) {
+					model.excludeFromFederation = true;
 				}
 				break;
 			default:
@@ -188,6 +214,9 @@
 			// Permissions
 			if (model.canAdmin) {
 				roles.add(Constants.ADMIN_ROLE);
+			}
+			if (model.excludeFromFederation) {
+				roles.add(Constants.NOT_FEDERATED_ROLE);
 			}
 
 			StringBuilder sb = new StringBuilder();
@@ -336,12 +365,11 @@
 				StringBuilder sb = new StringBuilder();
 				sb.append(password);
 				sb.append(',');
-				List<String> revisedRoles = new ArrayList<String>();
+
 				// skip first value (password)
 				for (int i = 1; i < values.length; i++) {
 					String value = values[i];
 					if (!value.equalsIgnoreCase(role)) {
-						revisedRoles.add(value);
 						sb.append(value);
 						sb.append(',');
 					}
@@ -382,7 +410,7 @@
 				for (int i = 1; i < roles.length; i++) {
 					String r = roles[i];
 					if (r.equalsIgnoreCase(oldRole)) {
-						needsRenameRole.remove(username);
+						needsRenameRole.add(username);
 						break;
 					}
 				}
@@ -396,13 +424,13 @@
 				StringBuilder sb = new StringBuilder();
 				sb.append(password);
 				sb.append(',');
-				List<String> revisedRoles = new ArrayList<String>();
-				revisedRoles.add(newRole);
+				sb.append(newRole);
+				sb.append(',');
+				
 				// skip first value (password)
 				for (int i = 1; i < values.length; i++) {
 					String value = values[i];
 					if (!value.equalsIgnoreCase(oldRole)) {
-						revisedRoles.add(value);
 						sb.append(value);
 						sb.append(',');
 					}
@@ -443,7 +471,7 @@
 				for (int i = 1; i < roles.length; i++) {
 					String r = roles[i];
 					if (r.equalsIgnoreCase(role)) {
-						needsDeleteRole.remove(username);
+						needsDeleteRole.add(username);
 						break;
 					}
 				}
@@ -457,12 +485,10 @@
 				StringBuilder sb = new StringBuilder();
 				sb.append(password);
 				sb.append(',');
-				List<String> revisedRoles = new ArrayList<String>();
 				// skip first value (password)
 				for (int i = 1; i < values.length; i++) {
 					String value = values[i];
 					if (!value.equalsIgnoreCase(role)) {
-						revisedRoles.add(value);
 						sb.append(value);
 						sb.append(',');
 					}
@@ -499,14 +525,15 @@
 		// If the write is successful, delete the current file and rename
 		// the temporary copy to the original filename.
 		if (realmFileCopy.exists() && realmFileCopy.length() > 0) {
-			if (propertiesFile.delete()) {
-				if (!realmFileCopy.renameTo(propertiesFile)) {
-					throw new IOException(MessageFormat.format("Failed to rename {0} to {1}!",
-							realmFileCopy.getAbsolutePath(), propertiesFile.getAbsolutePath()));
+			if (propertiesFile.exists()) {
+				if (!propertiesFile.delete()) {
+					throw new IOException(MessageFormat.format("Failed to delete {0}!",
+							propertiesFile.getAbsolutePath()));
 				}
-			} else {
-				throw new IOException(MessageFormat.format("Failed to delete (0)!",
-						propertiesFile.getAbsolutePath()));
+			}
+			if (!realmFileCopy.renameTo(propertiesFile)) {
+				throw new IOException(MessageFormat.format("Failed to rename {0} to {1}!",
+						realmFileCopy.getAbsolutePath(), propertiesFile.getAbsolutePath()));
 			}
 		} else {
 			throw new IOException(MessageFormat.format("Failed to save {0}!",
@@ -533,4 +560,9 @@
 		}
 		return allUsers;
 	}
+
+	@Override
+	public String toString() {
+		return getClass().getSimpleName() + "(" + propertiesFile.getAbsolutePath() + ")";
+	}
 }

--
Gitblit v1.9.1