Skip to content

Commit a5ae3da

Browse files
committed
Added GET_USER request to RPC interface (issue-275)
1 parent 5392d72 commit a5ae3da

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

releases.moxie

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ r18: {
2929
- updated Brazilian Portuguese translation
3030
additions:
3131
- Added optional browser-side page caching using Last-Modified and Cache-Control for the dashboard, activity, project, and several repository pages
32+
- Added a GET_USER request type for the RPC mechanism (issue-275)
3233
dependencyChanges: ~
3334
settings:
3435
- { name: 'web.pageCacheExpires', defaultValue: 0 }
@@ -45,6 +46,7 @@ r18: {
4546
- Rafael Cavazin
4647
- Tamás Papp
4748
- Florian Zschocke
49+
- Amélie Benoit
4850
}
4951

5052
#

src/main/java/com/gitblit/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public String toString() {
321321
public static enum RpcRequest {
322322
// Order is important here. anything above LIST_SETTINGS requires
323323
// administrator privileges and web.allowRpcManagement.
324-
CLEAR_REPOSITORY_CACHE, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, LIST_SETTINGS,
324+
CLEAR_REPOSITORY_CACHE, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER, LIST_SETTINGS,
325325
CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,
326326
LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER,
327327
LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM,

src/main/java/com/gitblit/RpcServlet.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
import com.gitblit.models.ServerSettings;
3737
import com.gitblit.models.TeamModel;
3838
import com.gitblit.models.UserModel;
39+
import com.gitblit.utils.DeepCopier;
3940
import com.gitblit.utils.HttpUtils;
4041
import com.gitblit.utils.JGitUtils;
4142
import com.gitblit.utils.RpcUtils;
43+
import com.gitblit.utils.StringUtils;
4244

4345
/**
4446
* Handles remote procedure calls.
@@ -50,7 +52,7 @@ public class RpcServlet extends JsonServlet {
5052

5153
private static final long serialVersionUID = 1L;
5254

53-
public static final int PROTOCOL_VERSION = 5;
55+
public static final int PROTOCOL_VERSION = 6;
5456

5557
public RpcServlet() {
5658
super();
@@ -132,6 +134,28 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
132134
repository.close();
133135
}
134136
result = localBranches;
137+
} else if (RpcRequest.GET_USER.equals(reqType)) {
138+
if (StringUtils.isEmpty(objectName)) {
139+
if (UserModel.ANONYMOUS.equals(user)) {
140+
response.sendError(forbiddenCode);
141+
} else {
142+
// return the current user, reset credentials
143+
UserModel requestedUser = DeepCopier.copy(user);
144+
result = requestedUser;
145+
}
146+
} else {
147+
if (user.canAdmin() || objectName.equals(user.username)) {
148+
// return the specified user
149+
UserModel requestedUser = GitBlit.self().getUserModel(objectName);
150+
if (requestedUser == null) {
151+
response.setStatus(failureCode);
152+
} else {
153+
result = requestedUser;
154+
}
155+
} else {
156+
response.sendError(forbiddenCode);
157+
}
158+
}
135159
} else if (RpcRequest.LIST_USERS.equals(reqType)) {
136160
// list users
137161
List<String> names = GitBlit.self().getAllUsernames();

src/main/java/com/gitblit/utils/RpcUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,24 @@ public static boolean deleteUser(UserModel user, String serverUrl, String accoun
297297
char[] password) throws IOException {
298298
return doAction(RpcRequest.DELETE_USER, null, user, serverUrl, account, password);
299299
}
300+
301+
/**
302+
* Tries to get the specified gitblit user account from the remote gitblit instance.
303+
* If the username is null or empty, the current user is returned.
304+
*
305+
* @param username
306+
* @param serverUrl
307+
* @param account
308+
* @param password
309+
* @return a UserModel or null
310+
* @throws IOException
311+
*/
312+
public static UserModel getUser(String username, String serverUrl, String account, char[] password)
313+
throws IOException {
314+
String url = asLink(serverUrl, RpcRequest.GET_USER);
315+
UserModel model = JsonUtils.retrieveJson(url, UserModel.class, account, password);
316+
return model;
317+
}
300318

301319
/**
302320
* Create a team on the Gitblit server.

src/site/rpc.mkd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ The Gitblit API includes methods for retrieving and interpreting RSS feeds. The
6666
<tr><td>Gitblit v0.9.0 - v1.0.0</td><td>3</td></tr>
6767
<tr><td>Gitblit v1.1.0</td><td>4</td></tr>
6868
<tr><td>Gitblit v1.2.0+</td><td>5</td></tr>
69+
<tr><td>Gitblit v1.3.1+</td><td>6</td></tr>
6970
</tbody>
7071
</table>
7172

@@ -85,6 +86,7 @@ Use *SET_REPOSITORY_TEAM_PERMISSIONS* instead.
8586
<tr><td>LIST_REPOSITORIES</td><td>-</td><td>-</td><td>1</td><td>-</td><td>Map&lt;String, RepositoryModel&gt;</td></tr>
8687
<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>
8788
<tr><td>LIST_SETTINGS</td><td>-</td><td><em>-</em></td><td>1</td><td>-</td><td>ServerSettings (basic keys)</td></tr>
89+
<tr><td>GET_USER</td><td>user name</td><td>-</td><td>6</td><td>-</td><td>UserModel</td></tr>
8890
<tr><td colspan='6'><em>web.enableRpcManagement=true</em></td></tr>
8991
<tr><td>CREATE_REPOSITORY</td><td>repository name</td><td><em>admin</em></td><td>1</td><td>RepositoryModel</td><td>-</td></tr>
9092
<tr><td>EDIT_REPOSITORY</td><td>repository name</td><td><em>admin</em></td><td>1</td><td>RepositoryModel</td><td>-</td></tr>

src/test/java/com/gitblit/tests/RpcTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.gitblit.Constants.AuthorizationControl;
3838
import com.gitblit.Constants.PermissionType;
3939
import com.gitblit.Constants.RegistrantType;
40+
import com.gitblit.GitBlitException.NotAllowedException;
4041
import com.gitblit.GitBlitException.UnauthorizedException;
4142
import com.gitblit.Keys;
4243
import com.gitblit.RpcServlet;
@@ -102,6 +103,20 @@ public void testListUsers() throws IOException {
102103
list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
103104
assertTrue("User list is empty!", list.size() > 0);
104105
}
106+
107+
@Test
108+
public void testGetUser() throws IOException {
109+
UserModel user = null;
110+
try {
111+
user = RpcUtils.getUser("admin", url, null, null);
112+
} catch (NotAllowedException e) {
113+
}
114+
assertNull("Server allows anyone to get user!", user);
115+
116+
user = RpcUtils.getUser("admin", url, "admin", "admin".toCharArray());
117+
assertEquals("User is not the admin!", "admin", user.username);
118+
assertTrue("User is not an administrator!", user.canAdmin());
119+
}
105120

106121
@Test
107122
public void testListTeams() throws IOException {

0 commit comments

Comments
 (0)