Commits

Anonymous committed d011cb4

move group fk list from GroupList to UserPermissionList

Comments (0)

Files changed (4)

djangotoolbox/auth/backends.py

 from django.contrib.auth.backends import ModelBackend
 from django.contrib.auth.models import User
-from djangotoolbox.auth.models import UserPermissionList
+from djangotoolbox.auth.models import UserPermissionList, GroupPermissionList
 
 
 class NonrelPermissionBackend(ModelBackend):
         if not hasattr(user_obj, '_group_perm_cache'):
             perms = set([])
             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_perm_obj, created = UserPermissionList.objects.get_or_create(user=user_obj)
+            
+            group_perm_lists = GroupPermissionList.objects.filter(group__id__in=user_perm_obj.group_fk_list)
+            
+            for group_perm_list in group_perm_lists:
+                perms.update(group_perm_list.permission_list)
+                
             user_obj._group_perm_cache = perms
         return user_obj._group_perm_cache
     

djangotoolbox/auth/models.py

         objs.update(obj_cls .objects.filter(id__in=obj_ids).order_by('name'))
     return objs
 
+
 class UserPermissionList(models.Model):
     user = models.ForeignKey(User)
+    
     permission_list = ListField(models.CharField(max_length=128))
+    permission_fk_list = ListField(models.CharField(max_length=32))
+
     group_permission_list = ListField(models.CharField(max_length=128))
-    fk_list = ListField(models.CharField(max_length=32))
-    
+    group_fk_list = ListField(models.CharField(max_length=32))
+
+    def _get_objs(self):
+        return get_objs(Group, self.group_fk_list)
+
+
 class GroupPermissionList(models.Model):
     group = models.ForeignKey(Group)
     permission_list = ListField(models.CharField(max_length=128))
-    fk_list = ListField(models.CharField(max_length=32))
-
-
-class GroupList(models.Model):
-    """
-    GroupLists are used to map a list of groups to a user
-    """
-    user = models.ForeignKey(User)
-    #fk_list = ListField(models.CharField(max_length=32))
-    fk_list = ListField(models.ForeignKey(Group))
-    
-    def __unicode__(self):
-        return u'%s' %(self.user.username)
-    
-    def _get_objs(self):
-        if not hasattr(self, '_groups_cache'):
-            setattr(self, '_groups_cache', get_objs(Group, self.fk_list))
-        return self._groups_cache
-    groups = property(_get_objs)
+    permission_fk_list = ListField(models.CharField(max_length=32))

djangotoolbox/auth/tests.py

 from django.contrib.contenttypes.models import ContentType
 from django.test import TestCase
 from djangotoolbox.auth.models import UserPermissionList, \
-     GroupPermissionList, GroupList
+     GroupPermissionList
 from djangotoolbox.auth.utils import add_permission_to_user, \
      add_user_to_group, add_permission_to_group, update_permissions_user, update_user_groups, update_permissions_group
 
         user = User.objects.get(username='test')
         group = Group.objects.create(name='test_group')
         update_user_groups(user, [group])
-        self.assertEqual(GroupList.objects.count(), 1)
-        self.assertNotEqual(GroupList.objects.all()[0] , None)
+        self.assertEqual(UserPermissionList.objects.count(), 1)
+        self.assertNotEqual(UserPermissionList.objects.all()[0] , None)
         
     
     def test_update_permissions_group(self):
         self.assertEqual(user.has_perm('auth.test1'), True)
 
         update_permissions_group([], group1)
-        group_list = GroupList.objects.filter(fk_list=group1.id)
+        group_list = UserPermissionList.objects.filter(group_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)

djangotoolbox/auth/utils.py

 from copy import copy
 
-from djangotoolbox.auth.models import UserPermissionList, GroupPermissionList,\
-     GroupList
+from djangotoolbox.auth.models import UserPermissionList, GroupPermissionList
 
 
 def update_user_group_permissions(group_list):
     permissions of a group are modified and everytime a user joins
     or leaves a group
     """
-    
+
+    return True
     perms = set()
-    if len(group_list.fk_list) > 0:
+    if len(group_list.group_fk_list) > 0:
         group_permissions = set()
-        group_permissions.update(GroupPermissionList.objects.filter(group__id__in=group_list.fk_list))
+        group_permissions.update(GroupPermissionList.objects.filter(group__id__in=group_list.group_fk_list))
         for group_perm in group_permissions:
             perms.update(group_perm.permission_list)
         
     add_perm_to(perm, UserPermissionList,  {'user': user }) 
 
 def add_user_to_group(user, group):
-    obj_list, created = GroupList.objects.get_or_create(user=user)
-    obj_list.fk_list.append(group.id)
+    obj_list, created = UserPermissionList.objects.get_or_create(user=user)
+    obj_list.group_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)
+    group_list = UserPermissionList.objects.filter(group_fk_list=group.id)
     for gl in group_list:
         update_user_group_permissions(gl)
 
         except ValueError:
             i = list_obj.permission_list.index(perm)
             list_obj.permission_list.pop(i)
-            list_obj.fk_list.pop(i)
+            list_obj.permission_fk_list.pop(i)
 
     i = 0    
     for perm in perm_strs:
             old_perms.index(perm)
         except ValueError:
             list_obj.permission_list.append(perm)
-            list_obj.fk_list.append(perm_ids[i])
+            list_obj.permission_fk_list.append(perm_ids[i])
         i += 1
 
     list_obj.save()
 def update_permissions_group(perms, group):
     update_list(perms, GroupPermissionList, {'group': group})
 
-    group_list = GroupList.objects.filter(fk_list=group.id)
+    group_list = UserPermissionList.objects.filter(group_fk_list=group.id)
     for gl in group_list:
         update_user_group_permissions(gl)
 
 def update_user_groups(user, groups):
-    objs = groups
-    obj_list, created = GroupList.objects.get_or_create(user=user)
-    old_objs = list(obj_list._get_objs())
+    new_group_ids = [ group.id for group in groups]
+    pl, created = UserPermissionList.objects.get_or_create(user=user)
+    old_group_ids = copy(pl.group_fk_list)
     
-    for obj in old_objs:
+    for group_id in old_group_ids:
         try:
-            objs.index(obj)
+            new_group_ids.index(group_id)
         except ValueError:
-            obj_list.fk_list.remove(obj.id)
+            pl.group_fk_list.remove(group_id)
     
-    for obj in objs:
+    for group_id in new_group_ids:
         try:
-            old_objs.index(obj)
+            old_group_ids.index(group_id)
         except ValueError:
-            obj_list.fk_list.append(obj.id)
+            pl.group_fk_list.append(group_id)
     
-    obj_list.save()
+    pl.save()
     
-    update_user_group_permissions(obj_list)
+    update_user_group_permissions(pl)