]> source.dussan.org Git - gitblit.git/commitdiff
Extract UserManager from GitBlit singleton
authorJames Moger <james.moger@gitblit.com>
Thu, 21 Nov 2013 01:52:11 +0000 (20:52 -0500)
committerJames Moger <james.moger@gitblit.com>
Fri, 29 Nov 2013 16:05:51 +0000 (11:05 -0500)
Change-Id: I4885255ed63aa6c4e000c3e5501675440dca3958

16 files changed:
src/main/java/com/gitblit/ConfigUserService.java
src/main/java/com/gitblit/DaggerModule.java
src/main/java/com/gitblit/GitBlit.java
src/main/java/com/gitblit/Gitblit.java
src/main/java/com/gitblit/GitblitUserService.java
src/main/java/com/gitblit/HtpasswdUserService.java
src/main/java/com/gitblit/IUserService.java
src/main/java/com/gitblit/LdapUserService.java
src/main/java/com/gitblit/PAMUserService.java
src/main/java/com/gitblit/RedmineUserService.java
src/main/java/com/gitblit/SalesforceUserService.java
src/main/java/com/gitblit/WindowsUserService.java
src/main/java/com/gitblit/manager/IUserManager.java
src/main/java/com/gitblit/manager/UserManager.java [new file with mode: 0644]
src/main/java/com/gitblit/utils/StringUtils.java
src/test/java/de/akquinet/devops/GitBlit4UITests.java

index 79cbdba7d9b36288b1088fedd016fed0f129e3ed..39374e88d90463adc8f41dbeb7f1ac645cf964a0 100644 (file)
@@ -35,6 +35,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;\r
 \r
 import com.gitblit.Constants.AccessPermission;\r
+import com.gitblit.Constants.AccountType;\r
 import com.gitblit.manager.IRuntimeManager;\r
 import com.gitblit.models.TeamModel;\r
 import com.gitblit.models.UserModel;\r
@@ -486,7 +487,7 @@ public class ConfigUserService implements IUserService {
         * @return list of all usernames that can bypass the access restriction\r
         */\r
        @Override\r
-       public synchronized List<String> getTeamnamesForRepositoryRole(String role) {\r
+       public synchronized List<String> getTeamNamesForRepositoryRole(String role) {\r
                List<String> list = new ArrayList<String>();\r
                try {\r
                        read();\r
@@ -1111,4 +1112,9 @@ public class ConfigUserService implements IUserService {
        public String toString() {\r
                return getClass().getSimpleName() + "(" + realmFile.getAbsolutePath() + ")";\r
        }\r
+\r
+       @Override\r
+       public AccountType getAccountType() {\r
+               return AccountType.LOCAL;\r
+       }\r
 }\r
index 2f060c0ba645a17c2ecdc960ecb504686e8b77a7..7f12fca3f29ea62507e8958405c23e4a4fe60961 100644 (file)
@@ -30,6 +30,7 @@ import com.gitblit.manager.ISessionManager;
 import com.gitblit.manager.IUserManager;
 import com.gitblit.manager.NotificationManager;
 import com.gitblit.manager.RuntimeManager;
+import com.gitblit.manager.UserManager;
 import com.gitblit.wicket.GitBlitWebApp;
 import com.gitblit.wicket.GitblitWicketFilter;
 
@@ -100,8 +101,8 @@ public class DaggerModule {
                return new NotificationManager(settings);
        }
 
-       @Provides @Singleton IUserManager provideUserManager() {
-               return gitblit;
+       @Provides @Singleton IUserManager provideUserManager(IRuntimeManager runtimeManager) {
+               return new UserManager(runtimeManager);
        }
 
        @Provides @Singleton ISessionManager provideSessionManager() {
index 4e77974844f46d35f972c9194f33f1e5860318dc..f7e7627355f6b81a3c81bbfbf0de9f0983a4c4d4 100644 (file)
@@ -80,7 +80,6 @@ import org.slf4j.Logger;
 
 import com.gitblit.Constants.AccessPermission;
 import com.gitblit.Constants.AccessRestrictionType;
-import com.gitblit.Constants.AccountType;
 import com.gitblit.Constants.AuthenticationType;
 import com.gitblit.Constants.AuthorizationControl;
 import com.gitblit.Constants.CommitMessageRenderer;
@@ -162,8 +161,7 @@ import dagger.ObjectGraph;
  */
 @WebListener
 public class GitBlit extends DaggerContextListener
-                                        implements IUserManager,
-                                                               ISessionManager,
+                                        implements ISessionManager,
                                                                IRepositoryManager,
                                                                IProjectManager,
                                                                IFederationManager,
@@ -202,8 +200,6 @@ public class GitBlit extends DaggerContextListener
 
        private File repositoriesFolder;
 
-       private IUserService userService;
-
        private IStoredSettings settings;
 
        private LuceneExecutor luceneExecutor;
@@ -223,13 +219,6 @@ public class GitBlit extends DaggerContextListener
                this.goBaseFolder = null;
        }
 
-       protected GitBlit(final IUserService userService) {
-               this.goSettings = null;
-               this.goBaseFolder = null;
-               this.userService = userService;
-               gitblit = this;
-       }
-
        public GitBlit(IStoredSettings settings, File baseFolder) {
                this.goSettings = settings;
                this.goBaseFolder = baseFolder;
@@ -335,7 +324,7 @@ public class GitBlit extends DaggerContextListener
                if (user == null) {
                        user = UserModel.ANONYMOUS;
                }
-               String username = encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username);
+               String username = StringUtils.encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username);
 
                List<RepositoryUrl> list = new ArrayList<RepositoryUrl>();
                // http/https url
@@ -485,75 +474,6 @@ public class GitBlit extends DaggerContextListener
                return null;
        }
 
-       /**
-        * Set the user service. The user service authenticates all users and is
-        * responsible for managing user permissions.
-        *
-        * @param userService
-        */
-       public void setUserService(IUserService userService) {
-               logger.info("Setting up user service " + userService.toString());
-               this.userService = userService;
-               this.userService.setup(getManager(IRuntimeManager.class));
-       }
-
-       @Override
-       public boolean supportsAddUser() {
-               return supportsCredentialChanges(new UserModel(""));
-       }
-
-       /**
-        * Returns true if the user's credentials can be changed.
-        *
-        * @param user
-        * @return true if the user service supports credential changes
-        */
-       @Override
-       public boolean supportsCredentialChanges(UserModel user) {
-               if (user == null) {
-                       return false;
-               } else if (AccountType.LOCAL.equals(user.accountType)) {
-                       // local account, we can change credentials
-                       return true;
-               } else {
-                       // external account, ask user service
-                       return userService.supportsCredentialChanges();
-               }
-       }
-
-       /**
-        * Returns true if the user's display name can be changed.
-        *
-        * @param user
-        * @return true if the user service supports display name changes
-        */
-       @Override
-       public boolean supportsDisplayNameChanges(UserModel user) {
-               return (user != null && user.isLocalAccount()) || userService.supportsDisplayNameChanges();
-       }
-
-       /**
-        * Returns true if the user's email address can be changed.
-        *
-        * @param user
-        * @return true if the user service supports email address changes
-        */
-       @Override
-       public boolean supportsEmailAddressChanges(UserModel user) {
-               return (user != null && user.isLocalAccount()) || userService.supportsEmailAddressChanges();
-       }
-
-       /**
-        * Returns true if the user's team memberships can be changed.
-        *
-        * @param user
-        * @return true if the user service supports team membership changes
-        */
-       @Override
-       public boolean supportsTeamMembershipChanges(UserModel user) {
-               return (user != null && user.isLocalAccount()) || userService.supportsTeamMembershipChanges();
-       }
-
        /**
         * Returns true if the username represents an internal account
         *
@@ -580,7 +500,7 @@ public class GitBlit extends DaggerContextListener
                        // can not authenticate empty username
                        return null;
                }
-               String usernameDecoded = decodeUsername(username);
+               String usernameDecoded = StringUtils.decodeUsername(username);
                String pw = new String(password);
                if (StringUtils.isEmpty(pw)) {
                        // can not authenticate empty password
@@ -598,10 +518,7 @@ public class GitBlit extends DaggerContextListener
                }
 
                // delegate authentication to the user service
-               if (userService == null) {
-                       return null;
-               }
-               return userService.authenticate(usernameDecoded, password);
+               return getManager(IUserManager.class).authenticate(usernameDecoded, password);
        }
 
        /**
@@ -611,15 +528,12 @@ public class GitBlit extends DaggerContextListener
         * @return a user object or null
         */
        protected UserModel authenticate(Cookie[] cookies) {
-               if (userService == null) {
-                       return null;
-               }
-               if (userService.supportsCookies()) {
+               if (getManager(IUserManager.class).supportsCookies()) {
                        if (cookies != null && cookies.length > 0) {
                                for (Cookie cookie : cookies) {
                                        if (cookie.getName().equals(Constants.NAME)) {
                                                String value = cookie.getValue();
-                                               return userService.authenticate(value.toCharArray());
+                                               return getManager(IUserManager.class).authenticate(value.toCharArray());
                                        }
                                }
                        }
@@ -658,7 +572,7 @@ public class GitBlit extends DaggerContextListener
                UserModel model = HttpUtils.getUserModelFromCertificate(httpRequest, checkValidity, oids);
                if (model != null) {
                        // grab real user model and preserve certificate serial number
-                       UserModel user = getUserModel(model.username);
+                       UserModel user = getManager(IUserManager.class).getUserModel(model.username);
                        X509Metadata metadata = HttpUtils.getCertificateMetadata(httpRequest);
                        if (user != null) {
                                flagWicketSession(AuthenticationType.CERTIFICATE);
@@ -682,7 +596,7 @@ public class GitBlit extends DaggerContextListener
                        String username = principal.getName();
                        if (!StringUtils.isEmpty(username)) {
                                boolean internalAccount = isInternalAccount(username);
-                               UserModel user = getUserModel(username);
+                               UserModel user = getManager(IUserManager.class).getUserModel(username);
                                if (user != null) {
                                        // existing user
                                        flagWicketSession(AuthenticationType.CONTAINER);
@@ -695,7 +609,7 @@ public class GitBlit extends DaggerContextListener
                                        user = new UserModel(username.toLowerCase());
                                        user.displayName = username;
                                        user.password = Constants.EXTERNAL_ACCOUNT;
-                                       userService.updateUserModel(user);
+                                       getManager(IUserManager.class).updateUserModel(user);
                                        flagWicketSession(AuthenticationType.CONTAINER);
                                        logger.debug(MessageFormat.format("{0} authenticated and created by servlet container principal from {1}",
                                                        user.username, httpRequest.getRemoteAddr()));
@@ -708,7 +622,7 @@ public class GitBlit extends DaggerContextListener
                }
 
                // try to authenticate by cookie
-               if (supportsCookies()) {
+               if (getManager(IUserManager.class).supportsCookies()) {
                        UserModel user = authenticate(httpRequest.getCookies());
                        if (user != null) {
                                flagWicketSession(AuthenticationType.COOKIE);
@@ -726,7 +640,7 @@ public class GitBlit extends DaggerContextListener
                        String credentials = new String(Base64.decode(base64Credentials),
                                        Charset.forName("UTF-8"));
                        // credentials = username:password
-                       final String[] values = credentials.split(":",2);
+                       final String[] values = credentials.split(":", 2);
 
                        if (values.length == 2) {
                                String username = values[0];
@@ -774,20 +688,17 @@ public class GitBlit extends DaggerContextListener
         */
        @Override
        public void setCookie(HttpServletResponse response, UserModel user) {
-               if (userService == null) {
-                       return;
-               }
                GitBlitWebSession session = GitBlitWebSession.get();
                boolean standardLogin = session.authenticationType.isStandard();
 
-               if (userService.supportsCookies() && standardLogin) {
+               if (getManager(IUserManager.class).supportsCookies() && standardLogin) {
                        Cookie userCookie;
                        if (user == null) {
                                // clear cookie for logout
                                userCookie = new Cookie(Constants.NAME, "");
                        } else {
                                // set cookie for login
-                               String cookie = userService.getCookie(user);
+                               String cookie = getManager(IUserManager.class).getCookie(user);
                                if (StringUtils.isEmpty(cookie)) {
                                        // create empty cookie
                                        userCookie = new Cookie(Constants.NAME, "");
@@ -802,79 +713,6 @@ public class GitBlit extends DaggerContextListener
                }
        }
 
-       /**
-        * Logout a user.
-        *
-        * @param user
-        */
-       @Override
-       public void logout(UserModel user) {
-               if (userService == null) {
-                       return;
-               }
-               userService.logout(user);
-       }
-
-       /**
-        * 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()
-        * @return list of all usernames
-        */
-       @Override
-       public List<String> getAllUsernames() {
-               List<String> names = new ArrayList<String>(userService.getAllUsernames());
-               return names;
-       }
-
-       /**
-        * Returns the list of all users available to the login service.
-        *
-        * @see IUserService.getAllUsernames()
-        * @return list of all usernames
-        */
-       @Override
-       public List<UserModel> getAllUsers() {
-               List<UserModel> users = userService.getAllUsers();
-               return users;
-       }
-
-       /**
-        * Delete the user object with the specified username
-        *
-        * @see IUserService.deleteUser(String)
-        * @param username
-        * @return true if successful
-        */
-       @Override
-       public boolean deleteUser(String username) {
-               if (StringUtils.isEmpty(username)) {
-                       return false;
-               }
-               String usernameDecoded = decodeUsername(username);
-               return userService.deleteUser(usernameDecoded);
-       }
-
        @Override
        public UserModel getFederationUser() {
                // the federation user is an administrator
@@ -883,23 +721,6 @@ public class GitBlit extends DaggerContextListener
                return federationUser;
        }
 
-       /**
-        * Retrieve the user object for the specified username.
-        *
-        * @see IUserService.getUserModel(String)
-        * @param username
-        * @return a user object or null
-        */
-       @Override
-       public UserModel getUserModel(String username) {
-               if (StringUtils.isEmpty(username)) {
-                       return null;
-               }
-               String usernameDecoded = decodeUsername(username);
-               UserModel user = userService.getUserModel(usernameDecoded);
-               return user;
-       }
-
        /**
         * Returns the effective list of permissions for this user, taking into account
         * team memberships, ownerships.
@@ -965,7 +786,7 @@ public class GitBlit extends DaggerContextListener
                        return list;
                }
                // NAMED users and teams
-               for (UserModel user : userService.getAllUsers()) {
+               for (UserModel user : getManager(IUserManager.class).getAllUsers()) {
                        RegistrantAccessPermission ap = user.getRepositoryPermission(repository);
                        if (ap.permission.exceeds(AccessPermission.NONE)) {
                                list.add(ap);
@@ -987,12 +808,12 @@ public class GitBlit extends DaggerContextListener
                for (RegistrantAccessPermission up : permissions) {
                        if (up.mutable) {
                                // only set editable defined permissions
-                               UserModel user = userService.getUserModel(up.registrant);
+                               UserModel user = getManager(IUserManager.class).getUserModel(up.registrant);
                                user.setRepositoryPermission(repository.name, up.permission);
                                users.add(user);
                        }
                }
-               return userService.updateUserModels(users);
+               return getManager(IUserManager.class).updateUserModels(users);
        }
 
        /**
@@ -1005,7 +826,7 @@ public class GitBlit extends DaggerContextListener
         */
        @Override
        public List<String> getRepositoryUsers(RepositoryModel repository) {
-               return userService.getUsernamesForRepositoryRole(repository.name);
+               return getManager(IUserManager.class).getUsernamesForRepositoryRole(repository.name);
        }
 
        /**
@@ -1038,7 +859,7 @@ public class GitBlit extends DaggerContextListener
        public void updateUserModel(String username, UserModel user, boolean isCreate)
                        throws GitBlitException {
                if (!username.equalsIgnoreCase(user.username)) {
-                       if (userService.getUserModel(user.username) != null) {
+                       if (getManager(IUserManager.class).getUserModel(user.username) != null) {
                                throw new GitBlitException(MessageFormat.format(
                                                "Failed to rename ''{0}'' because ''{1}'' already exists.", username,
                                                user.username));
@@ -1060,45 +881,11 @@ public class GitBlit extends DaggerContextListener
                                }
                        }
                }
-               if (!userService.updateUserModel(username, user)) {
+               if (!getManager(IUserManager.class).updateUserModel(username, user)) {
                        throw new GitBlitException(isCreate ? "Failed to add user!" : "Failed to update user!");
                }
        }
 
-       /**
-        * Returns the list of available teams that a user or repository may be
-        * assigned to.
-        *
-        * @return the list of teams
-        */
-       public List<String> getAllTeamnames() {
-               List<String> teams = new ArrayList<String>(userService.getAllTeamNames());
-               return teams;
-       }
-
-       /**
-        * Returns the list of available teams that a user or repository may be
-        * assigned to.
-        *
-        * @return the list of teams
-        */
-       @Override
-       public List<TeamModel> getAllTeams() {
-               List<TeamModel> teams = userService.getAllTeams();
-               return teams;
-       }
-
-       /**
-        * Returns the TeamModel object for the specified name.
-        *
-        * @param teamname
-        * @return a TeamModel object or null
-        */
-       @Override
-       public TeamModel getTeamModel(String teamname) {
-               return userService.getTeamModel(teamname);
-       }
-
        /**
         * Returns the list of teams and their access permissions for the specified
         * repository including the source of the permission such as the admin flag
@@ -1110,7 +897,7 @@ public class GitBlit extends DaggerContextListener
        @Override
        public List<RegistrantAccessPermission> getTeamAccessPermissions(RepositoryModel repository) {
                List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();
-               for (TeamModel team : userService.getAllTeams()) {
+               for (TeamModel team : getManager(IUserManager.class).getAllTeams()) {
                        RegistrantAccessPermission ap = team.getRepositoryPermission(repository);
                        if (ap.permission.exceeds(AccessPermission.NONE)) {
                                list.add(ap);
@@ -1133,12 +920,12 @@ public class GitBlit extends DaggerContextListener
                for (RegistrantAccessPermission tp : permissions) {
                        if (tp.mutable) {
                                // only set explicitly defined access permissions
-                               TeamModel team = userService.getTeamModel(tp.registrant);
+                               TeamModel team = getManager(IUserManager.class).getTeamModel(tp.registrant);
                                team.setRepositoryPermission(repository.name, tp.permission);
                                teams.add(team);
                        }
                }
-               return userService.updateTeamModels(teams);
+               return getManager(IUserManager.class).updateTeamModels(teams);
        }
 
        /**
@@ -1151,7 +938,7 @@ public class GitBlit extends DaggerContextListener
         */
        @Override
        public List<String> getRepositoryTeams(RepositoryModel repository) {
-               return userService.getTeamnamesForRepositoryRole(repository.name);
+               return getManager(IUserManager.class).getTeamNamesForRepositoryRole(repository.name);
        }
 
        /**
@@ -1181,29 +968,17 @@ public class GitBlit extends DaggerContextListener
        public void updateTeamModel(String teamname, TeamModel team, boolean isCreate)
                        throws GitBlitException {
                if (!teamname.equalsIgnoreCase(team.name)) {
-                       if (userService.getTeamModel(team.name) != null) {
+                       if (getManager(IUserManager.class).getTeamModel(team.name) != null) {
                                throw new GitBlitException(MessageFormat.format(
                                                "Failed to rename ''{0}'' because ''{1}'' already exists.", teamname,
                                                team.name));
                        }
                }
-               if (!userService.updateTeamModel(teamname, team)) {
+               if (!getManager(IUserManager.class).updateTeamModel(teamname, team)) {
                        throw new GitBlitException(isCreate ? "Failed to add team!" : "Failed to update team!");
                }
        }
 
-       /**
-        * Delete the team object with the specified teamname
-        *
-        * @see IUserService.deleteTeam(String)
-        * @param teamname
-        * @return true if successful
-        */
-       @Override
-       public boolean deleteTeam(String teamname) {
-               return userService.deleteTeam(teamname);
-       }
-
        /**
         * Adds the repository to the list of cached repositories if Gitblit is
         * configured to cache the repository list.
@@ -1519,7 +1294,7 @@ public class GitBlit extends DaggerContextListener
        @Override
        public long getStarCount(RepositoryModel repository) {
                long count = 0;
-               for (UserModel user : getAllUsers()) {
+               for (UserModel user : getManager(IUserManager.class).getAllUsers()) {
                        if (user.getPreferences().isStarredRepository(repository.name)) {
                                count++;
                        }
@@ -1680,7 +1455,7 @@ public class GitBlit extends DaggerContextListener
                if (project == null) {
                        project = new ProjectModel(name);
                        if (ModelUtils.isPersonalRepository(name)) {
-                               UserModel user = getUserModel(ModelUtils.getUserNameFromRepoPath(name));
+                               UserModel user = getManager(IUserManager.class).getUserModel(ModelUtils.getUserNameFromRepoPath(name));
                                if (user != null) {
                                        project.title = user.getDisplayName();
                                        project.description = "personal repositories";
@@ -2297,7 +2072,7 @@ public class GitBlit extends DaggerContextListener
                                                        repository.name));
                                }
                                // rename the roles
-                               if (!userService.renameRepositoryRole(repositoryName, repository.name)) {
+                               if (!getManager(IUserManager.class).renameRepositoryRole(repositoryName, repository.name)) {
                                        throw new GitBlitException(MessageFormat.format(
                                                        "Failed to rename repository permissions ''{0}'' to ''{1}''.",
                                                        repositoryName, repository.name));
@@ -2514,7 +2289,7 @@ public class GitBlit extends DaggerContextListener
                        File folder = new File(repositoriesFolder, repositoryName);
                        if (folder.exists() && folder.isDirectory()) {
                                FileUtils.delete(folder, FileUtils.RECURSIVE | FileUtils.RETRY);
-                               if (userService.deleteRepositoryRole(repositoryName)) {
+                               if (getManager(IUserManager.class).deleteRepositoryRole(repositoryName)) {
                                        logger.info(MessageFormat.format("Repository \"{0}\" deleted", repositoryName));
                                        return true;
                                }
@@ -3046,8 +2821,8 @@ public class GitBlit extends DaggerContextListener
 
                // Team Scripts
                if (repository != null) {
-                       for (String teamname : userService.getTeamnamesForRepositoryRole(repository.name)) {
-                               TeamModel team = userService.getTeamModel(teamname);
+                       for (String teamname : getManager(IUserManager.class).getTeamNamesForRepositoryRole(repository.name)) {
+                               TeamModel team = getManager(IUserManager.class).getTeamModel(teamname);
                                if (!ArrayUtils.isEmpty(team.preReceiveScripts)) {
                                        scripts.addAll(team.preReceiveScripts);
                                }
@@ -3100,8 +2875,8 @@ public class GitBlit extends DaggerContextListener
                }
                // Team Scripts
                if (repository != null) {
-                       for (String teamname : userService.getTeamnamesForRepositoryRole(repository.name)) {
-                               TeamModel team = userService.getTeamModel(teamname);
+                       for (String teamname : getManager(IUserManager.class).getTeamNamesForRepositoryRole(repository.name)) {
+                               TeamModel team = getManager(IUserManager.class).getTeamModel(teamname);
                                if (!ArrayUtils.isEmpty(team.postReceiveScripts)) {
                                        scripts.addAll(team.postReceiveScripts);
                                }
@@ -3156,10 +2931,14 @@ public class GitBlit extends DaggerContextListener
         * @return Map<String, SettingModel>
         */
        private ServerSettings loadSettingModels(ServerSettings settingsModel) {
-               settingsModel.supportsCredentialChanges = userService.supportsCredentialChanges();
-               settingsModel.supportsDisplayNameChanges = userService.supportsDisplayNameChanges();
-               settingsModel.supportsEmailAddressChanges = userService.supportsEmailAddressChanges();
-               settingsModel.supportsTeamMembershipChanges = userService.supportsTeamMembershipChanges();
+               // this entire "supports" concept will go away with user service refactoring
+               UserModel externalUser = new UserModel(Constants.EXTERNAL_ACCOUNT);
+               externalUser.password = Constants.EXTERNAL_ACCOUNT;
+               IUserManager userManager = getManager(IUserManager.class);
+               settingsModel.supportsCredentialChanges = userManager.supportsCredentialChanges(externalUser);
+               settingsModel.supportsDisplayNameChanges = userManager.supportsDisplayNameChanges(externalUser);
+               settingsModel.supportsEmailAddressChanges = userManager.supportsEmailAddressChanges(externalUser);
+               settingsModel.supportsTeamMembershipChanges = userManager.supportsTeamMembershipChanges(externalUser);
                try {
                        // Read bundled Gitblit properties to extract setting descriptions.
                        // This copy is pristine and only used for populating the setting
@@ -3321,7 +3100,7 @@ public class GitBlit extends DaggerContextListener
                                Gitblit gitblit = new Gitblit(
                                                getManager(IRuntimeManager.class),
                                                getManager(INotificationManager.class),
-                                               this,
+                                               getManager(IUserManager.class),
                                                this,
                                                this,
                                                this,
@@ -3430,6 +3209,7 @@ public class GitBlit extends DaggerContextListener
                runtime.getStatus().servletContainer = context.getServerInfo();
 
                startManager(injector, INotificationManager.class);
+               startManager(injector, IUserManager.class);
 
                repositoriesFolder = getRepositoriesFolder();
 
@@ -3454,19 +3234,6 @@ public class GitBlit extends DaggerContextListener
                        getRepositoryList();
                }
 
-               if (this.userService == null) {
-                       String realm = runtimeSettings.getString(Keys.realm.userService, "${baseFolder}/users.properties");
-                       IUserService loginService = null;
-                       try {
-                               // check to see if this "file" is a login service class
-                               Class<?> realmClass = Class.forName(realm);
-                               loginService = (IUserService) realmClass.newInstance();
-                       } catch (Throwable t) {
-                               loginService = new GitblitUserService();
-                       }
-                       setUserService(loginService);
-               }
-
                loadSettingModels(runtime.getSettingsModel());
 
                // load and cache the project metadata
@@ -3747,7 +3514,7 @@ public class GitBlit extends DaggerContextListener
                // add the owner of the source repository to the clone's access list
                if (!ArrayUtils.isEmpty(repository.owners)) {
                        for (String owner : repository.owners) {
-                               UserModel originOwner = getUserModel(owner);
+                               UserModel originOwner = getManager(IUserManager.class).getUserModel(owner);
                                if (originOwner != null) {
                                        originOwner.setRepositoryPermission(cloneName, AccessPermission.CLONE);
                                        updateUserModel(originOwner.username, originOwner, false);
@@ -3760,7 +3527,7 @@ public class GitBlit extends DaggerContextListener
                List<UserModel> cloneUsers = new ArrayList<UserModel>();
                for (String name : users) {
                        if (!name.equalsIgnoreCase(user.username)) {
-                               UserModel cloneUser = getUserModel(name);
+                               UserModel cloneUser = getManager(IUserManager.class).getUserModel(name);
                                if (cloneUser.canClone(repository)) {
                                        // origin user can clone origin, grant clone access to fork
                                        cloneUser.setRepositoryPermission(cloneName, AccessPermission.CLONE);
@@ -3768,116 +3535,30 @@ public class GitBlit extends DaggerContextListener
                                cloneUsers.add(cloneUser);
                        }
                }
-               userService.updateUserModels(cloneUsers);
+               getManager(IUserManager.class).updateUserModels(cloneUsers);
 
                // grant origin's team list clone permission to fork
                List<String> teams = getRepositoryTeams(repository);
                List<TeamModel> cloneTeams = new ArrayList<TeamModel>();
                for (String name : teams) {
-                       TeamModel cloneTeam = getTeamModel(name);
+                       TeamModel cloneTeam = getManager(IUserManager.class).getTeamModel(name);
                        if (cloneTeam.canClone(repository)) {
                                // origin team can clone origin, grant clone access to fork
                                cloneTeam.setRepositoryPermission(cloneName, AccessPermission.CLONE);
                        }
                        cloneTeams.add(cloneTeam);
                }
-               userService.updateTeamModels(cloneTeams);
+               getManager(IUserManager.class).updateTeamModels(cloneTeams);
 
                // add this clone to the cached model
                addToCachedRepositoryList(cloneModel);
                return cloneModel;
        }
 
-       /**
-        * Allow to understand if GitBlit supports and is configured to allow
-        * cookie-based authentication.
-        *
-        * @return status of Cookie authentication enablement.
-        */
-       @Override
-       public boolean supportsCookies() {
-               return settings.getBoolean(Keys.web.allowCookieAuthentication, true) && userService.supportsCookies();
-       }
-
-       @Override
-       public String getCookie(UserModel model) {
-               return userService.getCookie(model);
-       }
-
-       @Override
-       public UserModel authenticate(char[] cookie) {
-               return userService.authenticate(cookie);
-       }
-
-       @Override
-       public boolean updateUserModel(UserModel model) {
-               return userService.updateUserModel(model);
-       }
-
-       @Override
-       public boolean updateUserModels(Collection<UserModel> models) {
-               return userService.updateUserModels(models);
-       }
-
-       @Override
-       public boolean updateUserModel(String username, UserModel model) {
-               return userService.updateUserModel(username, model);
-       }
-
-       @Override
-       public boolean deleteUserModel(UserModel model) {
-               return userService.deleteUserModel(model);
-       }
-
-       @Override
-       public List<String> getAllTeamNames() {
-               return userService.getAllTeamNames();
-       }
-
-       @Override
-       public List<String> getTeamnamesForRepositoryRole(String role) {
-               return userService.getTeamnamesForRepositoryRole(role);
-       }
-
-       @Override
-       public boolean updateTeamModel(TeamModel model) {
-               return userService.updateTeamModel(model);
-       }
-
-       @Override
-       public boolean updateTeamModels(Collection<TeamModel> models) {
-               return userService.updateTeamModels(models);
-       }
-
-       @Override
-       public boolean updateTeamModel(String teamname, TeamModel model) {
-               return userService.updateTeamModel(teamname, model);
-       }
-
-       @Override
-       public boolean deleteTeamModel(TeamModel model) {
-               return userService.deleteTeamModel(model);
-       }
-
-       @Override
-       public List<String> getUsernamesForRepositoryRole(String role) {
-               return userService.getUsernamesForRepositoryRole(role);
-       }
-
-       @Override
-       public boolean renameRepositoryRole(String oldRole, String newRole) {
-               return userService.renameRepositoryRole(oldRole, newRole);
-       }
-
-       @Override
-       public boolean deleteRepositoryRole(String role) {
-               return userService.deleteRepositoryRole(role);
-       }
-
        @Override
        public void logout(HttpServletResponse response, UserModel user) {
                setCookie(response,  null);
-               userService.logout(user);
+               getManager(IUserManager.class).logout(user);
        }
 
        @Override
index 687e4e20989b6ab45e802ae45f535937c9f18efa..7a5c73ea019a4a2f89799b4ec57fef226c31dc82 100644 (file)
@@ -348,8 +348,8 @@ public class Gitblit implements IRuntimeManager,
        }
 
        @Override
-       public List<String> getTeamnamesForRepositoryRole(String role) {
-               return userManager.getTeamnamesForRepositoryRole(role);
+       public List<String> getTeamNamesForRepositoryRole(String role) {
+               return userManager.getTeamNamesForRepositoryRole(role);
        }
 
        @Override
index 7278b22245437aaeaaac639026462cec7e13a4ea..715aed9d57cfa65dcfd37f078bf7e6a2f8cdf9b6 100644 (file)
@@ -126,6 +126,12 @@ public class GitblitUserService implements IUserService {
                return serviceImpl.getCookie(model);\r
        }\r
 \r
+       /**\r
+        * Authenticate a user based on their cookie.\r
+        *\r
+        * @param cookie\r
+        * @return a user object or null\r
+        */\r
        @Override\r
        public UserModel authenticate(char[] cookie) {\r
                UserModel user = serviceImpl.authenticate(cookie);\r
@@ -226,8 +232,8 @@ public class GitblitUserService implements IUserService {
        }\r
 \r
        @Override\r
-       public List<String> getTeamnamesForRepositoryRole(String role) {\r
-               return serviceImpl.getTeamnamesForRepositoryRole(role);\r
+       public List<String> getTeamNamesForRepositoryRole(String role) {\r
+               return serviceImpl.getTeamNamesForRepositoryRole(role);\r
        }\r
 \r
        @Override\r
@@ -312,7 +318,8 @@ public class GitblitUserService implements IUserService {
                }\r
        }\r
 \r
-       protected AccountType getAccountType() {\r
+       @Override\r
+       public AccountType getAccountType() {\r
                return AccountType.LOCAL;\r
        }\r
 }\r
index 3b7120f0a1053caadaf6a03058bd83981aea7529..ca5295c903dfdb393dd50ed3f410e7e2d7f01fd3 100644 (file)
@@ -275,7 +275,7 @@ public class HtpasswdUserService extends GitblitUserService
      * @return AccountType.HTPASSWD
      */
     @Override
-       protected AccountType getAccountType()
+       public AccountType getAccountType()
     {
         return AccountType.HTPASSWD;
     }
index 33f519f0e249d7e3d9e29420b995a0ac424a048a..316e4a5598b8dfc6fa19cdaf8c50ca6965a99ef6 100644 (file)
@@ -18,6 +18,7 @@ package com.gitblit;
 import java.util.Collection;\r
 import java.util.List;\r
 \r
+import com.gitblit.Constants.AccountType;\r
 import com.gitblit.manager.IRuntimeManager;\r
 import com.gitblit.models.TeamModel;\r
 import com.gitblit.models.UserModel;\r
@@ -205,7 +206,7 @@ public interface IUserService {
         * @return list of all usernames that can bypass the access restriction\r
         * @since 0.8.0\r
         */\r
-       List<String> getTeamnamesForRepositoryRole(String role);\r
+       List<String> getTeamNamesForRepositoryRole(String role);\r
 \r
        /**\r
         * Sets the list of all teams who are allowed to bypass the access\r
@@ -318,6 +319,14 @@ public interface IUserService {
         */\r
        boolean deleteRepositoryRole(String role);\r
 \r
+       /**\r
+        * Returns the account type for the user models.\r
+        *\r
+        * @return the account type\r
+        * @since 1.4.0\r
+        */\r
+       AccountType getAccountType();\r
+\r
        /**\r
         * @See java.lang.Object.toString();\r
         * @return string representation of the login service\r
index 5619dadd42dda31ea6e764c3b57a243ce568d41f..c075afca78399abcf0e73f4505050ac4c558461b 100644 (file)
@@ -262,7 +262,7 @@ public class LdapUserService extends GitblitUserService {
        }\r
 \r
        @Override\r
-       protected AccountType getAccountType() {\r
+       public AccountType getAccountType() {\r
                 return AccountType.LDAP;\r
        }\r
 \r
index b348e64c45af6ee75e93a8af39af2f175b104b54..db569fbfd70c28d1c5f4e125ce3fc0b3a053f196 100644 (file)
@@ -92,7 +92,7 @@ public class PAMUserService extends GitblitUserService {
     }\r
 \r
         @Override\r
-       protected AccountType getAccountType() {\r
+       public AccountType getAccountType() {\r
                return AccountType.PAM;\r
        }\r
 \r
index 62322ebc4f181dc104ebaf389c6c33e482d639f0..7c38ef2f1c1315bbcc1935fae277351008e5c059 100644 (file)
@@ -91,7 +91,7 @@ public class RedmineUserService extends GitblitUserService {
     }\r
 \r
         @Override\r
-       protected AccountType getAccountType() {\r
+       public AccountType getAccountType() {\r
                return AccountType.REDMINE;\r
        }\r
 \r
index 0eca6c9c526749e04ca3146c44990169d0eb436e..6161ba933338c3b9bdbc2e338bfd560b215c496a 100644 (file)
@@ -22,7 +22,7 @@ public class SalesforceUserService extends GitblitUserService {
        private IStoredSettings settings;
 
        @Override
-       protected AccountType getAccountType() {
+       public AccountType getAccountType() {
                return AccountType.SALESFORCE;
        }
 
index 9b25efbebb1dc378914e15ec2f9928d4685accfb..99077c6779120ec7091bbda6eaf40d44dca92a2f 100644 (file)
@@ -104,7 +104,7 @@ public class WindowsUserService extends GitblitUserService {
     }\r
 \r
         @Override\r
-       protected AccountType getAccountType() {\r
+       public AccountType getAccountType() {\r
                return AccountType.WINDOWS;\r
        }\r
 \r
index 3ce1e744574ad5d5ac28eb312d5fdffe7ae9c990..387a72089d23a00cdd32673ca881a5759787e9f7 100644 (file)
@@ -21,7 +21,7 @@ import java.util.List;
 import com.gitblit.models.TeamModel;
 import com.gitblit.models.UserModel;
 
-public interface IUserManager {
+public interface IUserManager extends IManager {
 
        boolean supportsAddUser();
 
@@ -189,7 +189,7 @@ public interface IUserManager {
         * @return list of all usernames that can bypass the access restriction
         * @since 0.8.0
         */
-       List<String> getTeamnamesForRepositoryRole(String role);
+       List<String> getTeamNamesForRepositoryRole(String role);
 
        /**
         * Retrieve the team object for the specified team name.
diff --git a/src/main/java/com/gitblit/manager/UserManager.java b/src/main/java/com/gitblit/manager/UserManager.java
new file mode 100644 (file)
index 0000000..f781c4c
--- /dev/null
@@ -0,0 +1,542 @@
+/*
+ * Copyright 2013 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.manager;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.gitblit.ConfigUserService;
+import com.gitblit.Constants;
+import com.gitblit.Constants.AccountType;
+import com.gitblit.IStoredSettings;
+import com.gitblit.IUserService;
+import com.gitblit.Keys;
+import com.gitblit.models.TeamModel;
+import com.gitblit.models.UserModel;
+import com.gitblit.utils.DeepCopier;
+import com.gitblit.utils.StringUtils;
+
+/**
+ * The user manager manages persistence and retrieval of users and teams.
+ *
+ * @author James Moger
+ *
+ */
+public class UserManager implements IUserManager {
+
+       private final Logger logger = LoggerFactory.getLogger(getClass());
+
+       private final IStoredSettings settings;
+
+       private final IRuntimeManager runtimeManager;
+
+       private IUserService userService;
+
+       public UserManager(IRuntimeManager runtimeManager) {
+               this.settings = runtimeManager.getSettings();
+               this.runtimeManager = runtimeManager;
+       }
+
+       /**
+        * Set the user service. The user service authenticates local users and is
+        * responsible for persisting and retrieving users and teams.
+        *
+        * @param userService
+        */
+       public void setUserService(IUserService userService) {
+               logger.info("Setting up user service " + userService.toString());
+               this.userService = userService;
+               this.userService.setup(runtimeManager);
+       }
+
+       @Override
+       public IManager setup() {
+               if (this.userService == null) {
+                       String realm = settings.getString(Keys.realm.userService, "${baseFolder}/users.properties");
+                       IUserService service = null;
+                       try {
+                               // check to see if this "file" is a login service class
+                               Class<?> realmClass = Class.forName(realm);
+                               service = (IUserService) realmClass.newInstance();
+                       } catch (Throwable t) {
+                               File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf");
+                               service = createUserService(realmFile);
+                       }
+                       setUserService(service);
+               }
+               return this;
+       }
+
+       protected IUserService createUserService(File realmFile) {
+               IUserService service = null;
+               if (realmFile.getName().toLowerCase().endsWith(".conf")) {
+                       // v0.8.0+ config-based realm file
+                       service = new ConfigUserService(realmFile);
+               }
+
+               assert service != null;
+
+               if (!realmFile.exists()) {
+                       // Create the Administrator account for a new realm file
+                       try {
+                               realmFile.createNewFile();
+                       } catch (IOException x) {
+                               logger.error(MessageFormat.format("COULD NOT CREATE REALM FILE {0}!", realmFile), x);
+                       }
+                       UserModel admin = new UserModel("admin");
+                       admin.password = "admin";
+                       admin.canAdmin = true;
+                       admin.excludeFromFederation = true;
+                       service.updateUserModel(admin);
+               }
+
+               return service;
+       }
+
+       @Override
+       public IManager stop() {
+               return this;
+       }
+
+       @Override
+       public boolean supportsAddUser() {
+               return supportsCredentialChanges(new UserModel(""));
+       }
+
+       /**
+        * Returns true if the user's credentials can be changed.
+        *
+        * @param user
+        * @return true if the user service supports credential changes
+        */
+       @Override
+       public boolean supportsCredentialChanges(UserModel user) {
+               if (user == null) {
+                       return false;
+               } else if (AccountType.LOCAL.equals(user.accountType)) {
+                       // local account, we can change credentials
+                       return true;
+               } else {
+                       // external account, ask user service
+                       return userService.supportsCredentialChanges();
+               }
+       }
+
+       /**
+        * Returns true if the user's display name can be changed.
+        *
+        * @param user
+        * @return true if the user service supports display name changes
+        */
+       @Override
+       public boolean supportsDisplayNameChanges(UserModel user) {
+               return (user != null && user.isLocalAccount()) || userService.supportsDisplayNameChanges();
+       }
+
+       /**
+        * Returns true if the user's email address can be changed.
+        *
+        * @param user
+        * @return true if the user service supports email address changes
+        */
+       @Override
+       public boolean supportsEmailAddressChanges(UserModel user) {
+               return (user != null && user.isLocalAccount()) || userService.supportsEmailAddressChanges();
+       }
+
+       /**
+        * Returns true if the user's team memberships can be changed.
+        *
+        * @param user
+        * @return true if the user service supports team membership changes
+        */
+       @Override
+       public boolean supportsTeamMembershipChanges(UserModel user) {
+               return (user != null && user.isLocalAccount()) || userService.supportsTeamMembershipChanges();
+       }
+
+       /**
+        * Allow to understand if GitBlit supports and is configured to allow
+        * cookie-based authentication.
+        *
+        * @return status of Cookie authentication enablement.
+        */
+       @Override
+       public boolean supportsCookies() {
+               return settings.getBoolean(Keys.web.allowCookieAuthentication, true) && userService.supportsCookies();
+       }
+
+       /**
+        * Returns the cookie value for the specified user.
+        *
+        * @param model
+        * @return cookie value
+        */
+       @Override
+       public String getCookie(UserModel model) {
+               return userService.getCookie(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) {
+               UserModel user = userService.authenticate(username, password);
+               setAccountType(user);
+               return user;
+       }
+
+       /**
+        * Authenticate a user based on their cookie.
+        *
+        * @param cookie
+        * @return a user object or null
+        */
+       @Override
+       public UserModel authenticate(char[] cookie) {
+               UserModel user = userService.authenticate(cookie);
+               setAccountType(user);
+               return user;
+       }
+
+       /**
+        * Logout a user.
+        *
+        * @param user
+        */
+       @Override
+       public void logout(UserModel user) {
+               if (userService == null) {
+                       return;
+               }
+               userService.logout(user);
+       }
+
+       /**
+        * Retrieve the user object for the specified username.
+        *
+        * @param username
+        * @return a user object or null
+        */
+       @Override
+       public UserModel getUserModel(String username) {
+               if (StringUtils.isEmpty(username)) {
+                       return null;
+               }
+               String usernameDecoded = StringUtils.decodeUsername(username);
+               UserModel user = userService.getUserModel(usernameDecoded);
+               setAccountType(user);
+               return user;
+       }
+
+       /**
+        * Updates/writes a complete user object.
+        *
+        * @param model
+        * @return true if update is successful
+        */
+       @Override
+       public boolean updateUserModel(UserModel model) {
+               return userService.updateUserModel(model);
+       }
+
+       /**
+        * Updates/writes all specified user objects.
+        *
+        * @param models a list of user models
+        * @return true if update is successful
+        * @since 1.2.0
+        */
+       @Override
+       public boolean updateUserModels(Collection<UserModel> models) {
+               return userService.updateUserModels(models);
+       }
+
+       /**
+        * Adds/updates a 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) {
+               if (model.isLocalAccount() || userService.supportsCredentialChanges()) {
+                       if (!model.isLocalAccount() && !userService.supportsTeamMembershipChanges()) {
+                               //  teams are externally controlled - copy from original model
+                               UserModel existingModel = getUserModel(username);
+
+                               model = DeepCopier.copy(model);
+                               model.teams.clear();
+                               model.teams.addAll(existingModel.teams);
+                       }
+                       return userService.updateUserModel(username, model);
+               }
+               if (model.username.equals(username)) {
+                       // passwords are not persisted by the backing user service
+                       model.password = null;
+                       if (!model.isLocalAccount() && !userService.supportsTeamMembershipChanges()) {
+                               //  teams are externally controlled- copy from original model
+                               UserModel existingModel = getUserModel(username);
+
+                               model = DeepCopier.copy(model);
+                               model.teams.clear();
+                               model.teams.addAll(existingModel.teams);
+                       }
+                       return userService.updateUserModel(username, model);
+               }
+               logger.error("Users can not be renamed!");
+               return false;
+       }
+
+       /**
+        * Deletes the user object from the user service.
+        *
+        * @param model
+        * @return true if successful
+        */
+       @Override
+       public boolean deleteUserModel(UserModel model) {
+               return userService.deleteUserModel(model);
+       }
+
+       /**
+        * Delete the user object with the specified username
+        *
+        * @param username
+        * @return true if successful
+        */
+       @Override
+       public boolean deleteUser(String username) {
+               if (StringUtils.isEmpty(username)) {
+                       return false;
+               }
+               String usernameDecoded = StringUtils.decodeUsername(username);
+               return userService.deleteUser(usernameDecoded);
+       }
+
+       /**
+        * Returns the list of all users available to the login service.
+        *
+        * @return list of all usernames
+        */
+       @Override
+       public List<String> getAllUsernames() {
+               List<String> names = new ArrayList<String>(userService.getAllUsernames());
+               return names;
+       }
+
+       /**
+        * Returns the list of all users available to the login service.
+        *
+        * @return list of all users
+        * @since 0.8.0
+        */
+       @Override
+       public List<UserModel> getAllUsers() {
+               List<UserModel> users = userService.getAllUsers();
+       for (UserModel user : users) {
+               setAccountType(user);
+       }
+               return users;
+       }
+
+       /**
+        * Returns the list of all teams available to the login service.
+        *
+        * @return list of all teams
+        * @since 0.8.0
+        */
+       @Override
+       public List<String> getAllTeamNames() {
+               return userService.getAllTeamNames();
+       }
+
+       /**
+        * Returns the list of all teams available to the login service.
+        *
+        * @return list of all teams
+        * @since 0.8.0
+        */
+       @Override
+       public List<TeamModel> getAllTeams() {
+               List<TeamModel> teams = userService.getAllTeams();
+               return teams;
+       }
+
+       /**
+        * Returns the list of all teams who are allowed to bypass the access
+        * restriction placed on the specified repository.
+        *
+        * @param role
+        *            the repository name
+        * @return list of all teams that can bypass the access restriction
+        * @since 0.8.0
+        */
+       @Override
+       public List<String> getTeamNamesForRepositoryRole(String role) {
+               return userService.getTeamNamesForRepositoryRole(role);
+       }
+
+       /**
+        * Retrieve the team object for the specified team name.
+        *
+        * @param teamname
+        * @return a team object or null
+        * @since 0.8.0
+        */
+       @Override
+       public TeamModel getTeamModel(String teamname) {
+               return userService.getTeamModel(teamname);
+       }
+
+       /**
+        * Updates/writes a complete team object.
+        *
+        * @param model
+        * @return true if update is successful
+        * @since 0.8.0
+        */
+       @Override
+       public boolean updateTeamModel(TeamModel model) {
+               return userService.updateTeamModel(model);
+       }
+
+       /**
+        * Updates/writes all specified team objects.
+        *
+        * @param models a list of team models
+        * @return true if update is successful
+        * @since 1.2.0
+        */
+       @Override
+       public boolean updateTeamModels(Collection<TeamModel> models) {
+               return userService.updateTeamModels(models);
+       }
+
+       /**
+        * Updates/writes and replaces a complete team object keyed by teamname.
+        * This method allows for renaming a team.
+        *
+        * @param teamname
+        *            the old teamname
+        * @param model
+        *            the team object to use for teamname
+        * @return true if update is successful
+        * @since 0.8.0
+        */
+       @Override
+       public boolean updateTeamModel(String teamname, TeamModel model) {
+               if (!userService.supportsTeamMembershipChanges()) {
+                       // teams are externally controlled - copy from original model
+                       TeamModel existingModel = getTeamModel(teamname);
+
+                       model = DeepCopier.copy(model);
+                       model.users.clear();
+                       model.users.addAll(existingModel.users);
+               }
+               return userService.updateTeamModel(teamname, model);
+       }
+
+       /**
+        * Deletes the team object from the user service.
+        *
+        * @param model
+        * @return true if successful
+        * @since 0.8.0
+        */
+       @Override
+       public boolean deleteTeamModel(TeamModel model) {
+               return userService.deleteTeamModel(model);
+       }
+
+       /**
+        * Delete the team object with the specified teamname
+        *
+        * @param teamname
+        * @return true if successful
+        * @since 0.8.0
+        */
+       @Override
+       public boolean deleteTeam(String teamname) {
+               return userService.deleteTeam(teamname);
+       }
+
+       /**
+        * 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
+        * @since 0.8.0
+        */
+       @Override
+       public List<String> getUsernamesForRepositoryRole(String role) {
+               return userService.getUsernamesForRepositoryRole(role);
+       }
+
+       /**
+        * Renames a repository role.
+        *
+        * @param oldRole
+        * @param newRole
+        * @return true if successful
+        */
+       @Override
+       public boolean renameRepositoryRole(String oldRole, String newRole) {
+               return userService.renameRepositoryRole(oldRole, newRole);
+       }
+
+       /**
+        * Removes a repository role from all users.
+        *
+        * @param role
+        * @return true if successful
+        */
+       @Override
+       public boolean deleteRepositoryRole(String role) {
+               return userService.deleteRepositoryRole(role);
+       }
+
+       protected void setAccountType(UserModel user) {
+               if (user != null) {
+                       if (!StringUtils.isEmpty(user.password)
+                                       && !Constants.EXTERNAL_ACCOUNT.equalsIgnoreCase(user.password)
+                                       && !"StoredInLDAP".equalsIgnoreCase(user.password)) {
+                               user.accountType = AccountType.LOCAL;
+                       } else {
+                               user.accountType = userService.getAccountType();
+                       }
+               }
+       }
+}
index e18bdc4f0f8d83567c62ae46f54df994cb6f13eb..ed10eb6cd86246c0ac5bf61ed0f176da4f1cd7c6 100644 (file)
@@ -747,4 +747,25 @@ public class StringUtils {
                }\r
                return input.replace('\n',' ').replace('\r',  ' ').trim();\r
        }\r
+\r
+\r
+       /**\r
+        * Encode the username for user in an url.\r
+        *\r
+        * @param name\r
+        * @return the encoded name\r
+        */\r
+       public static String encodeUsername(String name) {\r
+               return name.replace("@", "%40").replace(" ", "%20").replace("\\", "%5C");\r
+       }\r
+\r
+       /**\r
+        * Decode a username from an encoded url.\r
+        *\r
+        * @param name\r
+        * @return the decoded name\r
+        */\r
+       public static String decodeUsername(String name) {\r
+               return name.replace("%40", "@").replace("%20", " ").replace("%5C", "\\");\r
+       }\r
 }
\ No newline at end of file
index d4e3222212da40447e80c9d8d091d8411c42e1e1..9c559f880e4db9eb708f21a309d26d8230c6cd69 100644 (file)
@@ -9,7 +9,7 @@ public class GitBlit4UITests extends GitBlit {
        private boolean luceneIndexingEnabled;
 
        public GitBlit4UITests(boolean luceneIndexingEnabled) {
-               super(null);
+               super();
                this.luceneIndexingEnabled = luceneIndexingEnabled;
        }