[CalendarServer-changes] [998] CalendarServer/trunk/twistedcaldav/directory

source_changes at macosforge.org source_changes at macosforge.org
Tue Jan 9 13:07:12 PST 2007


Revision: 998
          http://trac.macosforge.org/projects/calendarserver/changeset/998
Author:   cdaboo at apple.com
Date:     2007-01-09 13:07:12 -0800 (Tue, 09 Jan 2007)

Log Message:
-----------
groupMembers() and groupMembership() now return principals. The cu-proxy database now references principals
by GUID rather than uri.

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py
    CalendarServer/trunk/twistedcaldav/directory/principal.py

Modified: CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py	2007-01-09 21:05:21 UTC (rev 997)
+++ CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py	2007-01-09 21:07:12 UTC (rev 998)
@@ -79,10 +79,12 @@
         super(CalendarUserProxyPrincipalResource, self).__init__(path, joinURL(parent.principalURL(), proxyType))
 
         self.parent = parent
+        self.pcollection = self.parent.parent.parent
         self.proxyType = proxyType
         self._url = joinURL(parent.principalURL(), proxyType)
         if self.isCollection():
             self._url += "/"
+        self.guid = self.parent.principalUID() + "-" + proxyType
 
         # Provision in __init__() because principals are used prior to request
         # lookups.
@@ -95,13 +97,10 @@
         @return: the L{CalendarUserProxyDatabase} for the principal collection.
         """
         
-        # Get the principal collection we are contained in
-        pcollection = self.parent.parent.parent
-        
         # The db is located in the principal collection root
-        if not hasattr(pcollection, "calendar_user_proxy_db"):
-            setattr(pcollection, "calendar_user_proxy_db", CalendarUserProxyDatabase(pcollection.fp.path))
-        return pcollection.calendar_user_proxy_db
+        if not hasattr(self.pcollection, "calendar_user_proxy_db"):
+            setattr(self.pcollection, "calendar_user_proxy_db", CalendarUserProxyDatabase(self.pcollection.fp.path))
+        return self.pcollection.calendar_user_proxy_db
 
     def resourceType(self):
         if self.proxyType == "calendar-proxy-read":
@@ -120,10 +119,26 @@
         return super(CalendarUserProxyPrincipalResource, self).writeProperty(property, request)
 
     def setGroupMemberSet(self, new_members, request):
-        
+        # FIXME: as defined right now it is not possible to specify a calendar-user-proxy group as
+        # a member of any other group since the directory service does not know how to lookup
+        # these special resource GUIDs.
+        #
+        # Really, c-u-p principals should be treated the same way as any other principal, so
+        # they should be allowed as members of groups.
+        #
+        # This implementation simply ignores any principal URIs that correspond to c-u-p principals.
+
         # Break out the list into a set of URIs.
         members = [str(h) for h in new_members.children]
-        self._index().setGroupMembers(self._url, members)
+        
+        # Map the URIs to principals.
+        # NB Non-matching URIs will return None foe the principal
+        principals = [self.pcollection._principalForURI(uri) for uri in members]
+        
+        # Map the principals to GUIDs, ignoring principals that are None.
+        guids = [p.principalUID() for p in principals if p is not None]
+
+        self._index().setGroupMembers(self.guid, guids)
         return succeed(True)
 
     ##
@@ -219,14 +234,21 @@
     def principalURL(self):
         return self._url
 
+    def principalUID(self):
+        return self.guid
+
     def principalCollections(self):
         return self.parent.principalCollections()
 
     def groupMembers(self):
-        return self._index().getMembers(self._url)
+        # Get member GUIDs and map to principal resources
+        members = self._index().getMembers(self.guid)
+        return [self.pcollection.principalForGUID(guid) for guid in members]
 
     def groupMemberships(self):
-        return self._index().getMemberships(self._url)
+        # Get membership GUIDs and map to principal resources
+        memberships = self._index().getMemberships(self.guid)
+        return [self.pcollection.principalForGUID(guid) for guid in memberships]
 
 
 class CalendarUserProxyDatabase(AbstractSQLDatabase):
@@ -243,74 +265,74 @@
     
     dbType = "CALENDARUSERPROXY"
     dbFilename = ".db.calendaruserproxy"
-    dbFormatVersion = "1"
+    dbFormatVersion = "2"
 
     def __init__(self, path):
         path = os.path.join(path, CalendarUserProxyDatabase.dbFilename)
         super(CalendarUserProxyDatabase, self).__init__(path, CalendarUserProxyDatabase.dbFormatVersion)
 
-    def setGroupMembers(self, principalURI, members):
+    def setGroupMembers(self, principalGUID, members):
         """
         Add a group membership record.
     
-        @param principalURI: the principalURI of the group principal to add.
-        @param members: the list of principalURIs that are members of this group.
+        @param principalGUID: the principalGUID of the group principal to add.
+        @param members: the list of principalGUIDs that are members of this group.
         """
         
         # Remove what is there, then add it back.
-        self._delete_from_db(principalURI)
-        self._add_to_db(principalURI, members)
+        self._delete_from_db(principalGUID)
+        self._add_to_db(principalGUID, members)
         self._db_commit()
 
-    def removeGroup(self, principalURI):
+    def removeGroup(self, principalGUID):
         """
         Remove a group membership record.
     
-        @param principalURI: the principalURI of the group principal to add.
+        @param principalGUID: the principalGUID of the group principal to remove.
         """
-        self._delete_from_db(principalURI)
+        self._delete_from_db(principalGUID)
         self._db_commit()
     
-    def getMembers(self, principalURI):
+    def getMembers(self, principalGUID):
         """
-        Return the list of group members for the specified principal.
+        Return the list of group member GUIDs for the specified principal.
         """
         members = set()
-        for row in self._db_execute("select MEMBER from GROUPS where GROUPNAME = :1", principalURI):
+        for row in self._db_execute("select MEMBER from GROUPS where GROUPNAME = :1", principalGUID):
             members.add(row[0])
         return members
     
-    def getMemberships(self, principalURI):
+    def getMemberships(self, principalGUID):
         """
-        Return the list of groups the specified principal is a member of.
+        Return the list of group principal GUIDs the specified principal is a member of.
         """
         members = set()
-        for row in self._db_execute("select GROUPNAME from GROUPS where MEMBER = :1", principalURI):
+        for row in self._db_execute("select GROUPNAME from GROUPS where MEMBER = :1", principalGUID):
             members.add(row[0])
         return members
 
-    def _add_to_db(self, principalURI, members):
+    def _add_to_db(self, principalGUID, members):
         """
         Insert the specified entry into the database.
 
-        @param principalURI: the principalURI of the group principal to remove.
-        @param members: the list of principalURIs that are members of this group.
+        @param principalGUID: the principalGUID of the group principal to add.
+        @param members: the list of principalGUIDs that are members of this group.
         """
         for member in members:
             self._db_execute(
                 """
                 insert into GROUPS (GROUPNAME, MEMBER)
                 values (:1, :2)
-                """, principalURI, member
+                """, principalGUID, member
             )
        
-    def _delete_from_db(self, principalURI):
+    def _delete_from_db(self, principalGUID):
         """
         Deletes the specified entry from the database.
 
-        @param principalURI: the principalURI of the group principal to remove.
+        @param principalGUID: the principalGUID of the group principal to remove.
         """
-        self._db_execute("delete from GROUPS where GROUPNAME = :1", principalURI)
+        self._db_execute("delete from GROUPS where GROUPNAME = :1", principalGUID)
     
     def _db_type(self):
         """

Modified: CalendarServer/trunk/twistedcaldav/directory/principal.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/principal.py	2007-01-09 21:05:21 UTC (rev 997)
+++ CalendarServer/trunk/twistedcaldav/directory/principal.py	2007-01-09 21:07:12 UTC (rev 998)
@@ -384,13 +384,41 @@
             setattr(pcollection, "calendar_user_proxy_db", CalendarUserProxyDatabase(pcollection.fp.path))
         return pcollection.calendar_user_proxy_db
 
+    def _map_calendar_user_proxy_guids(self, guids):
+        """
+        Return a list of principals mapped from a list of calendar user proxy principal GUIDs.
+        
+        @param guids: a C{list} of C{str}'s containing the GUIDs to map.
+        @return: a C{list} of L{CalendarPrincipalResource}s for each mapped GUID.
+        """
+        proxies = []
+        for guid in guids:
+            # Get the "base" GUID for the parent of the proxy principal
+            if guid.endswith("-calendar-proxy-read"):
+                guid = guid[:-20]
+                proxyType = "calendar-proxy-read"
+            elif guid.endswith("-calendar-proxy-write"):
+                guid = guid[:-21]
+                proxyType = "calendar-proxy-write"
+                
+            # Lookup the base GUID and get its principal resource
+            principal = self.parent.principalForGUID(guid)
+            if principal:
+                proxyprincipal = principal.getChild(proxyType)
+                if proxyprincipal:
+                    proxies.append(proxyprincipal)
+                    
+        return proxies
+            
     def groupMembers(self):
         return self._getRelatives("members")
 
     def groupMemberships(self):
         groups = self._getRelatives("groups")
         if config.CalendarUserProxyEnabled:
-            groups.update(self._calendar_user_proxy_index().getMemberships(self._url))
+            # Get proxy group GUIDs and map to principal resources
+            proxies = self._map_calendar_user_proxy_guids(self._calendar_user_proxy_index().getMemberships(self.principalUID()))
+            groups.update(proxies)
         return groups
 
     def principalCollections(self):

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20070109/89c52a0e/attachment.html


More information about the calendarserver-changes mailing list