summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2013-07-22 09:38:55 -0400
committerJames Moger <james.moger@gitblit.com>2013-07-22 09:38:55 -0400
commita5ae3da334fc82c60d375b764065198ec54f2d31 (patch)
tree5601401e732959a27b829df20c93de98beba3dbe /src
parent5392d72bd34dcc63861d0084ebe2529523d4bb5f (diff)
downloadgitblit-a5ae3da334fc82c60d375b764065198ec54f2d31.tar.gz
gitblit-a5ae3da334fc82c60d375b764065198ec54f2d31.zip
Added GET_USER request to RPC interface (issue-275)
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/gitblit/Constants.java2
-rw-r--r--src/main/java/com/gitblit/RpcServlet.java26
-rw-r--r--src/main/java/com/gitblit/utils/RpcUtils.java18
-rw-r--r--src/site/rpc.mkd2
-rw-r--r--src/test/java/com/gitblit/tests/RpcTests.java15
5 files changed, 61 insertions, 2 deletions
diff --git a/src/main/java/com/gitblit/Constants.java b/src/main/java/com/gitblit/Constants.java
index aa3767c3..67f9d65d 100644
--- a/src/main/java/com/gitblit/Constants.java
+++ b/src/main/java/com/gitblit/Constants.java
@@ -321,7 +321,7 @@ public class Constants {
public static enum RpcRequest {
// Order is important here. anything above LIST_SETTINGS requires
// administrator privileges and web.allowRpcManagement.
- CLEAR_REPOSITORY_CACHE, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, LIST_SETTINGS,
+ CLEAR_REPOSITORY_CACHE, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER, LIST_SETTINGS,
CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,
LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER,
LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM,
diff --git a/src/main/java/com/gitblit/RpcServlet.java b/src/main/java/com/gitblit/RpcServlet.java
index f31bf862..89df4731 100644
--- a/src/main/java/com/gitblit/RpcServlet.java
+++ b/src/main/java/com/gitblit/RpcServlet.java
@@ -36,9 +36,11 @@ import com.gitblit.models.RepositoryModel;
import com.gitblit.models.ServerSettings;
import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
+import com.gitblit.utils.DeepCopier;
import com.gitblit.utils.HttpUtils;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.RpcUtils;
+import com.gitblit.utils.StringUtils;
/**
* Handles remote procedure calls.
@@ -50,7 +52,7 @@ public class RpcServlet extends JsonServlet {
private static final long serialVersionUID = 1L;
- public static final int PROTOCOL_VERSION = 5;
+ public static final int PROTOCOL_VERSION = 6;
public RpcServlet() {
super();
@@ -132,6 +134,28 @@ public class RpcServlet extends JsonServlet {
repository.close();
}
result = localBranches;
+ } else if (RpcRequest.GET_USER.equals(reqType)) {
+ if (StringUtils.isEmpty(objectName)) {
+ if (UserModel.ANONYMOUS.equals(user)) {
+ response.sendError(forbiddenCode);
+ } else {
+ // return the current user, reset credentials
+ UserModel requestedUser = DeepCopier.copy(user);
+ result = requestedUser;
+ }
+ } else {
+ if (user.canAdmin() || objectName.equals(user.username)) {
+ // return the specified user
+ UserModel requestedUser = GitBlit.self().getUserModel(objectName);
+ if (requestedUser == null) {
+ response.setStatus(failureCode);
+ } else {
+ result = requestedUser;
+ }
+ } else {
+ response.sendError(forbiddenCode);
+ }
+ }
} else if (RpcRequest.LIST_USERS.equals(reqType)) {
// list users
List<String> names = GitBlit.self().getAllUsernames();
diff --git a/src/main/java/com/gitblit/utils/RpcUtils.java b/src/main/java/com/gitblit/utils/RpcUtils.java
index cd80fc45..290be49e 100644
--- a/src/main/java/com/gitblit/utils/RpcUtils.java
+++ b/src/main/java/com/gitblit/utils/RpcUtils.java
@@ -297,6 +297,24 @@ public class RpcUtils {
char[] password) throws IOException {
return doAction(RpcRequest.DELETE_USER, null, user, serverUrl, account, password);
}
+
+ /**
+ * Tries to get the specified gitblit user account from the remote gitblit instance.
+ * If the username is null or empty, the current user is returned.
+ *
+ * @param username
+ * @param serverUrl
+ * @param account
+ * @param password
+ * @return a UserModel or null
+ * @throws IOException
+ */
+ public static UserModel getUser(String username, String serverUrl, String account, char[] password)
+ throws IOException {
+ String url = asLink(serverUrl, RpcRequest.GET_USER);
+ UserModel model = JsonUtils.retrieveJson(url, UserModel.class, account, password);
+ return model;
+ }
/**
* Create a team on the Gitblit server.
diff --git a/src/site/rpc.mkd b/src/site/rpc.mkd
index 26575d90..8f562296 100644
--- a/src/site/rpc.mkd
+++ b/src/site/rpc.mkd
@@ -66,6 +66,7 @@ The Gitblit API includes methods for retrieving and interpreting RSS feeds. The
<tr><td>Gitblit v0.9.0 - v1.0.0</td><td>3</td></tr>
<tr><td>Gitblit v1.1.0</td><td>4</td></tr>
<tr><td>Gitblit v1.2.0+</td><td>5</td></tr>
+<tr><td>Gitblit v1.3.1+</td><td>6</td></tr>
</tbody>
</table>
@@ -85,6 +86,7 @@ Use *SET_REPOSITORY_TEAM_PERMISSIONS* instead.
<tr><td>LIST_REPOSITORIES</td><td>-</td><td>-</td><td>1</td><td>-</td><td>Map&lt;String, RepositoryModel&gt;</td></tr>
<tr><td>LIST_BRANCHES</td><td>-</td><td>-</td><td>1</td><td>-</td><td>Map&lt;String, List&lt;String&gt;&gt;</td></tr>
<tr><td>LIST_SETTINGS</td><td>-</td><td><em>-</em></td><td>1</td><td>-</td><td>ServerSettings (basic keys)</td></tr>
+<tr><td>GET_USER</td><td>user name</td><td>-</td><td>6</td><td>-</td><td>UserModel</td></tr>
<tr><td colspan='6'><em>web.enableRpcManagement=true</em></td></tr>
<tr><td>CREATE_REPOSITORY</td><td>repository name</td><td><em>admin</em></td><td>1</td><td>RepositoryModel</td><td>-</td></tr>
<tr><td>EDIT_REPOSITORY</td><td>repository name</td><td><em>admin</em></td><td>1</td><td>RepositoryModel</td><td>-</td></tr>
diff --git a/src/test/java/com/gitblit/tests/RpcTests.java b/src/test/java/com/gitblit/tests/RpcTests.java
index bd7f2779..2f0cffe2 100644
--- a/src/test/java/com/gitblit/tests/RpcTests.java
+++ b/src/test/java/com/gitblit/tests/RpcTests.java
@@ -37,6 +37,7 @@ import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Constants.AuthorizationControl;
import com.gitblit.Constants.PermissionType;
import com.gitblit.Constants.RegistrantType;
+import com.gitblit.GitBlitException.NotAllowedException;
import com.gitblit.GitBlitException.UnauthorizedException;
import com.gitblit.Keys;
import com.gitblit.RpcServlet;
@@ -102,6 +103,20 @@ public class RpcTests {
list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
assertTrue("User list is empty!", list.size() > 0);
}
+
+ @Test
+ public void testGetUser() throws IOException {
+ UserModel user = null;
+ try {
+ user = RpcUtils.getUser("admin", url, null, null);
+ } catch (NotAllowedException e) {
+ }
+ assertNull("Server allows anyone to get user!", user);
+
+ user = RpcUtils.getUser("admin", url, "admin", "admin".toCharArray());
+ assertEquals("User is not the admin!", "admin", user.username);
+ assertTrue("User is not an administrator!", user.canAdmin());
+ }
@Test
public void testListTeams() throws IOException {