From a5ae3da334fc82c60d375b764065198ec54f2d31 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 22 Jul 2013 09:38:55 -0400 Subject: [PATCH] Added GET_USER request to RPC interface (issue-275) --- releases.moxie | 2 ++ src/main/java/com/gitblit/Constants.java | 2 +- src/main/java/com/gitblit/RpcServlet.java | 26 ++++++++++++++++++- src/main/java/com/gitblit/utils/RpcUtils.java | 18 +++++++++++++ src/site/rpc.mkd | 2 ++ src/test/java/com/gitblit/tests/RpcTests.java | 15 +++++++++++ 6 files changed, 63 insertions(+), 2 deletions(-) diff --git a/releases.moxie b/releases.moxie index 0e984414..addf96e5 100644 --- a/releases.moxie +++ b/releases.moxie @@ -29,6 +29,7 @@ r18: { - updated Brazilian Portuguese translation additions: - Added optional browser-side page caching using Last-Modified and Cache-Control for the dashboard, activity, project, and several repository pages + - Added a GET_USER request type for the RPC mechanism (issue-275) dependencyChanges: ~ settings: - { name: 'web.pageCacheExpires', defaultValue: 0 } @@ -45,6 +46,7 @@ r18: { - Rafael Cavazin - Tamás Papp - Florian Zschocke + - Amélie Benoit } # 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 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 Gitblit v0.9.0 - v1.0.03 Gitblit v1.1.04 Gitblit v1.2.0+5 +Gitblit v1.3.1+6 @@ -85,6 +86,7 @@ Use *SET_REPOSITORY_TEAM_PERMISSIONS* instead. LIST_REPOSITORIES--1-Map<String, RepositoryModel> LIST_BRANCHES--1-Map<String, List<String>> LIST_SETTINGS--1-ServerSettings (basic keys) +GET_USERuser name-6-UserModel web.enableRpcManagement=true CREATE_REPOSITORYrepository nameadmin1RepositoryModel- EDIT_REPOSITORYrepository nameadmin1RepositoryModel- 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 { -- 2.39.5