]> source.dussan.org Git - gitblit.git/commitdiff
Differentiate between an explicit permission and a regex permission
authorJames Moger <james.moger@gitblit.com>
Mon, 22 Oct 2012 20:15:40 +0000 (16:15 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 22 Oct 2012 20:15:40 +0000 (16:15 -0400)
src/com/gitblit/GitBlit.java
src/com/gitblit/client/GitblitClient.java
src/com/gitblit/client/RegistrantPermissionsPanel.java
src/com/gitblit/client/RegistrantPermissionsTableModel.java
src/com/gitblit/models/RegistrantAccessPermission.java
src/com/gitblit/models/TeamModel.java
src/com/gitblit/models/UserModel.java
src/com/gitblit/wicket/GitBlitWebApp.properties
src/com/gitblit/wicket/panels/RegistrantPermissionsPanel.html
src/com/gitblit/wicket/panels/RegistrantPermissionsPanel.java
tests/com/gitblit/tests/RpcTests.java

index ce556b6eb318b7730e2b7522907c838a0cf0e5cf..e83da933bb9352284bd0446408ed6e85fcfd3a35 100644 (file)
@@ -640,8 +640,10 @@ public class GitBlit implements ServletContextListener {
        public List<RegistrantAccessPermission> getUserAccessPermissions(RepositoryModel repository) {\r
                List<RegistrantAccessPermission> permissions = new ArrayList<RegistrantAccessPermission>();\r
                for (String user : userService.getUsernamesForRepositoryRole(repository.name)) {\r
-                       AccessPermission ap = userService.getUserModel(user).getRepositoryPermission(repository);\r
-                       permissions.add(new RegistrantAccessPermission(user, ap, RegistrantType.USER));\r
+                       UserModel model = userService.getUserModel(user);\r
+                       AccessPermission ap = model.getRepositoryPermission(repository);\r
+                       boolean isExplicit = model.hasExplicitRepositoryPermission(repository.name);\r
+                       permissions.add(new RegistrantAccessPermission(user, ap, isExplicit, RegistrantType.USER));\r
                }\r
                return permissions;\r
        }\r
@@ -656,9 +658,12 @@ public class GitBlit implements ServletContextListener {
        public boolean setUserAccessPermissions(RepositoryModel repository, Collection<RegistrantAccessPermission> permissions) {\r
                List<UserModel> users = new ArrayList<UserModel>();\r
                for (RegistrantAccessPermission up : permissions) {\r
-                       UserModel user = userService.getUserModel(up.registrant);\r
-                       user.setRepositoryPermission(repository.name, up.permission);\r
-                       users.add(user);\r
+                       if (up.isExplicit) {\r
+                               // only set explicitly defined permissions\r
+                               UserModel user = userService.getUserModel(up.registrant);\r
+                               user.setRepositoryPermission(repository.name, up.permission);\r
+                               users.add(user);\r
+                       }\r
                }\r
                return userService.updateUserModels(users);\r
        }\r
@@ -772,8 +777,10 @@ public class GitBlit implements ServletContextListener {
        public List<RegistrantAccessPermission> getTeamAccessPermissions(RepositoryModel repository) {\r
                List<RegistrantAccessPermission> permissions = new ArrayList<RegistrantAccessPermission>();\r
                for (String team : userService.getTeamnamesForRepositoryRole(repository.name)) {\r
-                       AccessPermission ap = userService.getTeamModel(team).getRepositoryPermission(repository);\r
-                       permissions.add(new RegistrantAccessPermission(team, ap, RegistrantType.TEAM));\r
+                       TeamModel model = userService.getTeamModel(team);\r
+                       AccessPermission ap = model.getRepositoryPermission(repository);\r
+                       boolean isExplicit = model.hasExplicitRepositoryPermission(repository.name);\r
+                       permissions.add(new RegistrantAccessPermission(team, ap, isExplicit, RegistrantType.TEAM));\r
                }\r
                return permissions;\r
        }\r
@@ -788,9 +795,12 @@ public class GitBlit implements ServletContextListener {
        public boolean setTeamAccessPermissions(RepositoryModel repository, Collection<RegistrantAccessPermission> permissions) {\r
                List<TeamModel> teams = new ArrayList<TeamModel>();\r
                for (RegistrantAccessPermission tp : permissions) {\r
-                       TeamModel team = userService.getTeamModel(tp.registrant);\r
-                       team.setRepositoryPermission(repository.name, tp.permission);\r
-                       teams.add(team);\r
+                       if (tp.isExplicit) {\r
+                               // only set explicitly defined access permissions\r
+                               TeamModel team = userService.getTeamModel(tp.registrant);\r
+                               team.setRepositoryPermission(repository.name, tp.permission);\r
+                               teams.add(team);\r
+                       }\r
                }\r
                return userService.updateTeamModels(teams);\r
        }\r
index 9e31c794f6419c818f079aedb006e959e404598c..4620fefa6c9e97a9d79127cea413aadd81cd32cf 100644 (file)
@@ -28,6 +28,7 @@ import java.util.Set;
 import java.util.TreeSet;\r
 \r
 import com.gitblit.Constants;\r
+import com.gitblit.Constants.AccessPermission;\r
 import com.gitblit.Constants.AccessRestrictionType;\r
 import com.gitblit.Constants.AuthorizationControl;\r
 import com.gitblit.Constants.RegistrantType;\r
@@ -36,10 +37,10 @@ import com.gitblit.GitBlitException.NotAllowedException;
 import com.gitblit.GitBlitException.UnauthorizedException;\r
 import com.gitblit.GitBlitException.UnknownRequestException;\r
 import com.gitblit.Keys;\r
-import com.gitblit.models.RegistrantAccessPermission;\r
 import com.gitblit.models.FederationModel;\r
 import com.gitblit.models.FeedEntryModel;\r
 import com.gitblit.models.FeedModel;\r
+import com.gitblit.models.RegistrantAccessPermission;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.ServerSettings;\r
 import com.gitblit.models.ServerStatus;\r
@@ -498,7 +499,9 @@ public class GitblitClient implements Serializable {
                List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();\r
                for (UserModel user : allUsers) {\r
                        if (user.hasRepositoryPermission(repository.name)) {\r
-                               list.add(new RegistrantAccessPermission(user.username, user.permissions.get(repository.name), RegistrantType.USER));\r
+                               AccessPermission ap = user.getRepositoryPermission(repository);\r
+                               boolean isExplicit = user.hasExplicitRepositoryPermission(repository.name);\r
+                               list.add(new RegistrantAccessPermission(user.username, ap, isExplicit, RegistrantType.USER));\r
                        }\r
                }\r
                return list;\r
@@ -535,7 +538,9 @@ public class GitblitClient implements Serializable {
                List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();\r
                for (TeamModel team : allTeams) {\r
                        if (team.hasRepositoryPermission(repository.name)) {\r
-                               list.add(new RegistrantAccessPermission(team.name, team.permissions.get(repository.name), RegistrantType.TEAM));\r
+                               AccessPermission ap = team.getRepositoryPermission(repository);\r
+                               boolean isExplicit = team.hasExplicitRepositoryPermission(repository.name);\r
+                               list.add(new RegistrantAccessPermission(team.name, ap, isExplicit, RegistrantType.TEAM));\r
                        }\r
                }\r
                return list;\r
index fa7ff5a807690adf204dc4f68308ad1ecf77ed40..4ea173fc3ff2e5ba6a4827ce6f261f5bd33a4dab 100644 (file)
@@ -29,6 +29,8 @@ import javax.swing.JComboBox;
 import javax.swing.JPanel;\r
 import javax.swing.JScrollPane;\r
 import javax.swing.JTable;\r
+import javax.swing.SwingConstants;\r
+import javax.swing.table.DefaultTableCellRenderer;\r
 \r
 import com.gitblit.Constants.AccessPermission;\r
 import com.gitblit.models.RegistrantAccessPermission;\r
@@ -59,8 +61,10 @@ public class RegistrantPermissionsPanel extends JPanel {
                JScrollPane jsp = new JScrollPane(permissionsTable);\r
                add(jsp, BorderLayout.CENTER);\r
                \r
+               permissionsTable.getColumnModel().getColumn(RegistrantPermissionsTableModel.Columns.Type.ordinal())\r
+                               .setCellRenderer(new RegexRenderer());\r
                permissionsTable.getColumnModel().getColumn(RegistrantPermissionsTableModel.Columns.Permission.ordinal())\r
-                               .setCellEditor(new AccessPermissionEditor());\r
+               .setCellEditor(new AccessPermissionEditor());\r
                \r
                registrantModel = new DefaultComboBoxModel();\r
                registrantSelector = new JComboBox(registrantModel);\r
@@ -137,4 +141,28 @@ public class RegistrantPermissionsPanel extends JPanel {
                super(new JComboBox(AccessPermission.values()));\r
            }\r
        }\r
+       \r
+       private class RegexRenderer extends DefaultTableCellRenderer {\r
+\r
+               private static final long serialVersionUID = 1L;\r
+\r
+               public RegexRenderer() {\r
+                       super();\r
+                       setHorizontalAlignment(SwingConstants.CENTER);\r
+               }\r
+\r
+               @Override\r
+               protected void setValue(Object value) {\r
+                       boolean isExplicit = (Boolean) value;\r
+                       if (isExplicit) {\r
+                               // explicit permission\r
+                               setText("");\r
+                               setToolTipText(null);\r
+                       } else {\r
+                               // regex matched permission\r
+                               setText("regex");\r
+                               setToolTipText(Translation.get("gb.regexPermission"));\r
+                       }\r
+               }\r
+       }\r
 }\r
index 91acec8b95858837e5c5cb3d4d401b1fce5edfaa..fcd9c8b463402691b70d6b5869c28e4b3308dc22 100644 (file)
@@ -36,7 +36,7 @@ public class RegistrantPermissionsTableModel extends AbstractTableModel {
        List<RegistrantAccessPermission> permissions;\r
 \r
        enum Columns {\r
-               Registrant, Permission;\r
+               Registrant, Type, Permission;\r
 \r
                @Override\r
                public String toString() {\r
@@ -72,6 +72,8 @@ public class RegistrantPermissionsTableModel extends AbstractTableModel {
                switch (col) {\r
                case Registrant:\r
                        return Translation.get("gb.name");\r
+               case Type:\r
+                       return Translation.get("gb.type");\r
                case Permission:\r
                        return Translation.get("gb.permission");\r
                }\r
@@ -88,13 +90,23 @@ public class RegistrantPermissionsTableModel extends AbstractTableModel {
        public Class<?> getColumnClass(int columnIndex) {\r
                if (columnIndex == Columns.Permission.ordinal()) {\r
                        return AccessPermission.class;\r
+               } else if (columnIndex == Columns.Type.ordinal()) {\r
+                       return Boolean.class;\r
                }\r
                return String.class;\r
        }\r
        \r
        @Override\r
        public boolean isCellEditable(int rowIndex, int columnIndex) {\r
-               return columnIndex == Columns.Permission.ordinal();\r
+               if (columnIndex == Columns.Permission.ordinal()) {\r
+                       // in order for the permission to be editable it must be\r
+                       // explicitly defined on the object.  regex permissions are inherited\r
+                       // and therefore can not be directly manipulated unless the current\r
+                       // object is the source of the regex (i.e. a user or team with explicit\r
+                       // regex definition)\r
+                       return permissions.get(rowIndex).isExplicit;\r
+               }\r
+               return false;\r
        }\r
 \r
        @Override\r
@@ -104,6 +116,8 @@ public class RegistrantPermissionsTableModel extends AbstractTableModel {
                switch (col) {\r
                case Registrant:\r
                        return rp.registrant;\r
+               case Type:\r
+                       return rp.isExplicit;\r
                case Permission:\r
                        return rp.permission;\r
                }\r
index 93027450e4c50350c1714c84d716bd3097426962..4a560d43472adfcac4a6468d9ff23d85f71883ed 100644 (file)
@@ -33,13 +33,16 @@ public class RegistrantAccessPermission implements Serializable, Comparable<Regi
        public String registrant;
        public AccessPermission permission;
        public RegistrantType type;
+       public boolean isExplicit;
 
        public RegistrantAccessPermission() {
+               isExplicit = true;
        }
        
-       public RegistrantAccessPermission(String registrant, AccessPermission permission, RegistrantType type) {
+       public RegistrantAccessPermission(String registrant, AccessPermission permission, boolean isExplicit, RegistrantType type) {
                this.registrant = registrant;
                this.permission = permission;
+               this.isExplicit = isExplicit;
                this.type = type;
        }
        
index 4c3a842799bc07286aa40f45177387a812f22586..9ba2f669ae5d064d08f527609400c5444830c745 100644 (file)
@@ -97,7 +97,7 @@ public class TeamModel implements Serializable, Comparable<TeamModel> {
        public List<RegistrantAccessPermission> getRepositoryPermissions() {\r
                List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();\r
                for (Map.Entry<String, AccessPermission> entry : permissions.entrySet()) {\r
-                       list.add(new RegistrantAccessPermission(entry.getKey(), entry.getValue(), RegistrantType.REPOSITORY));\r
+                       list.add(new RegistrantAccessPermission(entry.getKey(), entry.getValue(), true, RegistrantType.REPOSITORY));\r
                }\r
                Collections.sort(list);\r
                return list;\r
@@ -129,6 +129,18 @@ public class TeamModel implements Serializable, Comparable<TeamModel> {
                return false;\r
        }\r
        \r
+       /**\r
+        * Returns true if the team has an explicitly specified access permission for\r
+        * this repository.\r
+        * \r
+        * @param name\r
+        * @return if the team has an explicitly specified access permission\r
+        */\r
+       public boolean hasExplicitRepositoryPermission(String name) {\r
+               String repository = AccessPermission.repositoryFromRole(name).toLowerCase();\r
+               return permissions.containsKey(repository);\r
+       }\r
+       \r
        /**\r
         * Adds a repository permission to the team.\r
         * <p>\r
index 773370435e153768e5b612b8972af3a08213d5b0..97430bfe11772ca4fb331c3abd435bf2bc2b97ce 100644 (file)
@@ -137,7 +137,7 @@ public class UserModel implements Principal, Serializable, Comparable<UserModel>
        public List<RegistrantAccessPermission> getRepositoryPermissions() {\r
                List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();\r
                for (Map.Entry<String, AccessPermission> entry : permissions.entrySet()) {\r
-                       list.add(new RegistrantAccessPermission(entry.getKey(), entry.getValue(), RegistrantType.REPOSITORY));\r
+                       list.add(new RegistrantAccessPermission(entry.getKey(), entry.getValue(), true, RegistrantType.REPOSITORY));\r
                }\r
                Collections.sort(list);\r
                return list;\r
@@ -169,6 +169,18 @@ public class UserModel implements Principal, Serializable, Comparable<UserModel>
                return false;\r
        }\r
        \r
+       /**\r
+        * Returns true if the user has an explicitly specified access permission for\r
+        * this repository.\r
+        * \r
+        * @param name\r
+        * @return if the user has an explicitly specified access permission\r
+        */\r
+       public boolean hasExplicitRepositoryPermission(String name) {\r
+               String repository = AccessPermission.repositoryFromRole(name).toLowerCase();\r
+               return permissions.containsKey(repository);\r
+       }\r
+       \r
        /**\r
         * Adds a repository permission to the team.\r
         * <p>\r
index bdeb4a958416e1535bab15a96d49edd35a669925..41cbdd462a7af1e4554f1526f7ed81e1ffc35e12 100644 (file)
@@ -356,3 +356,4 @@ gb.createPermission = {0} (push, ref creation)
 gb.deletePermission = {0} (push, ref creation+deletion)\r
 gb.rewindPermission = {0} (push, ref creation+deletion+rewind)\r
 gb.permission = permission\r
+gb.regexPermission = this permission is set from a regular expression
\ No newline at end of file
index dd76d9fded22076e0ff454a62080d0137e19575a..f5da1c6f12406d406582863a868da7e89c5f45d3 100644 (file)
@@ -8,8 +8,8 @@
 <wicket:panel>\r
 \r
        <div wicket:id="permissionRow">\r
-               <div style="padding-top:10px" class="row-fluid">\r
-                       <span class="span8" wicket:id="registrant"></span> <select class="input-medium" wicket:id="permission"></select>\r
+               <div style="padding-top:10px;border-left:1px solid #ccc;border-right:1px solid #ccc;" class="row-fluid">\r
+                       <div style="padding-top:5px;padding-left:5px;" class="span6" wicket:id="registrant"></div><div style="padding-top:5px;" class="span2"><span class="label label-info" wicket:id="regex">[regex]</span></div> <select class="input-medium" wicket:id="permission"></select>\r
                </div>\r
        </div>\r
 \r
index 936659d2fd47ce89dc3fbde3a52cf6719f1e4a26..9dee2f2674b81cd8de19c89783924e2536d7d45d 100644 (file)
@@ -38,6 +38,7 @@ import org.apache.wicket.model.IModel;
 import com.gitblit.Constants.AccessPermission;
 import com.gitblit.models.RegistrantAccessPermission;
 import com.gitblit.utils.DeepCopier;
+import com.gitblit.wicket.WicketUtils;
 
 /**
  * Allows user to manipulate registrant access permissions.
@@ -78,20 +79,33 @@ public class RegistrantPermissionsPanel extends BasePanel {
                        public void populateItem(final Item<RegistrantAccessPermission> item) {
                                final RegistrantAccessPermission entry = item.getModelObject();
                                item.add(new Label("registrant", entry.registrant));
+                               if (entry.isExplicit) {
+                                       item.add(new Label("regex", "").setVisible(false));
+                               } else {
+                                       Label regex = new Label("regex", "regex");
+                                       WicketUtils.setHtmlTooltip(regex, getString("gb.regexPermission"));
+                                       item.add(regex);
+                               }
 
                                // use ajax to get immediate update of permission level change
                                // otherwise we can lose it if they change levels and then add
                                // a new repository permission
                                final DropDownChoice<AccessPermission> permissionChoice = new DropDownChoice<AccessPermission>(
                                                "permission", Arrays.asList(AccessPermission.values()), new AccessPermissionRenderer(translations));
-                               permissionChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
+                               // only allow changing an explicitly defined permission
+                               // this is designed to prevent changing a regex permission in
+                               // a repository
+                               permissionChoice.setEnabled(entry.isExplicit);
+                               if (entry.isExplicit) {
+                                       permissionChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
                           
-                                       private static final long serialVersionUID = 1L;
+                                               private static final long serialVersionUID = 1L;
 
-                                       protected void onUpdate(AjaxRequestTarget target) {
-                               target.addComponent(permissionChoice);
-                           }
-                       });
+                                               protected void onUpdate(AjaxRequestTarget target) {
+                                                       target.addComponent(permissionChoice);
+                                               }
+                                       });
+                               }
 
                                item.add(permissionChoice);
                        }
index c739eba3038d8e4738a19b30b22dbfd58b1350e3..0be2e02a488e2ddc4e49c2335add5d0520aac01f 100644 (file)
@@ -199,7 +199,7 @@ public class RpcTests {
                List<RegistrantAccessPermission> permissions = RpcUtils.getRepositoryMemberPermissions(retrievedRepository, url, account,\r
                                password.toCharArray());\r
                assertEquals("Membership permissions is not empty!", 0, permissions.size());\r
-               permissions.add(new RegistrantAccessPermission(testMember.username, AccessPermission.PUSH, RegistrantType.USER));\r
+               permissions.add(new RegistrantAccessPermission(testMember.username, AccessPermission.PUSH, true, RegistrantType.USER));\r
                assertTrue(\r
                                "Failed to set member permissions!",\r
                                RpcUtils.setRepositoryMemberPermissions(retrievedRepository, permissions, url, account,\r
@@ -288,7 +288,7 @@ public class RpcTests {
                // set no teams\r
                List<RegistrantAccessPermission> permissions = new ArrayList<RegistrantAccessPermission>();\r
                for (String team : helloworldTeams) {\r
-                       permissions.add(new RegistrantAccessPermission(team, AccessPermission.NONE, RegistrantType.TEAM));\r
+                       permissions.add(new RegistrantAccessPermission(team, AccessPermission.NONE, true, RegistrantType.TEAM));\r
                }\r
                assertTrue(RpcUtils.setRepositoryTeamPermissions(helloworld, permissions, url, account,\r
                                password.toCharArray()));\r