Commits

Kai Diefenbach committed a686c0f

Bugfix: prevent to add same Users several times to a Role; issue #6 of django-worlflows: https://bitbucket.org/diefenbach/django-workflows/issue/6/multiple-identical-users-on-a-single-role

  • Participants
  • Parent commits 7fbb292

Comments (0)

Files changed (2)

permissions/models.py

 from django.contrib.contenttypes.models import ContentType
 from django.utils.translation import ugettext_lazy as _
 
+# permissions imports
+import permissions.utils
+
 class Permission(models.Model):
     """A permission which can be granted to users/groups and objects.
 
     def add_principal(self, principal, content=None):
         """Addes the given principal (user or group) ot the Role.
         """
-        if isinstance(principal, User):
-            PrincipalRoleRelation.objects.create(user=principal, role=self)
-        else:
-            PrincipalRoleRelation.objects.create(group=principal, role=self)
+        return permissions.utils.add_role(principal, self)
 
     def get_groups(self, content=None):
         """Returns all groups which has this role assigned. If content is given

permissions/tests.py

         self.assertEqual(result[0].username, "john")
         self.assertEqual(result[1].username, "jane")
         self.assertEqual(len(result), 2)
+        
+    def test_get_users_3(self):
+        """
+        """
+        self.role_1.add_principal(self.user)
+        result = self.role_1.get_users()
+        self.assertEqual(result, [self.user])
+        
+        # Add same user again
+        self.role_1.add_principal(self.user)
+        result = self.role_1.get_users()
+        self.assertEqual(result, [self.user])
 
 class PermissionTestCase(TestCase):
     """