James Moger
2013-07-22 a5ae3da334fc82c60d375b764065198ec54f2d31
Added GET_USER request to RPC interface (issue-275)
6 files modified
65 ■■■■■ changed files
releases.moxie 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/Constants.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/gitblit/RpcServlet.java 26 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/utils/RpcUtils.java 18 ●●●●● patch | view | raw | blame | history
src/site/rpc.mkd 2 ●●●●● patch | view | raw | blame | history
src/test/java/com/gitblit/tests/RpcTests.java 15 ●●●●● patch | view | raw | blame | history
releases.moxie
@@ -29,6 +29,7 @@
    - 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 @@
    - Rafael Cavazin
    - Tamás Papp
    - Florian Zschocke
    - Amélie Benoit
}
#
src/main/java/com/gitblit/Constants.java
@@ -321,7 +321,7 @@
    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,
src/main/java/com/gitblit/RpcServlet.java
@@ -36,9 +36,11 @@
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 @@
    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 @@
                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();
src/main/java/com/gitblit/utils/RpcUtils.java
@@ -297,6 +297,24 @@
            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.
src/site/rpc.mkd
@@ -66,6 +66,7 @@
<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 @@
<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>
src/test/java/com/gitblit/tests/RpcTests.java
@@ -37,6 +37,7 @@
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 @@
        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 {