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 |  187 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 166 insertions(+), 21 deletions(-)

diff --git a/src/com/gitblit/FileUserService.java b/src/com/gitblit/FileUserService.java
index 01a50be..a98e417 100644
--- a/src/com/gitblit/FileUserService.java
+++ b/src/com/gitblit/FileUserService.java
@@ -33,6 +33,20 @@
 import com.gitblit.models.UserModel;
 import com.gitblit.utils.StringUtils;
 
+/**
+ * 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);
@@ -43,11 +57,32 @@
 		super(realmFile.getAbsolutePath());
 	}
 
+	/**
+	 * Setup the user service.
+	 * 
+	 * @param settings
+	 * @since 0.6.1
+	 */
+	@Override
+	public void setup(IStoredSettings settings) {
+	}
+
+	/**
+	 * Does the user service support cookie authentication?
+	 * 
+	 * @return true or false
+	 */
 	@Override
 	public boolean supportsCookies() {
 		return true;
 	}
 
+	/**
+	 * Returns the cookie value for the specified user.
+	 * 
+	 * @param model
+	 * @return cookie value
+	 */
 	@Override
 	public char[] getCookie(UserModel model) {
 		Properties allUsers = super.read();
@@ -58,6 +93,12 @@
 		return cookie.toCharArray();
 	}
 
+	/**
+	 * Authenticate a user based on their cookie.
+	 * 
+	 * @param cookie
+	 * @return a user object or null
+	 */
 	@Override
 	public UserModel authenticate(char[] cookie) {
 		String hash = new String(cookie);
@@ -73,6 +114,13 @@
 		return model;
 	}
 
+	/**
+	 * Authenticate a user based on a username and password.
+	 * 
+	 * @param username
+	 * @param password
+	 * @return a user object or null
+	 */
 	@Override
 	public UserModel authenticate(String username, char[] password) {
 		Properties allUsers = read();
@@ -83,16 +131,31 @@
 		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;
 	}
 
+	/**
+	 * Retrieve the user object for the specified username.
+	 * 
+	 * @param username
+	 * @return a user object or null
+	 */
 	@Override
 	public UserModel getUserModel(String username) {
 		Properties allUsers = read();
@@ -110,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:
@@ -119,11 +184,27 @@
 		return model;
 	}
 
+	/**
+	 * Updates/writes a complete user object.
+	 * 
+	 * @param model
+	 * @return true if update is successful
+	 */
 	@Override
 	public boolean updateUserModel(UserModel model) {
 		return updateUserModel(model.username, model);
 	}
 
+	/**
+	 * Updates/writes and replaces a complete user object keyed by username.
+	 * This method allows for renaming a user.
+	 * 
+	 * @param username
+	 *            the old username
+	 * @param model
+	 *            the user object to use for username
+	 * @return true if update is successful
+	 */
 	@Override
 	public boolean updateUserModel(String username, UserModel model) {
 		try {
@@ -133,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();
@@ -156,11 +240,23 @@
 		return false;
 	}
 
+	/**
+	 * Deletes the user object from the user service.
+	 * 
+	 * @param model
+	 * @return true if successful
+	 */
 	@Override
 	public boolean deleteUserModel(UserModel model) {
 		return deleteUser(model.username);
 	}
 
+	/**
+	 * Delete the user object with the specified username
+	 * 
+	 * @param username
+	 * @return true if successful
+	 */
 	@Override
 	public boolean deleteUser(String username) {
 		try {
@@ -175,6 +271,11 @@
 		return false;
 	}
 
+	/**
+	 * Returns the list of all users available to the login service.
+	 * 
+	 * @return list of all usernames
+	 */
 	@Override
 	public List<String> getAllUsernames() {
 		Properties allUsers = read();
@@ -182,8 +283,16 @@
 		return list;
 	}
 
+	/**
+	 * Returns the list of all users who are allowed to bypass the access
+	 * restriction placed on the specified repository.
+	 * 
+	 * @param role
+	 *            the repository name
+	 * @return list of all usernames that can bypass the access restriction
+	 */
 	@Override
-	public List<String> getUsernamesForRepository(String role) {
+	public List<String> getUsernamesForRepositoryRole(String role) {
 		List<String> list = new ArrayList<String>();
 		try {
 			Properties allUsers = read();
@@ -205,8 +314,17 @@
 		return list;
 	}
 
+	/**
+	 * Sets the list of all uses who are allowed to bypass the access
+	 * restriction placed on the specified repository.
+	 * 
+	 * @param role
+	 *            the repository name
+	 * @param usernames
+	 * @return true if successful
+	 */
 	@Override
-	public boolean setUsernamesForRepository(String role, List<String> usernames) {
+	public boolean setUsernamesForRepositoryRole(String role, List<String> usernames) {
 		try {
 			Set<String> specifiedUsers = new HashSet<String>(usernames);
 			Set<String> needsAddRole = new HashSet<String>(specifiedUsers);
@@ -247,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(',');
 					}
@@ -272,6 +389,13 @@
 		return false;
 	}
 
+	/**
+	 * Renames a repository role.
+	 * 
+	 * @param oldRole
+	 * @param newRole
+	 * @return true if successful
+	 */
 	@Override
 	public boolean renameRepositoryRole(String oldRole, String newRole) {
 		try {
@@ -286,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;
 					}
 				}
@@ -300,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(',');
 					}
@@ -327,6 +451,12 @@
 		return false;
 	}
 
+	/**
+	 * Removes a repository role from all users.
+	 * 
+	 * @param role
+	 * @return true if successful
+	 */
 	@Override
 	public boolean deleteRepositoryRole(String role) {
 		try {
@@ -341,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;
 					}
 				}
@@ -355,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(',');
 					}
@@ -380,23 +508,32 @@
 		return false;
 	}
 
+	/**
+	 * Writes the properties file.
+	 * 
+	 * @param properties
+	 * @throws IOException
+	 */
 	private void write(Properties properties) throws IOException {
-		// Update realm file
+		// Write a temporary copy of the users file
 		File realmFileCopy = new File(propertiesFile.getAbsolutePath() + ".tmp");
 		FileWriter writer = new FileWriter(realmFileCopy);
 		properties
 				.store(writer,
 						"# Gitblit realm file format: username=password,\\#permission,repository1,repository2...");
 		writer.close();
+		// 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}!",
@@ -404,11 +541,14 @@
 		}
 	}
 
+	/**
+	 * Reads the properties file and rebuilds the in-memory cookie lookup table.
+	 */
 	@Override
 	protected synchronized Properties read() {
-		long lastRead = lastRead();
+		long lastRead = lastModified();
 		Properties allUsers = super.read();
-		if (lastRead != lastRead()) {
+		if (lastRead != lastModified()) {
 			// reload hash cache
 			cookies.clear();
 			for (String username : allUsers.stringPropertyNames()) {
@@ -420,4 +560,9 @@
 		}
 		return allUsers;
 	}
+
+	@Override
+	public String toString() {
+		return getClass().getSimpleName() + "(" + propertiesFile.getAbsolutePath() + ")";
+	}
 }

--
Gitblit v1.9.1