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

source_changes at macosforge.org source_changes at macosforge.org
Mon Jul 30 16:32:20 PDT 2007


Revision: 1734
          http://trac.macosforge.org/projects/calendarserver/changeset/1734
Author:   wsanchez at apple.com
Date:     2007-07-30 16:32:19 -0700 (Mon, 30 Jul 2007)

Log Message:
-----------
Merge http://svn.macosforge.org/repository/calendarserver/CalendarServer/branches/users/wsanchez/guid-cleanup

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-07-30 23:31:15 UTC (rev 1733)
+++ CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py	2007-07-30 23:32:19 UTC (rev 1734)
@@ -39,7 +39,6 @@
 from twistedcaldav.sql import AbstractSQLDatabase
 from twistedcaldav.sql import db_prefix
 from twistedcaldav.static import AutoProvisioningFileMixIn
-from twistedcaldav.directory.util import uuidFromName
 
 import os
 
@@ -78,8 +77,6 @@
     Calendar user proxy principal resource.
     """
 
-    guidMapper = {}    # dict to map a GUID to a proxy principal
-
     def davComplianceClasses(self):
         return tuple(super(CalendarUserProxyPrincipalResource, self).davComplianceClasses()) + (
             "calendar-access",
@@ -102,9 +99,15 @@
         self._url = joinURL(parent.principalURL(), proxyType)
         if self.isCollection():
             self._url += "/"
-        self.guid = uuidFromName(self.parent.principalUID(), proxyType)
-        self.guidMapper[self.guid] = self
 
+        # Not terribly useful at present because we don't have a way
+        # to map a GUID back to the correct principal.
+        #self.guid = uuidFromName(self.parent.principalUID(), proxyType)
+
+        # Principal UID is parent's GUID plus the proxy type; this we
+        # can easily map back to a principal.
+        self.uid = "%s#%s" % (self.parent.principalUID(), proxyType)
+
         # Provision in __init__() because principals are used prior to request
         # lookups.
         self.provision()
@@ -121,10 +124,6 @@
             setattr(self.pcollection, "calendar_user_proxy_db", CalendarUserProxyDatabase(self.pcollection.fp.path))
         return self.pcollection.calendar_user_proxy_db
 
-    @classmethod
-    def principalForGUID(cls, guid):
-        return cls.guidMapper.get(guid)
-
     def resourceType(self):
         if self.proxyType == "calendar-proxy-read":
             return davxml.ResourceType.calendarproxyread
@@ -155,7 +154,7 @@
     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.
+        # these special resource UIDs.
         #
         # Really, c-u-p principals should be treated the same way as any other principal, so
         # they should be allowed as members of groups.
@@ -177,10 +176,10 @@
                 ))
             principals.append(principal)
         
-        # Map the principals to GUIDs.
-        guids = [p.principalUID() for p in principals]
+        # Map the principals to UIDs.
+        uids = [p.principalUID() for p in principals]
 
-        self._index().setGroupMembers(self.guid, guids)
+        self._index().setGroupMembers(self.uid, uids)
         return succeed(True)
 
     ##
@@ -226,7 +225,7 @@
                 """\n"""
                 """Proxy Principal Information\n"""
                 """---------------------\n"""
-                """GUID: %s\n"""                   % (self.guid,),
+               #"""GUID: %s\n"""                   % (self.guid,),
                 """Principal UID: %s\n"""          % (self.principalUID(),),
                 """Principal URL: %s\n"""          % (link(self.principalURL()),),
                 """\nAlternate URIs:\n"""          , format_list(self.alternateURIs()),
@@ -259,16 +258,16 @@
         return self._url
 
     def principalUID(self):
-        return self.guid
+        return self.uid
 
     def principalCollections(self):
         return self.parent.principalCollections()
 
     def groupMembers(self):
         if self.hasEditableMembership():
-            # Get member GUIDs from database and map to principal resources
-            members = self._index().getMembers(self.guid)
-            return [self.pcollection.principalForGUID(guid) for guid in members]
+            # Get member UIDs from database and map to principal resources
+            members = self._index().getMembers(self.uid)
+            return [self.pcollection.principalForUID(uid) for uid in members]
         else:
             # Fixed proxies are only for read-write - the read-only list is empty
             if self.proxyType == "calendar-proxy-write":
@@ -277,9 +276,9 @@
                 return ()
 
     def groupMemberships(self):
-        # Get membership GUIDs and map to principal resources
-        memberships = self._index().getMemberships(self.guid)
-        return [self.pcollection.principalForGUID(guid) for guid in memberships]
+        # Get membership UIDs and map to principal resources
+        memberships = self._index().getMemberships(self.uid)
+        return [self.pcollection.principalForUID(uid) for uid in memberships]
 
     def hasEditableMembership(self):
         return self.parent.hasEditableProxyMembership()
@@ -304,68 +303,68 @@
         path = os.path.join(path, CalendarUserProxyDatabase.dbFilename)
         super(CalendarUserProxyDatabase, self).__init__(path)
 
-    def setGroupMembers(self, principalGUID, members):
+    def setGroupMembers(self, principalUID, members):
         """
         Add a group membership record.
     
-        @param principalGUID: the principalGUID of the group principal to add.
-        @param members: the list of principalGUIDs that are members of this group.
+        @param principalUID: the UID of the group principal to add.
+        @param members: a list UIDs of principals that are members of this group.
         """
         
         # Remove what is there, then add it back.
-        self._delete_from_db(principalGUID)
-        self._add_to_db(principalGUID, members)
+        self._delete_from_db(principalUID)
+        self._add_to_db(principalUID, members)
         self._db_commit()
 
-    def removeGroup(self, principalGUID):
+    def removeGroup(self, principalUID):
         """
         Remove a group membership record.
     
-        @param principalGUID: the principalGUID of the group principal to remove.
+        @param principalUID: the UID of the group principal to remove.
         """
-        self._delete_from_db(principalGUID)
+        self._delete_from_db(principalUID)
         self._db_commit()
     
-    def getMembers(self, principalGUID):
+    def getMembers(self, principalUID):
         """
-        Return the list of group member GUIDs for the specified principal.
+        Return the list of group member UIDs for the specified principal.
         """
         members = set()
-        for row in self._db_execute("select MEMBER from GROUPS where GROUPNAME = :1", principalGUID):
+        for row in self._db_execute("select MEMBER from GROUPS where GROUPNAME = :1", principalUID):
             members.add(row[0])
         return members
     
-    def getMemberships(self, principalGUID):
+    def getMemberships(self, principalUID):
         """
-        Return the list of group principal GUIDs the specified principal is a member of.
+        Return the list of group principal UIDs the specified principal is a member of.
         """
         members = set()
-        for row in self._db_execute("select GROUPNAME from GROUPS where MEMBER = :1", principalGUID):
+        for row in self._db_execute("select GROUPNAME from GROUPS where MEMBER = :1", principalUID):
             members.add(row[0])
         return members
 
-    def _add_to_db(self, principalGUID, members):
+    def _add_to_db(self, principalUID, members):
         """
         Insert the specified entry into the database.
 
-        @param principalGUID: the principalGUID of the group principal to add.
-        @param members: the list of principalGUIDs that are members of this group.
+        @param principalUID: the UID of the group principal to add.
+        @param members: a list of UIDs or principals that are members of this group.
         """
         for member in members:
             self._db_execute(
                 """
                 insert into GROUPS (GROUPNAME, MEMBER)
                 values (:1, :2)
-                """, principalGUID, member
+                """, principalUID, member
             )
        
-    def _delete_from_db(self, principalGUID):
+    def _delete_from_db(self, principalUID):
         """
         Deletes the specified entry from the database.
 
-        @param principalGUID: the principalGUID of the group principal to remove.
+        @param principalUID: the UID of the group principal to remove.
         """
-        self._db_execute("delete from GROUPS where GROUPNAME = :1", principalGUID)
+        self._db_execute("delete from GROUPS where GROUPNAME = :1", principalUID)
     
     def _db_version(self):
         """

Modified: CalendarServer/trunk/twistedcaldav/directory/principal.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/principal.py	2007-07-30 23:31:15 UTC (rev 1733)
+++ CalendarServer/trunk/twistedcaldav/directory/principal.py	2007-07-30 23:32:19 UTC (rev 1734)
@@ -58,12 +58,41 @@
         # Permissions here are fixed, and are not subject to inherritance rules, etc.
         return succeed(self.defaultAccessControlList())
 
-class DirectoryPrincipalProvisioningResource (
+class DirectoryProvisioningResource(
     AutoProvisioningFileMixIn,
     PermissionsMixIn,
     CalendarPrincipalCollectionResource,
     DAVFile,
 ):
+    def principalForShortName(self, type, name):
+        raise NotImplementedError("Subclass must implement principalForShortName()")
+
+    def principalForUser(self, user):
+        return self.principalForShortName(DirectoryService.recordType_users, user)
+
+    def principalForGUID(self, guid):
+        record = self.directory.recordWithGUID(guid)
+        if record:
+            return self.principalForRecord(record)
+        else:
+            return None
+
+    def principalForUID(self, uid):
+        if "#" in uid:
+            # This UID belongs to a sub-principal
+            parent_uid, subType = uid.split("#")
+            return self.principalForGUID(parent_uid).getChild(subType)
+        else:
+            # This UID belongs to a primary principal (UID == GUID)
+            return self.principalForGUID(uid)
+
+    def principalForRecord(self, record):
+        return self.principalForShortName(record.recordType, record.shortName)
+
+    def principalForCalendarUserAddress(self, address):
+        raise NotImplementedError("Subclass must implement principalForCalendarUserAddress()")
+
+class DirectoryPrincipalProvisioningResource (DirectoryProvisioningResource):
     """
     Collection resource which provisions directory principals as its children.
     """
@@ -93,21 +122,6 @@
             return None
         return typeResource.getChild(name)
 
-    def principalForUser(self, user):
-        return self.principalForShortName(DirectoryService.recordType_users, user)
-
-    def principalForGUID(self, guid):
-        record = self.directory.recordWithGUID(guid)
-        if record:
-            return self.principalForRecord(record)
-        elif config.EnableProxyPrincipals:
-            return CalendarUserProxyPrincipalResource.principalForGUID(guid)
-        else:
-            return None
-
-    def principalForRecord(self, record):
-        return self.principalForShortName(record.recordType, record.shortName)
-
     def _principalForURI(self, uri):
         scheme, netloc, path, params, query, fragment = urlparse(uri)
 
@@ -194,12 +208,7 @@
     def principalCollections(self):
         return (self,)
 
-class DirectoryPrincipalTypeResource (
-    AutoProvisioningFileMixIn,
-    PermissionsMixIn,
-    CalendarPrincipalCollectionResource,
-    DAVFile,
-):
+class DirectoryPrincipalTypeResource (DirectoryProvisioningResource):
     """
     Collection resource which provisions directory principals of a specific type as its children.
     """
@@ -219,15 +228,6 @@
     def principalForShortName(self, type, name):
         return self.parent.principalForShortName(type, name)
 
-    def principalForUser(self, user):
-        return self.parent.principalForUser(user)
-
-    def principalForRecord(self, record):
-        return self.parent.principalForRecord(record)
-
-    def principalForGUID(self, guid):
-        return self.parent.principalForGUID(guid)
-
     def principalForCalendarUserAddress(self, address):
         return self.parent.principalForCalendarUserAddress(address)
 
@@ -412,8 +412,13 @@
             # Get any directory specified proxies
             groups.update(self._getRelatives("proxyFor", proxy=True))
 
-            # Get proxy group GUIDs and map to principal resources
-            proxies = [self.parent.principalForGUID(guid) for guid in self._calendar_user_proxy_index().getMemberships(self.principalUID())]
+            # Get proxy group UIDs and map to principal resources
+            proxies = []
+            for uid in self._calendar_user_proxy_index().getMemberships(self.principalUID()):
+                subprincipal = self.parent.principalForUID(uid)
+                if subprincipal:
+                    proxies.append(subprincipal)
+
             groups.update(proxies)
 
         return groups
@@ -437,8 +442,8 @@
             addresses.add("http://%s:%s%s" % (config.ServerHostName, config.HTTPPort, self.principalURL(),))
         if config.SSLPort:
             addresses.add("https://%s:%s%s" % (config.ServerHostName, config.SSLPort, self.principalURL(),))
-        addresses.add("urn:uuid:%s" % (self.principalUID(),))
-        
+        addresses.add("urn:uuid:%s" % (self.record.guid,))
+
         return addresses
 
     def autoSchedule(self):

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20070730/438b38a5/attachment.html


More information about the calendarserver-changes mailing list