Commits

Kai Diefenbach committed 81e9a04

Add regisration of groups

  • Participants
  • Parent commits 31ce2d0

Comments (0)

Files changed (4)

File docs/api.rst

   .. autofunction:: register_permission
   .. autofunction:: unregister_permission
 
+Register roles
+^^^^^^^^^^^^^^
+
+  .. autofunction:: register_role
+  .. autofunction:: unregister_role
+
 Register groups
 ^^^^^^^^^^^^^^^
 

File docs/overview.rst

 Overview
 ========
 
+* django-permissions is a generic framework for per-object permissions for
+  Django.
+
 Permissions
 ===========
-Permissions are granted to roles in order to allow the roles something.
+* Permissions are granted to roles (and only to roles) in order to allow 
+  something to users or groups which have these roles.
 
 Roles
 =====
-Roles are used to grant permissions. Typical roles are *Manager* or *Editor*.
+* Roles are used to grant permissions. Typical roles are *Reader*, *Manager*  
+  or *Editor*.
 
-Users and Groups
-================
+Local Roles
+===========
+* Local roles are roles which are assigned to users and groups for a specific 
+  content objects.
+
+Users
+=====
+* Users are actors which may need a permission to do something within the 
+  system.
 * Users can be member of several groups.
-* User and groups can have roles.
-* User and groups can have local roles, that is roles for a specific object.
-* Users "inherit" all roles of their groups.
-* Users "inherit" all permissions of their roles
+* User can have several roles, directly or via a membership to a group
+  (these are considered as global).
+* User can have local roles, directly or via a membership to a group. That is
+  roles for a specific object.
+* Users have all roles of their groups - global and local ones.
+* Users have all permissions of their roles - global and local ones.
+
+Groups
+======
+* Groups combines users together.
+* Groups can have roles (these are considered as global).
+* Groups can have local roles, that is roles for a specific object.
+* Groups has all permissions of their roles - global and local ones.
+* Users of a Group have the group's roles and permissions.

File permissions/tests.py

 class RegistrationTestCase(TestCase):
     """Tests the registration of different components.
     """
+    def test_group(self):
+        """Tests registering/unregistering of a group.
+        """
+        # Register a group
+        result = permissions.utils.register_group("Brights")
+        self.failUnless(isinstance(result, Group))
+
+        # It's there
+        group = Group.objects.get(name="Brights")
+        self.assertEqual(group.name, "Brights")
+
+        # Trying to register another group with same name
+        result = permissions.utils.register_group("Brights")
+        self.assertEqual(result, False)
+
+        group = Group.objects.get(name="Brights")
+        self.assertEqual(group.name, "Brights")
+
+        # Unregister the group
+        result = permissions.utils.unregister_group("Brights")
+        self.assertEqual(result, True)
+
+        # It's not there anymore
+        self.assertRaises(Group.DoesNotExist, Group.objects.get, name="Brights")
+
+        # Trying to unregister the group again
+        result = permissions.utils.unregister_group("Brights")
+        self.assertEqual(result, False)
+
     def test_role(self):
         """Tests registering/unregistering of a role.
         """

File permissions/utils.py

     return True
 
 def register_role(name):
-    """Registers a group with passed name to the framework. Returns the new
+    """Registers a role with passed name to the framework. Returns the new
     role if the registration was successfully, otherwise False.
 
     **Parameters:**
 
     name
-        The unique group name.
+        The unique role name.
     """
     try:
         role = Role.objects.create(name=name)
 
     **Parameters:**
 
-        name
-            The unique role name.
+    name
+        The unique role name.
     """
     try:
         role = Role.objects.get(name=name)
         return False
 
     role.delete()
-    return True
+    return True
+    
+def register_group(name):
+    """Registers a group with passed name to the framework. Returns the new
+    group if the registration was successfully, otherwise False.
+    
+    Actually this creates just a default Django Group.
+
+    **Parameters:**
+
+    name
+        The unique group name.
+    """
+    try:
+        group = Group.objects.create(name=name)
+    except IntegrityError:
+        return False
+    return group
+
+def unregister_group(name):
+    """Unregisters the group with passed name. Returns True if the 
+    unregistration was succesfull otherwise False.
+
+    Actually this deletes just a default Django Group.
+
+    **Parameters:**
+
+    name
+        The unique role name.
+    """
+    try:
+        group = Group.objects.get(name=name)
+    except Group.DoesNotExist:
+        return False
+
+    group.delete()
+    return True