]> source.dussan.org Git - gitblit.git/commitdiff
Separate management from administration in rpc servlet.
authorJames Moger <james.moger@gitblit.com>
Sun, 23 Oct 2011 14:25:39 +0000 (10:25 -0400)
committerJames Moger <james.moger@gitblit.com>
Sun, 23 Oct 2011 14:25:39 +0000 (10:25 -0400)
Added skeleton mechanism to update server settings via rpc.

distrib/gitblit.properties
src/com/gitblit/Constants.java
src/com/gitblit/GitBlit.java
src/com/gitblit/RpcFilter.java
src/com/gitblit/RpcServlet.java
src/com/gitblit/client/GitblitClient.java
src/com/gitblit/client/GitblitPanel.java
src/com/gitblit/utils/RpcUtils.java

index d5e457201f1a130562661926e1aaa2ab95f12838..28a6db8345bdf03fd284d5144cc29e42a03928b7 100644 (file)
@@ -93,8 +93,16 @@ web.allowAdministration = true
 # SINCE 0.7.0 \r
 web.enableRpcServlet = true\r
 \r
-# Allows remote clients to administer the Gitblit instance, if the authenticated\r
-# account has administrator permissions.  Requires *web.enableRpcServlet=true*.\r
+# Allows remote clients to manage repositories and users of the Gitblit instance,\r
+# if the authenticated account has administrator permissions.\r
+# Requires *web.enableRpcServlet=true*.\r
+#\r
+# SINCE 0.7.0 \r
+web.enableRpcManagement = false\r
+\r
+# Allows remote clients to control the server settings of the Gitblit instance,\r
+# if the authenticated account has administrator permissions.\r
+# Requires *web.enableRpcServlet=true* and *web.enableRpcManagement*.\r
 #\r
 # SINCE 0.7.0 \r
 web.enableRpcAdministration = false\r
index 79dfaf6a9fa791b09e8fe233b1ec7ad6ad4cddc8..450c11fba0bddbd7b65e0f256e66d0d461aca12f 100644 (file)
@@ -204,7 +204,7 @@ public class Constants {
                LIST_REPOSITORIES, CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,\r
                LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER, LIST_REPOSITORY_MEMBERS,\r
                SET_REPOSITORY_MEMBERS, LIST_FEDERATION_REGISTRATIONS, LIST_FEDERATION_RESULTS,\r
-               LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS, LIST_SETTINGS,\r
+               LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS, LIST_SETTINGS, EDIT_SETTINGS,\r
                LIST_STATUS;\r
 \r
                public static RpcRequest fromName(String name) {\r
index 6f537ec641954a2bf92bb394b87dda230656cfa5..49f93e4e99753d49ffde8fce7f501e1e511e6f6a 100644 (file)
@@ -25,6 +25,7 @@ import java.lang.reflect.Field;
 import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
 import java.util.Arrays;\r
+import java.util.Collection;\r
 import java.util.Collections;\r
 import java.util.HashMap;\r
 import java.util.List;\r
@@ -67,9 +68,9 @@ import com.gitblit.models.FederationSet;
 import com.gitblit.models.Metric;\r
 import com.gitblit.models.ObjectCache;\r
 import com.gitblit.models.RepositoryModel;\r
+import com.gitblit.models.ServerSettings;\r
 import com.gitblit.models.ServerStatus;\r
 import com.gitblit.models.SettingModel;\r
-import com.gitblit.models.ServerSettings;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.utils.ByteFormat;\r
 import com.gitblit.utils.FederationUtils;\r
@@ -246,6 +247,17 @@ public class GitBlit implements ServletContextListener {
                return self().settings.getBoolean(Keys.web.debugMode, false);\r
        }\r
 \r
+       /**\r
+        * Updates the list of server settings.\r
+        * \r
+        * @param settings\r
+        * @return true if the update succeeded\r
+        */\r
+       public boolean updateSettings(Collection<SettingModel> settings) {\r
+               // TODO update the settings\r
+               return false;\r
+       }\r
+\r
        public ServerStatus getStatus() {\r
                // update heap memory status\r
                serverStatus.heapAllocated = Runtime.getRuntime().totalMemory();\r
@@ -442,10 +454,9 @@ public class GitBlit implements ServletContextListener {
                        throws GitBlitException {\r
                if (!username.equalsIgnoreCase(user.username)) {\r
                        if (userService.getUserModel(user.username) != null) {\r
-                               throw new GitBlitException(\r
-                                               MessageFormat\r
-                                                               .format("Failed to rename ''{0}'' because ''{1}'' already exists.",\r
-                                                                               username, user.username));\r
+                               throw new GitBlitException(MessageFormat.format(\r
+                                               "Failed to rename ''{0}'' because ''{1}'' already exists.", username,\r
+                                               user.username));\r
                        }\r
                }\r
                if (!userService.updateUserModel(username, user)) {\r
@@ -735,10 +746,9 @@ public class GitBlit implements ServletContextListener {
                                        repository.name += org.eclipse.jgit.lib.Constants.DOT_GIT_EXT;\r
                                }\r
                                if (new File(repositoriesFolder, repository.name).exists()) {\r
-                                       throw new GitBlitException(\r
-                                                       MessageFormat\r
-                                                                       .format("Failed to rename ''{0}'' because ''{1}'' already exists.",\r
-                                                                                       repositoryName, repository.name));\r
+                                       throw new GitBlitException(MessageFormat.format(\r
+                                                       "Failed to rename ''{0}'' because ''{1}'' already exists.",\r
+                                                       repositoryName, repository.name));\r
                                }\r
                                closeRepository(repositoryName);\r
                                File folder = new File(repositoriesFolder, repositoryName);\r
index 2786f2a4eab9393310c724d49ce789090d9a0d8b..551ca3c682f33119f724a5fb3c94765bef6eaf15 100644 (file)
@@ -60,7 +60,7 @@ public class RpcFilter extends AuthenticationFilter {
                String fullUrl = getFullUrl(httpRequest);\r
                RpcRequest requestType = RpcRequest.fromName(httpRequest.getParameter("req"));\r
                if (requestType == null) {\r
-                       httpResponse.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);                 \r
+                       httpResponse.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);\r
                        return;\r
                }\r
 \r
@@ -75,22 +75,23 @@ public class RpcFilter extends AuthenticationFilter {
 \r
                boolean authenticateView = GitBlit.getBoolean(Keys.web.authenticateViewPages, false);\r
                boolean authenticateAdmin = GitBlit.getBoolean(Keys.web.authenticateAdminPages, true);\r
-               \r
-               // Wrap the HttpServletRequest with the RpcServletnRequest which\r
+\r
+               // Wrap the HttpServletRequest with the RpcServletRequest which\r
                // overrides the servlet container user principal methods.\r
                AuthenticatedRequest authenticatedRequest = new AuthenticatedRequest(httpRequest);\r
                UserModel user = getUser(httpRequest);\r
                if (user != null) {\r
                        authenticatedRequest.setUser(user);\r
                }\r
-               \r
-               // conditionally reject rpc administration requests\r
-               if (adminRequest && !GitBlit.getBoolean(Keys.web.enableRpcAdministration, false)) {\r
-                       logger.warn(Keys.web.enableRpcAdministration + " must be set TRUE for administrative rpc requests.");\r
+\r
+               // conditionally reject rpc management/administration requests\r
+               if (adminRequest && !GitBlit.getBoolean(Keys.web.enableRpcManagement, false)) {\r
+                       logger.warn(Keys.web.enableRpcManagement\r
+                                       + " must be set TRUE for management/administrative rpc requests.");\r
                        httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN);\r
                        return;\r
                }\r
-               \r
+\r
                // BASIC authentication challenge and response processing\r
                if ((adminRequest && authenticateAdmin) || (!adminRequest && authenticateView)) {\r
                        if (user == null) {\r
index ceed35e193cc2f5a2a337d0168b360e411d64bcc..7cf3a598ae8c63683e4b9180982f89f0764e45c0 100644 (file)
@@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
 \r
 import com.gitblit.Constants.RpcRequest;\r
 import com.gitblit.models.RepositoryModel;\r
+import com.gitblit.models.SettingModel;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.utils.HttpUtils;\r
 import com.gitblit.utils.RpcUtils;\r
@@ -182,7 +183,20 @@ public class RpcServlet extends JsonServlet {
                        }\r
                } else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {\r
                        // return the server's settings\r
-                       result = GitBlit.self().getSettingsModel();\r
+                       if (GitBlit.getBoolean(Keys.web.enableRpcAdministration, false)) {\r
+                               result = GitBlit.self().getSettingsModel();\r
+                       } else {\r
+                               response.sendError(notAllowedCode);\r
+                       }\r
+               } else if (RpcRequest.EDIT_SETTINGS.equals(reqType)) {\r
+                       // update settings on the server\r
+                       if (GitBlit.getBoolean(Keys.web.enableRpcAdministration, false)) {\r
+                               Collection<SettingModel> settings = deserialize(request, response,\r
+                                               RpcUtils.SETTINGS_TYPE);\r
+                               GitBlit.self().updateSettings(settings);\r
+                       } else {\r
+                               response.sendError(notAllowedCode);\r
+                       }\r
                } else if (RpcRequest.LIST_STATUS.equals(reqType)) {\r
                        // return the server's status information\r
                        result = GitBlit.self().getStatus();\r
index 76a673162798e671c34b780805a49f22cbea05b3..66675b16ffdb8d117a4c1246f9155d3fa38b0ea3 100644 (file)
@@ -48,8 +48,10 @@ public class GitblitClient implements Serializable {
        public final String account;\r
 \r
        private final char[] password;\r
+       \r
+       private volatile boolean allowManagement;\r
 \r
-       private volatile boolean isAdmin;\r
+       private volatile boolean allowAdministration;\r
 \r
        private volatile ServerSettings settings;\r
 \r
@@ -74,20 +76,33 @@ public class GitblitClient implements Serializable {
        public void login() throws IOException {\r
                refreshRepositories();\r
 \r
+               try {\r
+                       refreshUsers();\r
+                       allowManagement = true;\r
+               } catch (UnauthorizedException e) {\r
+               } catch (ForbiddenException e) {\r
+               } catch (IOException e) {\r
+                       System.err.println(e.getMessage());\r
+               }\r
+\r
                try {\r
                        settings = RpcUtils.getSettings(url, account, password);\r
                        status = RpcUtils.getStatus(url, account, password);\r
-                       refreshUsers();\r
-                       isAdmin = true;\r
+                       allowAdministration = true;\r
                } catch (UnauthorizedException e) {\r
                } catch (ForbiddenException e) {\r
                } catch (IOException e) {\r
                        System.err.println(e.getMessage());\r
                }\r
+\r
        }\r
 \r
-       public boolean allowAdmin() {\r
-               return isAdmin;\r
+       public boolean allowManagement() {\r
+               return allowManagement;\r
+       }\r
+       \r
+       public boolean allowAdministration() {\r
+               return allowAdministration;\r
        }\r
 \r
        public boolean isOwner(RepositoryModel model) {\r
index 5737c15cac313a91e4fa79226f586c9a5605b8d6..31de44ef45208062b62b5178b09bf2c4ee491542 100644 (file)
@@ -185,7 +185,8 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                repositoriesTable = Utils.newTable(repositoriesModel);\r
                repositoriesTable.setRowHeight(nameRenderer.getFont().getSize() + 8);\r
                repositoriesTable.setRowSorter(defaultRepositoriesSorter);\r
-               repositoriesTable.getRowSorter().toggleSortOrder(RepositoriesTableModel.Columns.Name.ordinal());\r
+               repositoriesTable.getRowSorter().toggleSortOrder(\r
+                               RepositoriesTableModel.Columns.Name.ordinal());\r
 \r
                setRepositoryRenderer(RepositoriesTableModel.Columns.Name, nameRenderer, -1);\r
                setRepositoryRenderer(RepositoriesTableModel.Columns.Indicators, typeRenderer, 100);\r
@@ -208,7 +209,7 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                                        RepositoryModel model = ((RepositoriesTableModel) repositoriesTable.getModel()).list\r
                                                        .get(modelRow);\r
                                        editRepository.setEnabled(singleSelection\r
-                                                       && (gitblit.allowAdmin() || gitblit.isOwner(model)));\r
+                                                       && (gitblit.allowManagement() || gitblit.isOwner(model)));\r
                                } else {\r
                                        editRepository.setEnabled(false);\r
                                }\r
@@ -217,7 +218,7 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
 \r
                repositoriesTable.addMouseListener(new MouseAdapter() {\r
                        public void mouseClicked(MouseEvent e) {\r
-                               if (e.getClickCount() == 2 && gitblit.allowAdmin()) {\r
+                               if (e.getClickCount() == 2 && gitblit.allowManagement()) {\r
                                        editRepository(getSelectedRepositories().get(0));\r
                                }\r
                        }\r
@@ -266,8 +267,8 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                return repositoriesPanel;\r
        }\r
 \r
-       private void setRepositoryRenderer(RepositoriesTableModel.Columns col, TableCellRenderer renderer,\r
-                       int maxWidth) {\r
+       private void setRepositoryRenderer(RepositoriesTableModel.Columns col,\r
+                       TableCellRenderer renderer, int maxWidth) {\r
                String name = repositoriesTable.getColumnName(col.ordinal());\r
                repositoriesTable.getColumn(name).setCellRenderer(renderer);\r
                if (maxWidth > 0) {\r
@@ -457,9 +458,8 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                updateRepositoriesTable();\r
                Utils.packColumns(repositoriesTable, 2);\r
 \r
-               if (gitblit.allowAdmin()) {\r
+               if (gitblit.allowManagement()) {\r
                        updateUsersTable();\r
-                       updateSettingsTable();\r
                } else {\r
                        // user does not have administrator privileges\r
                        // hide admin repository buttons\r
@@ -468,10 +468,23 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                        delRepository.setVisible(false);\r
 \r
                        while (tabs.getTabCount() > 1) {\r
-                               // remove admin tabs\r
+                               // remove all management/administration tabs\r
                                tabs.removeTabAt(1);\r
                        }\r
                }\r
+\r
+               if (gitblit.allowAdministration()) {\r
+                       updateSettingsTable();\r
+               } else {\r
+                       // remove the settings tab\r
+                       String settingsTitle = Translation.get("gb.settings");\r
+                       for (int i= 0; i < tabs.getTabCount(); i++) {\r
+                               if (tabs.getTitleAt(i).equals(settingsTitle)) {\r
+                                       tabs.removeTabAt(i);\r
+                                       break;\r
+                               }                               \r
+                       }\r
+               }\r
        }\r
 \r
        private void updateRepositoriesTable() {\r
@@ -547,7 +560,8 @@ public class GitblitPanel extends JPanel implements CloseTabListener {
                                return false;\r
                        }\r
                };\r
-               TableRowSorter<SettingsTableModel> sorter = new TableRowSorter<SettingsTableModel>(settingsModel);\r
+               TableRowSorter<SettingsTableModel> sorter = new TableRowSorter<SettingsTableModel>(\r
+                               settingsModel);\r
                sorter.setRowFilter(containsFilter);\r
                settingsTable.setRowSorter(sorter);\r
        }\r
index 2d409a0ad5f197f07f7e78d0c2a9ce1c01f45cc0..6572cd9d64bd9e6ebeb6affd87276dc86bad09a7 100644 (file)
@@ -28,8 +28,9 @@ import com.gitblit.models.FederationModel;
 import com.gitblit.models.FederationProposal;\r
 import com.gitblit.models.FederationSet;\r
 import com.gitblit.models.RepositoryModel;\r
-import com.gitblit.models.ServerStatus;\r
 import com.gitblit.models.ServerSettings;\r
+import com.gitblit.models.ServerStatus;\r
+import com.gitblit.models.SettingModel;\r
 import com.gitblit.models.UserModel;\r
 import com.google.gson.reflect.TypeToken;\r
 \r
@@ -44,6 +45,9 @@ public class RpcUtils {
        public static final Type NAMES_TYPE = new TypeToken<Collection<String>>() {\r
        }.getType();\r
 \r
+       public static final Type SETTINGS_TYPE = new TypeToken<Collection<SettingModel>>() {\r
+       }.getType();\r
+\r
        private static final Type REPOSITORIES_TYPE = new TypeToken<Map<String, RepositoryModel>>() {\r
        }.getType();\r
 \r
@@ -344,10 +348,28 @@ public class RpcUtils {
        public static ServerSettings getSettings(String serverUrl, String account, char[] password)\r
                        throws IOException {\r
                String url = asLink(serverUrl, RpcRequest.LIST_SETTINGS);\r
-               ServerSettings settings = JsonUtils.retrieveJson(url, ServerSettings.class, account, password);\r
+               ServerSettings settings = JsonUtils.retrieveJson(url, ServerSettings.class, account,\r
+                               password);\r
                return settings;\r
        }\r
 \r
+       /**\r
+        * Update the settings on the Gitblit server.\r
+        * \r
+        * @param settings\r
+        *            the settings to update\r
+        * @param serverUrl\r
+        * @param account\r
+        * @param password\r
+        * @return true if the action succeeded\r
+        * @throws IOException\r
+        */\r
+       public static boolean updateSettings(Map<String, String> settings, String serverUrl,\r
+                       String account, char[] password) throws IOException {\r
+               return doAction(RpcRequest.EDIT_SETTINGS, null, settings, serverUrl, account, password);\r
+\r
+       }\r
+\r
        /**\r
         * Retrieves the server status object.\r
         * \r