Anonymous avatar Anonymous committed 5ffdef8

add support for group permissions

Comments (0)

Files changed (3)

djangotoolbox/contrib/auth/backends.py

         groups.
         """
         if not hasattr(user_obj, '_group_perm_cache'):
-                  
-            perm_ids = set()
-            for group in user_obj.groups:
-                perm_ids.update(group.permissions.permissions)
-
-            if len(perm_ids) > 0:
-                q = Permission.objects.filter(id__in=perm_ids)
-            else:
-                q = list()
-                
-            perms = list([[perm.content_type.app_label, perm.codename] for perm in q])
-  
+            perm_objs = set([])
+            try:
+                gl = GroupList.objects.get(user=user_obj)
+                group_ids = gl._group_list
+                if len(group_ids) > 0:
+                    group_permissions = set()
+                    for group_id in group_ids:
+                        group_permissions.update(GroupPermissionList.objects.filter(group__id=group_id))
+                    for group_perm in group_permissions:
+                        perm_objs.update(group_perm.permissions)
+                    
+            except GroupList.DoesNotExist:
+                pass
+            
+            perms = list([[perm.content_type.app_label, perm.codename] for perm in perm_objs])
             user_obj._group_perm_cache = set(["%s.%s" % (ct, name) for ct, name in perms])
         return user_obj._group_perm_cache
 
                 pl = UserPermissionList.objects.get(user=user_obj)
                 user_obj._perm_cache = set([u"%s.%s" % (p.content_type.app_label, p.codename) for p in pl.permission_list])
             except UserPermissionList.DoesNotExist:
-                user_obj._perm_cache = list()
+                user_obj._perm_cache = set()
                 pass
-            #user_obj._perm_cache.update(self.get_group_permissions(user_obj))
+            user_obj._perm_cache.update(self.get_group_permissions(user_obj))
         return user_obj._perm_cache
 
     def has_perm(self, user_obj, perm):

djangotoolbox/contrib/auth/models.py

         return self._permissions_cache
     permission_list = property(_get_permissions)
 
+
 class GroupPermissionList(models.Model):
-    user = models.ForeignKey(Group)
+    group = models.ForeignKey(Group)
     _permission_list = ListField(models.ForeignKey(Permission))
 
     def _get_permissions(self):
             setattr(self, '_permissions_cache', permissions)
             
         return self._permissions_cache
-    permission_list = property(_get_permissions)
+    permissions = property(_get_permissions)
 
 class GroupList(models.Model):
     """
     GroupLists are used to map a list of groups to a user
     """
-    group = models.ForeignKey(Group)
-    groups = ListField(models.ForeignKey(Group))
+    user = models.ForeignKey(User)
+    _group_list = ListField(models.ForeignKey(Group))
+
+    def __unicode__(self):
+        return u'%s' %(self.user.username)
+    
+    def _get_group_list(self):
+        if not hasattr(self, '_groups_cache'):
+            group_ids = self._group_list
+            groups = set()
+            if len(group_ids) > 0:
+                # order_by() has to be used to override invalid default Permission filter
+                groups.update(Group.objects.filter(id__in=group_ids))
+            setattr(self, '_groups_cache', groups)
+            
+        return self._groups_cache
+    groups = property(_get_group_list)
+
+    

djangotoolbox/contrib/auth/tests.py

 from django.contrib.contenttypes.models import ContentType
 from django.test import TestCase
 
-from djangotoolbox.contrib.auth.models import UserPermissionList
-from djangotoolbox.contrib.auth.utils import add_permission_to_user
+from djangotoolbox.contrib.auth.models import UserPermissionList, GroupPermissionList, GroupList
+from djangotoolbox.contrib.auth.utils import add_permission_to_user, add_user_to_group, add_permission_to_group
 
 
 class BackendTest(TestCase):
         User.objects.create_user('test', 'test@example.com', 'test')
 
     def test_add_permission_to_user(self):
+        content_type=ContentType.objects.get_for_model(User)
+        perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
+        user = User.objects.get(username='test')
+        self.assertEqual(user.has_perm('auth.test'), False)
+        user = User.objects.get(username='test')
+        add_permission_to_user(perm, user)
+        self.assertEqual(UserPermissionList.objects.count(), 1)
+        pl = UserPermissionList.objects.all()[0]
+        self.assertEqual(pl.permission_list , set([perm]))
+        self.assertEqual(user.has_perm('auth.test'), True)
+
+    def test_add_user_to_group(self):
+        user = User.objects.get(username='test')
+        group = Group.objects.create(name='test_group')
+        add_user_to_group(user, group)
+        self.assertEqual(GroupList.objects.count(), 1)
+        self.assertNotEqual(GroupList.objects.all()[0] , None)
+
+    def test_add_permission_to_group(self):
         content_type=ContentType.objects.get_for_model(Group)
         perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
         user = User.objects.get(username='test')
         self.assertEqual(user.has_perm('auth.test'), False)
         user = User.objects.get(username='test')
-        add_permission_to_user(user, perm)
-        self.assertEqual(UserPermissionList.objects.count(), 1)
-        pl = UserPermissionList.objects.all()[0]
-        self.assertEqual(pl.permission_list , set([perm]))
+        group = Group.objects.create(name='test_group')
+        add_user_to_group(user, group)
+        add_permission_to_group(perm, group)
+        self.assertEqual(GroupPermissionList.objects.count(), 1)
+        gl = GroupPermissionList.objects.all()[0]
+        self.assertEqual(gl.permissions , set([perm]))
         self.assertEqual(user.has_perm('auth.test'), True)
         
     def test_has_perm(self):
-        """
         user = User.objects.get(username='test')
         self.assertEqual(user.has_perm('auth.test'), False)
         user.is_staff = True
         user.is_active = False
         user.save()
         self.assertEqual(user.has_perm('auth.test'), False)
-        """
+        
         
     def test_custom_perms(self):
-        """
+        
         user = User.objects.get(username='test')
-        content_type=ContentType.objects.get_for_model(Group)
+        content_type=ContentType.objects.get_for_model(Permission)
         perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
         # default django way (ManyToManyField)
-        user.user_permissions.add(perm)
+        #user.user_permissions.add(perm)      
+
+        add_permission_to_user(perm, user)
         
-        user.save()
         # reloading user to purge the _perm_cache
         user = User.objects.get(username='test')
         self.assertEqual(user.get_all_permissions() == set([u'auth.test']), True)
         self.assertEqual(user.has_module_perms('auth'), True)
         
         perm = Permission.objects.create(name='test2', content_type=content_type, codename='test2')
+        
+        # default django way (ManyToManyField)
+        #user.user_permissions.add(perm)
 
-        # default django way (ManyToManyField)
-        user.user_permissions.add(perm)
-
-        user.save()
+        add_permission_to_user(perm, user)
+        
         perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3')
 
         # default django  way (ManyToManyField)
-        user.user_permissions.add(perm)
+        #user.user_permissions.add(perm)
 
-        user.save()
+        add_permission_to_user(perm, user)
+
         user = User.objects.get(username='test')
         self.assertEqual(user.get_all_permissions(), set([u'auth.test2', u'auth.test', u'auth.test3']))
         self.assertEqual(user.has_perm('test'), False)
         # default django way (ManyToManyField)
         #group.permissions.add(perm)
 
-        # current djangotoolbox.contrib.auth way
-        group.permissions.permissions.append(perm.id)
-        
-        group.save()
+        add_permission_to_group(perm, group)
 
         # default django way (ManyToManyField)
-        user.groups.add(group)
+        #user.groups.add(group)
 
-        # djangotoolbox.contrib.auth needs save()
-        user.save()
-        """
+        add_user_to_group(user, group)
         
-        """# current djangotoolbox way
-        user.group_list.groups.append(group.id)
-        user.save()
-        """
-
-        """
+        
+        
         user = User.objects.get(username='test')
         exp = set([u'auth.test2', u'auth.test', u'auth.test3', u'auth.test_group'])
         self.assertEqual(user.get_all_permissions(), exp)
         user = AnonymousUser()
         self.assertEqual(user.has_perm('test'), False)
         self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), False)
-        """
+        
         
     def test_has_no_object_perm(self):
         """Regressiontest for #12462"""
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.