summaryrefslogtreecommitdiffstats
path: root/src/com/gitblit
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2011-12-13 17:36:58 -0500
committerJames Moger <james.moger@gitblit.com>2011-12-13 17:36:58 -0500
commit997c16d6826cfa1bef33ba08e15055cc407b9398 (patch)
tree124c737565cfdec1eddce9b9432f4e5798a380b2 /src/com/gitblit
parentd17f433bf1c709b4daab58f503a142dd5e19cc4a (diff)
downloadgitblit-997c16d6826cfa1bef33ba08e15055cc407b9398.tar.gz
gitblit-997c16d6826cfa1bef33ba08e15055cc407b9398.zip
Federation support for Teams
Diffstat (limited to 'src/com/gitblit')
-rw-r--r--src/com/gitblit/Constants.java2
-rw-r--r--src/com/gitblit/FederationPullExecutor.java32
-rw-r--r--src/com/gitblit/FederationServlet.java20
-rw-r--r--src/com/gitblit/models/UserModel.java12
-rw-r--r--src/com/gitblit/utils/FederationUtils.java18
5 files changed, 80 insertions, 4 deletions
diff --git a/src/com/gitblit/Constants.java b/src/com/gitblit/Constants.java
index 32799808..c2d5eb26 100644
--- a/src/com/gitblit/Constants.java
+++ b/src/com/gitblit/Constants.java
@@ -117,7 +117,7 @@ public class Constants {
* Enumeration representing the types of federation requests.
*/
public static enum FederationRequest {
- POKE, PROPOSAL, PULL_REPOSITORIES, PULL_USERS, PULL_SETTINGS, STATUS;
+ POKE, PROPOSAL, PULL_REPOSITORIES, PULL_USERS, PULL_TEAMS, PULL_SETTINGS, STATUS;
public static FederationRequest fromName(String name) {
for (FederationRequest type : values()) {
diff --git a/src/com/gitblit/FederationPullExecutor.java b/src/com/gitblit/FederationPullExecutor.java
index 20fd67c6..c84761b3 100644
--- a/src/com/gitblit/FederationPullExecutor.java
+++ b/src/com/gitblit/FederationPullExecutor.java
@@ -47,6 +47,7 @@ import com.gitblit.Constants.FederationStrategy;
import com.gitblit.GitBlitException.ForbiddenException;
import com.gitblit.models.FederationModel;
import com.gitblit.models.RepositoryModel;
+import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.FederationUtils;
import com.gitblit.utils.JGitUtils;
@@ -282,10 +283,12 @@ public class FederationPullExecutor implements Runnable {
try {
// Pull USERS
+ // TeamModels are automatically pulled because they are contained
+ // within the UserModel. The UserService creates unknown teams
+ // and updates existing teams.
Collection<UserModel> users = FederationUtils.getUsers(registration);
if (users != null && users.size() > 0) {
- File realmFile = new File(registrationFolderFile, registration.name
- + "_users.conf");
+ File realmFile = new File(registrationFolderFile, registration.name + "_users.conf");
realmFile.delete();
ConfigUserService userService = new ConfigUserService(realmFile);
for (UserModel user : users) {
@@ -318,6 +321,31 @@ public class FederationPullExecutor implements Runnable {
localUser.canAdmin = user.canAdmin;
GitBlit.self().updateUserModel(localUser.username, localUser, false);
}
+
+ for (String teamname : GitBlit.self().getAllTeamnames()) {
+ TeamModel team = GitBlit.self().getTeamModel(teamname);
+ if (user.isTeamMember(teamname) && !team.hasUser(user.username)) {
+ // new team member
+ team.addUser(user.username);
+ GitBlit.self().updateTeamModel(teamname, team, false);
+ } else if (!user.isTeamMember(teamname) && team.hasUser(user.username)) {
+ // remove team member
+ team.removeUser(user.username);
+ GitBlit.self().updateTeamModel(teamname, team, false);
+ }
+
+ // update team repositories
+ TeamModel remoteTeam = user.getTeam(teamname);
+ if (remoteTeam != null && remoteTeam.repositories != null) {
+ int before = team.repositories.size();
+ team.addRepositories(remoteTeam.repositories);
+ int after = team.repositories.size();
+ if (after > before) {
+ // repository count changed, update
+ GitBlit.self().updateTeamModel(teamname, team, false);
+ }
+ }
+ }
}
}
}
diff --git a/src/com/gitblit/FederationServlet.java b/src/com/gitblit/FederationServlet.java
index 0be1066f..f2ed903c 100644
--- a/src/com/gitblit/FederationServlet.java
+++ b/src/com/gitblit/FederationServlet.java
@@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletResponse;
import com.gitblit.Constants.FederationRequest;
import com.gitblit.models.FederationModel;
import com.gitblit.models.FederationProposal;
+import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.FederationUtils;
import com.gitblit.utils.HttpUtils;
@@ -90,7 +91,7 @@ public class FederationServlet extends JsonServlet {
if (proposal == null) {
return;
}
-
+
// reject proposal, if not receipt prohibited
if (!GitBlit.getBoolean(Keys.federation.allowProposals, false)) {
logger.error(MessageFormat.format("Rejected {0} federation proposal from {1}",
@@ -198,6 +199,23 @@ public class FederationServlet extends JsonServlet {
}
}
result = users;
+ } else if (FederationRequest.PULL_TEAMS.equals(reqType)) {
+ // pull teams
+ if (!GitBlit.self().validateFederationRequest(reqType, token)) {
+ // invalid token to pull teams
+ logger.warn(MessageFormat.format(
+ "Federation token from {0} not authorized to pull TEAMS",
+ request.getRemoteAddr()));
+ response.sendError(HttpServletResponse.SC_FORBIDDEN);
+ return;
+ }
+ List<String> teamnames = GitBlit.self().getAllTeamnames();
+ List<TeamModel> teams = new ArrayList<TeamModel>();
+ for (String teamname : teamnames) {
+ TeamModel user = GitBlit.self().getTeamModel(teamname);
+ teams.add(user);
+ }
+ result = teams;
}
}
diff --git a/src/com/gitblit/models/UserModel.java b/src/com/gitblit/models/UserModel.java
index bd8974d7..ecb97cfc 100644
--- a/src/com/gitblit/models/UserModel.java
+++ b/src/com/gitblit/models/UserModel.java
@@ -96,6 +96,18 @@ public class UserModel implements Principal, Serializable, Comparable<UserModel>
return false;
}
+ public TeamModel getTeam(String teamname) {
+ if (teams == null) {
+ return null;
+ }
+ for (TeamModel team : teams) {
+ if (team.name.equalsIgnoreCase(teamname)) {
+ return team;
+ }
+ }
+ return null;
+ }
+
@Override
public String getName() {
return username;
diff --git a/src/com/gitblit/utils/FederationUtils.java b/src/com/gitblit/utils/FederationUtils.java
index 324aa67e..8207962a 100644
--- a/src/com/gitblit/utils/FederationUtils.java
+++ b/src/com/gitblit/utils/FederationUtils.java
@@ -38,6 +38,7 @@ import com.gitblit.Keys;
import com.gitblit.models.FederationModel;
import com.gitblit.models.FederationProposal;
import com.gitblit.models.RepositoryModel;
+import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
import com.google.gson.reflect.TypeToken;
@@ -58,6 +59,9 @@ public class FederationUtils {
private static final Type USERS_TYPE = new TypeToken<Collection<UserModel>>() {
}.getType();
+ private static final Type TEAMS_TYPE = new TypeToken<Collection<TeamModel>>() {
+ }.getType();
+
private static final Logger LOGGER = LoggerFactory.getLogger(FederationUtils.class);
/**
@@ -281,6 +285,20 @@ public class FederationUtils {
}
/**
+ * Tries to pull the gitblit team definitions from the remote gitblit instance.
+ *
+ * @param registration
+ * @return a collection of TeamModel objects
+ * @throws Exception
+ */
+ public static List<TeamModel> getTeams(FederationModel registration) throws Exception {
+ String url = asLink(registration.url, registration.token, FederationRequest.PULL_TEAMS);
+ Collection<TeamModel> models = JsonUtils.retrieveJson(url, TEAMS_TYPE);
+ List<TeamModel> list = new ArrayList<TeamModel>(models);
+ return list;
+ }
+
+ /**
* Tries to pull the gitblit server settings from the remote gitblit
* instance.
*