]> source.dussan.org Git - gitblit.git/commitdiff
Implemented UserServiceWrapper. Renamed notifyNNN to sendEmail to be more descriptive.
authorJames Moger <james.moger@gitblit.com>
Sat, 17 Dec 2011 19:14:11 +0000 (14:14 -0500)
committerJames Moger <james.moger@gitblit.com>
Sat, 17 Dec 2011 20:16:00 +0000 (15:16 -0500)
distrib/gitblit.properties
groovy/sendemail.groovy
src/com/gitblit/FederationPullExecutor.java
src/com/gitblit/GitBlit.java
src/com/gitblit/UserServiceWrapper.java [new file with mode: 0644]

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