]> source.dussan.org Git - gitblit.git/commitdiff
Add regex permission matching to hasRepositoryPermission check (issue 36)
authorJames Moger <james.moger@gitblit.com>
Wed, 10 Oct 2012 21:13:18 +0000 (17:13 -0400)
committerJames Moger <james.moger@gitblit.com>
Wed, 10 Oct 2012 21:13:18 +0000 (17:13 -0400)
src/com/gitblit/models/TeamModel.java
src/com/gitblit/models/UserModel.java
tests/com/gitblit/tests/PermissionsTest.java

index 0b9c5069a3c94e4f081f882aac4691273a0650ca..896adfe66a2f4c888997b334e5d5b93a395a868d 100644 (file)
@@ -92,7 +92,21 @@ public class TeamModel implements Serializable, Comparable<TeamModel> {
         */\r
        public boolean hasRepositoryPermission(String name) {\r
                String repository = AccessPermission.repositoryFromRole(name).toLowerCase();\r
-               return permissions.containsKey(repository) || repositories.contains(repository);\r
+               if (permissions.containsKey(repository)) {\r
+                       // exact repository permission specified\r
+                       return true;\r
+               } else {\r
+                       // search for regex permission match\r
+                       for (String key : permissions.keySet()) {\r
+                               if (name.matches(key)) {\r
+                                       AccessPermission p = permissions.get(key);\r
+                                       if (p != null) {\r
+                                               return true;\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+               return false;\r
        }\r
        \r
        /**\r
index a4a402454e32188c4fe2ac91b1ceb9db6079620e..d8c2abe3af52ef66b003ca4776abfdcbadbd42c2 100644 (file)
@@ -132,7 +132,21 @@ public class UserModel implements Principal, Serializable, Comparable<UserModel>
         */\r
        public boolean hasRepositoryPermission(String name) {\r
                String repository = AccessPermission.repositoryFromRole(name).toLowerCase();\r
-               return permissions.containsKey(repository) || repositories.contains(repository);\r
+               if (permissions.containsKey(repository)) {\r
+                       // exact repository permission specified\r
+                       return true;\r
+               } else {\r
+                       // search for regex permission match\r
+                       for (String key : permissions.keySet()) {\r
+                               if (name.matches(key)) {\r
+                                       AccessPermission p = permissions.get(key);\r
+                                       if (p != null) {\r
+                                               return true;\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+               return false;\r
        }\r
        \r
        /**\r
index 83ab11e14db3eca23f2cf91845c382979cc15d06..41ff5a638aa9f2b48ee18f66cdaaa4ed677b2be0 100644 (file)
@@ -2329,6 +2329,7 @@ public class PermissionsTest extends Assert {
                UserModel user = new UserModel("test");
                repository.owner = user.username;
 
+               assertFalse("user SHOULD NOT HAVE a repository permission!", user.hasRepositoryPermission(repository.name));
                assertTrue("owner CAN NOT view!", user.canView(repository));
                assertTrue("owner CAN NOT clone!", user.canClone(repository));
                assertTrue("owner CAN NOT push!", user.canPush(repository));
@@ -2352,6 +2353,7 @@ public class PermissionsTest extends Assert {
                UserModel user = new UserModel("test");
                repository.owner = user.username;
 
+               assertFalse("user SHOULD NOT HAVE a repository permission!", user.hasRepositoryPermission(repository.name));
                assertTrue("user CAN NOT view!", user.canView(repository));
                assertTrue("user CAN NOT clone!", user.canClone(repository));
                assertTrue("user CAN NOT push!", user.canPush(repository));
@@ -2375,6 +2377,7 @@ public class PermissionsTest extends Assert {
                UserModel user = new UserModel("visitor");
                repository.owner = "test";
 
+               assertFalse("user HAS a repository permission!", user.hasRepositoryPermission(repository.name));
                assertFalse("user CAN view!", user.canView(repository));
                assertFalse("user CAN clone!", user.canClone(repository));
                assertFalse("user CAN push!", user.canPush(repository));
@@ -2398,6 +2401,7 @@ public class PermissionsTest extends Assert {
                UserModel user = new UserModel("test");
                user.setRepositoryPermission("ubercool/[A-Za-z0-9-~_\\./]+", AccessPermission.CLONE);
 
+               assertTrue("user DOES NOT HAVE a repository permission!", user.hasRepositoryPermission(repository.name));
                assertTrue("user CAN NOT view!", user.canView(repository));
                assertTrue("user CAN NOT clone!", user.canClone(repository));
                assertFalse("user CAN push!", user.canPush(repository));