From 9fd38cf138f0661990c4f542795beac618942c41 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Sat, 17 Dec 2011 15:16:00 -0500
Subject: [PATCH] Implemented UserServiceWrapper. Renamed notifyNNN to sendEmail to be more descriptive.

---
 src/com/gitblit/GitBlit.java                |   59 +----------
 src/com/gitblit/FederationPullExecutor.java |    2 
 distrib/gitblit.properties                  |    4 
 src/com/gitblit/UserServiceWrapper.java     |  221 ++++++++++++++++++++++++++++++++++++++++++++
 groovy/sendemail.groovy                     |    5 
 5 files changed, 236 insertions(+), 55 deletions(-)

diff --git a/distrib/gitblit.properties b/distrib/gitblit.properties
index 18566d8..ebaa29a 100644
--- a/distrib/gitblit.properties
+++ b/distrib/gitblit.properties
@@ -430,11 +430,11 @@
 # SINCE 0.6.0
 mail.adminAddresses = 
 
-# List of email addresses for sending commit email notifications.
+# List of email addresses for sending push email notifications.
 #
 # This key currently requires use of the sendemail.groovy hook script.
 # If you set sendemail.groovy in *groovy.postReceiveScripts* then email
-# notifications for all repositories (regardless of access restrictions)
+# notifications for all repositories (regardless of access restrictions!)
 # will be sent to these addresses.
 #
 # SPACE-DELIMITED
diff --git a/groovy/sendemail.groovy b/groovy/sendemail.groovy
index 29c5e5a..471ede8 100644
--- a/groovy/sendemail.groovy
+++ b/groovy/sendemail.groovy
@@ -87,6 +87,9 @@
 // add all mailing lists defined in gitblit.properties or web.xml
 toAddresses.addAll(gitblit.getStrings(Keys.mail.mailingLists))
 
+// add all mail recipients for the repository
+toAddresses.addAll(repository.mailRecipients)
+
 // special custom cases
 switch(repository.name) {
 	case "ex@mple.git":
@@ -149,4 +152,4 @@
 ${changes}"""
 
 // tell Gitblit to send the message (Gitblit filters duplicate addresses)
-gitblit.notifyUsers("${emailprefix} ${user.username} pushed ${commitCount} commits => ${repository.name}", msg, toAddresses)
\ No newline at end of file
+gitblit.sendEmail("${emailprefix} ${user.username} pushed ${commitCount} commits => ${repository.name}", msg, toAddresses)
\ No newline at end of file
diff --git a/src/com/gitblit/FederationPullExecutor.java b/src/com/gitblit/FederationPullExecutor.java
index c84761b..c3b7d8b 100644
--- a/src/com/gitblit/FederationPullExecutor.java
+++ b/src/com/gitblit/FederationPullExecutor.java
@@ -113,7 +113,7 @@
 						String message = "Federation pull of " + registration.name + " @ "
 								+ registration.url + " is now at " + is.name();
 						GitBlit.self()
-								.notifyAdministrators(
+								.sendEmailToAdministrators(
 										"Pull Status of " + registration.name + " is " + is.name(),
 										message);
 					}
diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java
index 11454f3..2719734 100644
--- a/src/com/gitblit/GitBlit.java
+++ b/src/com/gitblit/GitBlit.java
@@ -1421,7 +1421,7 @@
 	 * @param subject
 	 * @param message
 	 */
-	public void notifyAdministrators(String subject, String message) {
+	public void sendEmailToAdministrators(String subject, String message) {
 		try {
 			Message mail = mailExecutor.createMessageForAdministrators();
 			if (mail != null) {
@@ -1441,8 +1441,8 @@
 	 * @param message
 	 * @param toAddresses
 	 */
-	public void notifyUsers(String subject, String message, ArrayList<String> toAddresses) {
-		this.notifyUsers(subject, message, toAddresses.toArray(new String[0]));
+	public void sendEmail(String subject, String message, ArrayList<String> toAddresses) {
+		this.sendEmail(subject, message, toAddresses.toArray(new String[0]));
 	}
 
 	/**
@@ -1452,7 +1452,7 @@
 	 * @param message
 	 * @param toAddresses
 	 */
-	public void notifyUsers(String subject, String message, String... toAddresses) {
+	public void sendEmail(String subject, String message, String... toAddresses) {
 		try {
 			Message mail = mailExecutor.createMessage(toAddresses);
 			if (mail != null) {
@@ -1553,7 +1553,6 @@
 	 * 
 	 * @param settings
 	 */
-	@SuppressWarnings("deprecation")
 	public void configureContext(IStoredSettings settings, boolean startFederation) {
 		logger.info("Reading configuration from " + settings.toString());
 		this.settings = settings;
@@ -1570,53 +1569,11 @@
 				loginService = (IUserService) realmClass.newInstance();
 			}
 		} catch (Throwable t) {
-			// not a login service class or class could not be instantiated.
-			// try to use default file login service
-			File realmFile = getFileOrFolder(Keys.realm.userService, "users.conf");
-			if (realmFile.exists()) {
-				// load the existing realm file
-				if (realmFile.getName().toLowerCase().endsWith(".properties")) {
-					// load the v0.5.0 - v0.7.0 properties-based realm file
-					loginService = new FileUserService(realmFile);
-
-					// automatically create a users.conf realm file from the
-					// original users.properties file
-					File usersConfig = new File(realmFile.getParentFile(), "users.conf");
-					if (!usersConfig.exists()) {
-						logger.info(MessageFormat.format("Automatically creating {0} based on {1}",
-								usersConfig.getAbsolutePath(), realmFile.getAbsolutePath()));
-						ConfigUserService configService = new ConfigUserService(usersConfig);
-						for (String username : loginService.getAllUsernames()) {
-							UserModel userModel = loginService.getUserModel(username);
-							configService.updateUserModel(userModel);
-						}
-					}
-
-					// issue suggestion about switching to users.conf
-					logger.warn("Please consider using \"users.conf\" instead of the deprecated \"users.properties\" file");
-				} else if (realmFile.getName().toLowerCase().endsWith(".conf")) {
-					// load the config-based realm file
-					loginService = new ConfigUserService(realmFile);
+			loginService = new UserServiceWrapper() {
+				@Override
+				public void setupService(IStoredSettings settings) {
 				}
-			} else {
-				// Create a new realm file and add the default admin
-				// account. This is necessary for bootstrapping a dynamic
-				// environment like running on a cloud service.
-				// As of v0.8.0 the default realm file is ConfigUserService.
-				try {
-					realmFile = getFileOrFolder(Keys.realm.userService, "users.conf");
-					realmFile.createNewFile();
-					loginService = new ConfigUserService(realmFile);
-					UserModel admin = new UserModel("admin");
-					admin.password = "admin";
-					admin.canAdmin = true;
-					admin.excludeFromFederation = true;
-					loginService.updateUserModel(admin);
-				} catch (IOException x) {
-					logger.error(
-							MessageFormat.format("COULD NOT CREATE REALM FILE {0}!", realmFile), x);
-				}
-			}
+			};
 		}
 		setUserService(loginService);
 		mailExecutor = new MailExecutor(settings);
diff --git a/src/com/gitblit/UserServiceWrapper.java b/src/com/gitblit/UserServiceWrapper.java
new file mode 100644
index 0000000..88eab48
--- /dev/null
+++ b/src/com/gitblit/UserServiceWrapper.java
@@ -0,0 +1,221 @@
+/*
+ * Copyright 2011 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gitblit;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.gitblit.models.TeamModel;
+import com.gitblit.models.UserModel;
+
+/**
+ * This class wraps the default user service and is recommended as the starting
+ * point for custom user service implementations.
+ * 
+ * This does seem a little convoluted, but the idea is to allow IUserService to
+ * evolve and be replaced without hampering custom implementations.
+ * 
+ * The most common need for a custom IUserService is to override authentication
+ * and then delegate to one of Gitblit's user services. Subclassing this allows
+ * for authentication customization without having to keep-up-with IUSerService
+ * API changes.
+ * 
+ * @author James Moger
+ * 
+ */
+public abstract class UserServiceWrapper implements IUserService {
+
+	protected IUserService defaultService;
+
+	private final Logger logger = LoggerFactory.getLogger(UserServiceWrapper.class);
+
+	public UserServiceWrapper() {
+	}
+
+	@SuppressWarnings("deprecation")
+	@Override
+	public final void setup(IStoredSettings settings) {
+		File realmFile = GitBlit.getFileOrFolder(Keys.realm.userService, "users.conf");
+		if (realmFile.exists()) {
+			// load the existing realm file
+			if (realmFile.getName().toLowerCase().endsWith(".properties")) {
+				// load the v0.5.0 - v0.7.0 properties-based realm file
+				defaultService = new FileUserService(realmFile);
+
+				// automatically create a users.conf realm file from the
+				// original users.properties file
+				File usersConfig = new File(realmFile.getParentFile(), "users.conf");
+				if (!usersConfig.exists()) {
+					logger.info(MessageFormat.format("Automatically creating {0} based on {1}",
+							usersConfig.getAbsolutePath(), realmFile.getAbsolutePath()));
+					ConfigUserService configService = new ConfigUserService(usersConfig);
+					for (String username : defaultService.getAllUsernames()) {
+						UserModel userModel = defaultService.getUserModel(username);
+						configService.updateUserModel(userModel);
+					}
+				}
+
+				// issue suggestion about switching to users.conf
+				logger.warn("Please consider using \"users.conf\" instead of the deprecated \"users.properties\" file");
+			} else if (realmFile.getName().toLowerCase().endsWith(".conf")) {
+				// load the config-based realm file
+				defaultService = new ConfigUserService(realmFile);
+			}
+		} else {
+			// Create a new realm file and add the default admin
+			// account. This is necessary for bootstrapping a dynamic
+			// environment like running on a cloud service.
+			// As of v0.8.0 the default realm file is ConfigUserService.
+			try {
+				realmFile = GitBlit.getFileOrFolder(Keys.realm.userService, "users.conf");
+				realmFile.createNewFile();
+				defaultService = new ConfigUserService(realmFile);
+				UserModel admin = new UserModel("admin");
+				admin.password = "admin";
+				admin.canAdmin = true;
+				admin.excludeFromFederation = true;
+				defaultService.updateUserModel(admin);
+			} catch (IOException x) {
+				logger.error(MessageFormat.format("COULD NOT CREATE REALM FILE {0}!", realmFile), x);
+			}
+		}
+
+		// call subclass setup
+		setupService(settings);
+	}
+
+	/**
+	 * Subclasses must implement this method.
+	 * 
+	 * @param settings
+	 */
+	public abstract void setupService(IStoredSettings settings);
+
+	@Override
+	public boolean supportsCookies() {
+		return defaultService.supportsCookies();
+	}
+
+	@Override
+	public char[] getCookie(UserModel model) {
+		return defaultService.getCookie(model);
+	}
+
+	@Override
+	public UserModel authenticate(char[] cookie) {
+		return defaultService.authenticate(cookie);
+	}
+
+	@Override
+	public UserModel authenticate(String username, char[] password) {
+		return defaultService.authenticate(username, password);
+	}
+
+	@Override
+	public UserModel getUserModel(String username) {
+		return defaultService.getUserModel(username);
+	}
+
+	@Override
+	public boolean updateUserModel(UserModel model) {
+		return defaultService.updateUserModel(model);
+	}
+
+	@Override
+	public boolean updateUserModel(String username, UserModel model) {
+		return defaultService.updateUserModel(username, model);
+	}
+
+	@Override
+	public boolean deleteUserModel(UserModel model) {
+		return defaultService.deleteUserModel(model);
+	}
+
+	@Override
+	public boolean deleteUser(String username) {
+		return defaultService.deleteUser(username);
+	}
+
+	@Override
+	public List<String> getAllUsernames() {
+		return defaultService.getAllUsernames();
+	}
+
+	@Override
+	public List<String> getAllTeamNames() {
+		return defaultService.getAllTeamNames();
+	}
+
+	@Override
+	public List<String> getTeamnamesForRepositoryRole(String role) {
+		return defaultService.getTeamnamesForRepositoryRole(role);
+	}
+
+	@Override
+	public boolean setTeamnamesForRepositoryRole(String role, List<String> teamnames) {
+		return defaultService.setTeamnamesForRepositoryRole(role, teamnames);
+	}
+
+	@Override
+	public TeamModel getTeamModel(String teamname) {
+		return defaultService.getTeamModel(teamname);
+	}
+
+	@Override
+	public boolean updateTeamModel(TeamModel model) {
+		return defaultService.updateTeamModel(model);
+	}
+
+	@Override
+	public boolean updateTeamModel(String teamname, TeamModel model) {
+		return defaultService.updateTeamModel(teamname, model);
+	}
+
+	@Override
+	public boolean deleteTeamModel(TeamModel model) {
+		return defaultService.deleteTeamModel(model);
+	}
+
+	@Override
+	public boolean deleteTeam(String teamname) {
+		return defaultService.deleteTeam(teamname);
+	}
+
+	@Override
+	public List<String> getUsernamesForRepositoryRole(String role) {
+		return defaultService.getUsernamesForRepositoryRole(role);
+	}
+
+	@Override
+	public boolean setUsernamesForRepositoryRole(String role, List<String> usernames) {
+		return defaultService.setUsernamesForRepositoryRole(role, usernames);
+	}
+
+	@Override
+	public boolean renameRepositoryRole(String oldRole, String newRole) {
+		return defaultService.renameRepositoryRole(oldRole, newRole);
+	}
+
+	@Override
+	public boolean deleteRepositoryRole(String role) {
+		return defaultService.deleteRepositoryRole(role);
+	}
+}

--
Gitblit v1.9.1