James Moger
2013-01-05 9e186eedf1a09ca7ac4fbdea32b00e7e5331f7eb
src/com/gitblit/RpcServlet.java
@@ -30,9 +30,11 @@
import org.eclipse.jgit.lib.Repository;
import com.gitblit.Constants.RpcRequest;
import com.gitblit.models.RegistrantAccessPermission;
import com.gitblit.models.RefModel;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.ServerSettings;
import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.HttpUtils;
import com.gitblit.utils.JGitUtils;
@@ -47,6 +49,8 @@
public class RpcServlet extends JsonServlet {
   private static final long serialVersionUID = 1L;
   public static final int PROTOCOL_VERSION = 5;
   public RpcServlet() {
      super();
@@ -70,14 +74,17 @@
      UserModel user = (UserModel) request.getUserPrincipal();
      boolean allowManagement = user != null && user.canAdmin
      boolean allowManagement = user != null && user.canAdmin()
            && GitBlit.getBoolean(Keys.web.enableRpcManagement, false);
      boolean allowAdmin = user != null && user.canAdmin
      boolean allowAdmin = user != null && user.canAdmin()
            && GitBlit.getBoolean(Keys.web.enableRpcAdministration, false);
      Object result = null;
      if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
      if (RpcRequest.GET_PROTOCOL.equals(reqType)) {
         // Return the protocol version
         result = PROTOCOL_VERSION;
      } else if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
         // Determine the Gitblit clone url
         String gitblitUrl = HttpUtils.getGitblitURL(request);
         StringBuilder sb = new StringBuilder();
@@ -101,6 +108,11 @@
         for (RepositoryModel model : models) {
            if (!model.hasCommits) {
               // skip empty repository
               continue;
            }
            if (model.isCollectingGarbage) {
               // skip garbage collecting repository
               logger.warn(MessageFormat.format("Temporarily excluding {0} from RPC, busy collecting garbage", model.name));
               continue;
            }
            // get local branches
@@ -128,6 +140,14 @@
            users.add(GitBlit.self().getUserModel(name));
         }
         result = users;
      } else if (RpcRequest.LIST_TEAMS.equals(reqType)) {
         // list teams
         List<String> names = GitBlit.self().getAllTeamnames();
         List<TeamModel> teams = new ArrayList<TeamModel>();
         for (String name : names) {
            teams.add(GitBlit.self().getTeamModel(name));
         }
         result = teams;
      } else if (RpcRequest.CREATE_REPOSITORY.equals(reqType)) {
         // create repository
         RepositoryModel model = deserialize(request, response, RepositoryModel.class);
@@ -180,18 +200,65 @@
         if (!GitBlit.self().deleteUser(model.username)) {
            response.setStatus(failureCode);
         }
      } else if (RpcRequest.CREATE_TEAM.equals(reqType)) {
         // create team
         TeamModel model = deserialize(request, response, TeamModel.class);
         try {
            GitBlit.self().updateTeamModel(model.name, model, true);
         } catch (GitBlitException e) {
            response.setStatus(failureCode);
         }
      } else if (RpcRequest.EDIT_TEAM.equals(reqType)) {
         // edit team
         TeamModel model = deserialize(request, response, TeamModel.class);
         // name parameter specifies original team name in event of rename
         String teamname = objectName;
         if (teamname == null) {
            teamname = model.name;
         }
         try {
            GitBlit.self().updateTeamModel(teamname, model, false);
         } catch (GitBlitException e) {
            response.setStatus(failureCode);
         }
      } else if (RpcRequest.DELETE_TEAM.equals(reqType)) {
         // delete team
         TeamModel model = deserialize(request, response, TeamModel.class);
         if (!GitBlit.self().deleteTeam(model.name)) {
            response.setStatus(failureCode);
         }
      } else if (RpcRequest.LIST_REPOSITORY_MEMBERS.equals(reqType)) {
         // get repository members
         RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
         result = GitBlit.self().getRepositoryUsers(model);
      } else if (RpcRequest.SET_REPOSITORY_MEMBERS.equals(reqType)) {
         // update repository access list
         // rejected since 1.2.0
         response.setStatus(failureCode);
      } else if (RpcRequest.LIST_REPOSITORY_MEMBER_PERMISSIONS.equals(reqType)) {
         // get repository member permissions
         RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
         Collection<String> names = deserialize(request, response, RpcUtils.NAMES_TYPE);
         List<String> users = new ArrayList<String>(names);
         if (!GitBlit.self().setRepositoryUsers(model, users)) {
            response.setStatus(failureCode);
         }
         result = GitBlit.self().getUserAccessPermissions(model);
      } else if (RpcRequest.SET_REPOSITORY_MEMBER_PERMISSIONS.equals(reqType)) {
         // set the repository permissions for the specified users
         RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
         Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
         result = GitBlit.self().setUserAccessPermissions(model, permissions);
      } else if (RpcRequest.LIST_REPOSITORY_TEAMS.equals(reqType)) {
         // get repository teams
         RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
         result = GitBlit.self().getRepositoryTeams(model);
      } else if (RpcRequest.SET_REPOSITORY_TEAMS.equals(reqType)) {
         // rejected since 1.2.0
         response.setStatus(failureCode);
      } else if (RpcRequest.LIST_REPOSITORY_TEAM_PERMISSIONS.equals(reqType)) {
         // get repository team permissions
         RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
         result = GitBlit.self().getTeamAccessPermissions(model);
      } else if (RpcRequest.SET_REPOSITORY_TEAM_PERMISSIONS.equals(reqType)) {
         // set the repository permissions for the specified teams
         RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
         Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
         result = GitBlit.self().setTeamAccessPermissions(model, permissions);
      } else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
         // return the list of federation registrations
         if (allowAdmin) {
@@ -232,7 +299,8 @@
            List<String> keys = new ArrayList<String>();
            keys.add(Keys.web.siteName);
            keys.add(Keys.web.mountParameters);
            keys.add(Keys.web.syndicationEntries);
            if (allowManagement) {
               // keys necessary for repository and/or user management
               keys.add(Keys.realm.minPasswordLength);
@@ -243,6 +311,9 @@
            ServerSettings managementSettings = new ServerSettings();
            for (String key : keys) {
               managementSettings.add(settings.get(key));
            }
            if (allowManagement) {
               managementSettings.pushScripts = settings.pushScripts;
            }
            result = managementSettings;
         }
@@ -262,6 +333,13 @@
         } else {
            response.sendError(notAllowedCode);
         }
      } else if (RpcRequest.CLEAR_REPOSITORY_CACHE.equals(reqType)) {
         // clear the repository list cache
         if (allowManagement) {
            GitBlit.self().resetRepositoryListCache();
         } else {
            response.sendError(notAllowedCode);
         }
      }
      // send the result of the request