Commits

Anonymous committed 431b062

user and group permission strings are stored in UserPermissionList

Comments (0)

Files changed (4)

djangotoolbox/auth/backends.py

     """
     supports_object_permissions = False
     supports_anonymous_user = True
-
-    def get_group_permissions(self, user_obj):
+    
+    def get_group_permissions(self, user_obj, user_perm_obj=None):
         """
         Returns a set of permission strings that this user has through his/her
         groups.
         """
         if not hasattr(user_obj, '_group_perm_cache'):
             perms = set([])
-            try:
-                gl = GroupList.objects.get(user=user_obj)
-                group_ids = gl.fk_list
-                if len(group_ids) > 0:
-                    group_permissions = set()
-                    group_permissions.update(GroupPermissionList.objects.filter(group__id__in=gl.fk_list))
-                    for group_perm in group_permissions:
-                        perms.update(group_perm.permission_list)
-                    
-            except GroupList.DoesNotExist:
-                pass
-            
+            if user_perm_obj is None:
+                try:
+                    pl = UserPermissionList.objects.get(user=user_obj)
+                    perms = pl.group_permission_list
+                except UserPermissionList.DoesNotExist:
+                    pass
+            else:
+                perms = user_perm_obj.group_permission_list
+
             user_obj._group_perm_cache = perms
         return user_obj._group_perm_cache
-
+    
     def get_all_permissions(self, user_obj):
         if user_obj.is_anonymous():
             return set()
             try:
                 pl = UserPermissionList.objects.get(user=user_obj)
                 user_obj._perm_cache = set(pl.permission_list)
+                
             except UserPermissionList.DoesNotExist:
+                pl = None
                 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, pl))
         return user_obj._perm_cache
 
     def has_perm(self, user_obj, perm):

djangotoolbox/auth/models.py

 class UserPermissionList(models.Model):
     user = models.ForeignKey(User)
     permission_list = ListField(models.CharField(max_length=128))
+    group_permission_list = ListField(models.CharField(max_length=128))
     fk_list = ListField(models.PositiveIntegerField())
 
 class GroupPermissionList(models.Model):
     """
     user = models.ForeignKey(User)
     fk_list = ListField(models.ForeignKey(Group))
-
+    
     def __unicode__(self):
         return u'%s' %(self.user.username)
     

djangotoolbox/auth/tests.py

         self.assertEqual(user.has_perm('auth.test2'), True)
         self.assertEqual(user.has_perm('auth.test'), True)
         self.assertEqual(user.has_perm('auth.test23x'), False)
+
+
+        user = User.objects.get(username='test')
+        pl = UserPermissionList.objects.all()[0]
+        update_permissions_user([perm], user)
+        self.assertEqual(user.has_perm('auth.test1'), False)
+        self.assertEqual(user.has_perm('auth.test2'), False)
+        self.assertEqual(user.has_perm('auth.test'), True)
+        self.assertEqual(user.has_perm('auth.test23x'), False)
         
         # remove all permissions
         user = User.objects.get(username='test')
         self.assertEqual(user.has_perm('auth.test'), True)
         self.assertEqual(user.has_perm('auth.test2312'), False)
         
-    
+        group1= Group.objects.create(name='test_group1')
+        perm1 = Permission.objects.create(name='test1',
+                                         content_type=content_type,
+                                         codename='test1')
+
+        add_user_to_group(user, group1)
+        update_permissions_group([perm1], group1)
+        user = User.objects.get(username='test')
+        self.assertEqual(user.has_perm('auth.test'), True)
+        self.assertEqual(user.has_perm('auth.test1'), True)
+
+        update_permissions_group([], group1)
+        group_list = GroupList.objects.filter(fk_list=group1.id)
+        user = User.objects.get(username='test')
+        self.assertEqual(user.has_perm('auth.test'), True)
+        self.assertEqual(user.has_perm('auth.test1'), False)
+
+        update_user_groups(user, [])
+        user = User.objects.get(username='test')
+        self.assertEqual(user.has_perm('auth.test'), False)
+        self.assertEqual(user.has_perm('auth.test1'), False)
+        
     def test_has_perm(self):
         user = User.objects.get(username='test')
         self.assertEqual(user.has_perm('auth.test'), False)

djangotoolbox/auth/utils.py

 from djangotoolbox.auth.models import UserPermissionList, GroupPermissionList, GroupList
 
+def update_user_group_permissions(obj_list):
+    group_ids = obj_list.fk_list
+    perms = set()
+    if len(group_ids) > 0:
+        group_permissions = set()
+        group_permissions.update(GroupPermissionList.objects.filter(group__id__in=group_ids))
+        for group_perm in group_permissions:
+            perms.update(group_perm.permission_list)
+        
+    
+    user_perm, created = UserPermissionList.objects.get_or_create(user=obj_list.user)
+    user_perm.group_permission_list = list(perms)
+    user_perm.save()
 
 def add_perm_to(obj, list_cls, filter):
     obj_list, created = list_cls.objects.get_or_create(**filter)
     obj_list, created = GroupList.objects.get_or_create(user=user)
 
     obj_list.fk_list.append(group.id)
+    
     obj_list.save()
-        
+    update_user_group_permissions(obj_list)
+    
 def add_permission_to_group(perm, group):
     add_perm_to(perm, GroupPermissionList, {'group': group})
+    group_list = GroupList.objects.filter(fk_list=group.id)
+
+    for gl in group_list:
+        update_user_group_permissions(gl)
 
 def update_list(perm_objs, list_cls, filter):
     list_obj, created = list_cls.objects.get_or_create(**filter)
 
-    old_perms = list_obj.permission_list
+    from copy import copy
+    old_perms = copy(list_obj.permission_list)
 
-    perm_strs = [['%s.%s' % (perm.content_type.app_label, perm.codename), perm.id] for perm in perm_objs]
-
+    perm_strs = ['%s.%s' % (perm.content_type.app_label, perm.codename) for perm in perm_objs]
+    perm_ids = [perm.id for perm in perm_objs]
+    
     for perm in old_perms:
         try: 
             perm_strs.index(perm)
             list_obj.permission_list.pop(i)
             list_obj.fk_list.pop(i)
 
+    i = 0    
     for perm in perm_strs:
         try:
-            old_perms.index(perm[0])
+            old_perms.index(perm)
         except ValueError:
-            list_obj.permission_list.append(perm[0])
-            list_obj.fk_list.append(perm[1])
-            
+            list_obj.permission_list.append(perm)
+            list_obj.fk_list.append(perm_ids[i])
+        i += 1
+    
     if len(perm_strs) == 0:
         list_obj.permission_list = []
         list_obj.fk_list = []
-        
+
     list_obj.save()
     
 def update_permissions_user(perms, user):
 def update_permissions_group(perms, group):
     update_list(perms, GroupPermissionList, {'group': group})
 
+    group_list = GroupList.objects.filter(fk_list=group.id)
+    
+    for gl in group_list:
+        update_user_group_permissions(gl)
+
 def update_user_groups(user, group):
     objs = group
     obj_list, created = GroupList.objects.get_or_create(user=user)
             obj_list.fk_list.append(obj.id)
     
     obj_list.save()
-
+    
+    update_user_group_permissions(obj_list)