[CalendarServer-changes] [6195] CalendarServer/trunk/calendarserver/tools

source_changes at macosforge.org source_changes at macosforge.org
Thu Aug 26 16:38:36 PDT 2010


Revision: 6195
          http://trac.macosforge.org/projects/calendarserver/changeset/6195
Author:   sagen at apple.com
Date:     2010-08-26 16:38:36 -0700 (Thu, 26 Aug 2010)
Log Message:
-----------
calendarserver_command_gateway now allows setting of proxies while creating locations and resources (eliminating the need for multiple commands)

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/gateway.py
    CalendarServer/trunk/calendarserver/tools/principals.py
    CalendarServer/trunk/calendarserver/tools/test/test_gateway.py
    CalendarServer/trunk/calendarserver/tools/test/test_principals.py

Modified: CalendarServer/trunk/calendarserver/tools/gateway.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/gateway.py	2010-08-26 18:26:29 UTC (rev 6194)
+++ CalendarServer/trunk/calendarserver/tools/gateway.py	2010-08-26 23:38:36 UTC (rev 6195)
@@ -26,7 +26,7 @@
 from twext.python.plistlib import readPlistFromString, writePlistToString
 
 from twisted.internet import reactor
-from twisted.internet.defer import inlineCallbacks
+from twisted.internet.defer import inlineCallbacks, returnValue
 from twisted.python.util import switchUID
 from twistedcaldav.config import config, ConfigurationError
 from twistedcaldav.directory.directory import DirectoryError
@@ -35,7 +35,7 @@
 from calendarserver.tools.util import loadConfig, getDirectory, setupMemcached, checkDirectory
 from calendarserver.tools.principals import (
     principalForPrincipalID, proxySubprincipal, addProxy, removeProxy,
-    ProxyError, ProxyWarning, updateRecord
+    getProxies, setProxies, ProxyError, ProxyWarning, updateRecord
 )
 
 from twext.python.log import StandardIOObserver
@@ -220,13 +220,19 @@
                 kwargs[info['attr']] = command[key]
 
         try:
-            (yield updateRecord(True, self.dir, "locations", **kwargs))
+            record = (yield updateRecord(True, self.dir, "locations", **kwargs))
         except DirectoryError, e:
             respondWithError(str(e))
             return
 
+        readProxies = command.get("ReadProxies", None)
+        writeProxies = command.get("WriteProxies", None)
+        principal = principalForPrincipalID(record.guid, directory=self.dir)
+        (yield setProxies(principal, readProxies, writeProxies))
+
         respondWithRecordsOfType(self.dir, command, "locations")
 
+    @inlineCallbacks
     def command_getLocationAttributes(self, command):
         guid = command['GeneratedUID']
         record = self.dir.recordWithGUID(guid)
@@ -239,8 +245,11 @@
             respondWithError("Principal not found: %s" % (guid,))
             return
         recordDict['AutoSchedule'] = principal.getAutoSchedule()
+        recordDict['ReadProxies'], recordDict['WriteProxies'] = (yield getProxies(principal))
         respond(command, recordDict)
 
+    command_getResourceAttributes = command_getLocationAttributes
+
     @inlineCallbacks
     def command_setLocationAttributes(self, command):
 
@@ -255,13 +264,18 @@
             if command.has_key(key):
                 kwargs[info['attr']] = command[key]
         try:
-            (yield updateRecord(False, self.dir, "locations", **kwargs))
+            record = (yield updateRecord(False, self.dir, "locations", **kwargs))
         except DirectoryError, e:
             respondWithError(str(e))
             return
 
-        self.command_getLocationAttributes(command)
+        readProxies = command.get("ReadProxies", None)
+        writeProxies = command.get("WriteProxies", None)
+        principal = principalForPrincipalID(record.guid, directory=self.dir)
+        (yield setProxies(principal, readProxies, writeProxies))
 
+        yield self.command_getLocationAttributes(command)
+
     def command_deleteLocation(self, command):
         kwargs = {}
         for key, info in attrMap.iteritems():
@@ -287,16 +301,18 @@
                 kwargs[info['attr']] = command[key]
 
         try:
-            (yield updateRecord(True, self.dir, "resources", **kwargs))
+            record = (yield updateRecord(True, self.dir, "resources", **kwargs))
         except DirectoryError, e:
             respondWithError(str(e))
             return
 
+        readProxies = command.get("ReadProxies", None)
+        writeProxies = command.get("WriteProxies", None)
+        principal = principalForPrincipalID(record.guid, directory=self.dir)
+        (yield setProxies(principal, readProxies, writeProxies))
+
         respondWithRecordsOfType(self.dir, command, "resources")
 
-    def command_getResourceAttributes(self, command):
-        self.command_getLocationAttributes(command)
-
     @inlineCallbacks
     def command_setResourceAttributes(self, command):
 
@@ -311,13 +327,18 @@
             if command.has_key(key):
                 kwargs[info['attr']] = command[key]
         try:
-            (yield updateRecord(False, self.dir, "resources", **kwargs))
+            record = (yield updateRecord(False, self.dir, "resources", **kwargs))
         except DirectoryError, e:
             respondWithError(str(e))
             return
 
-        self.command_getResourceAttributes(command)
+        readProxies = command.get("ReadProxies", None)
+        writeProxies = command.get("WriteProxies", None)
+        principal = principalForPrincipalID(record.guid, directory=self.dir)
+        (yield setProxies(principal, readProxies, writeProxies))
 
+        yield self.command_getResourceAttributes(command)
+
     def command_deleteResource(self, command):
         kwargs = {}
         for key, info in attrMap.iteritems():

Modified: CalendarServer/trunk/calendarserver/tools/principals.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/principals.py	2010-08-26 18:26:29 UTC (rev 6194)
+++ CalendarServer/trunk/calendarserver/tools/principals.py	2010-08-26 23:38:36 UTC (rev 6195)
@@ -26,7 +26,7 @@
 
 from twisted.python.util import switchUID
 from twisted.internet import reactor
-from twisted.internet.defer import inlineCallbacks
+from twisted.internet.defer import inlineCallbacks, returnValue
 from twext.web2.dav import davxml
 
 from twext.python.log import clearLogLevels
@@ -573,6 +573,58 @@
 
 
 @inlineCallbacks
+def setProxies(principal, readProxyPrincipals, writeProxyPrincipals, directory=None):
+    """
+    Set read/write proxies en masse for a principal
+    @param principal: DirectoryPrincipalResource
+    @param readProxyPrincipals: a list of principal IDs (see principalForPrincipalID)
+    @param writeProxyPrincipals: a list of principal IDs (see principalForPrincipalID)
+    """
+
+    proxyTypes = [
+        ("read", readProxyPrincipals),
+        ("write", writeProxyPrincipals),
+    ]
+    for proxyType, proxyIDs in proxyTypes:
+        if proxyIDs is None:
+            continue
+        subPrincipal = proxySubprincipal(principal, proxyType)
+        if subPrincipal is None:
+            raise ProxyError("Unable to edit %s proxies for %s\n" % (proxyType,
+                prettyPrincipal(principal)))
+        memberURLs = []
+        for proxyID in proxyIDs:
+            proxyPrincipal = principalForPrincipalID(proxyID, directory=directory)
+            proxyURL = proxyPrincipal.url()
+            memberURLs.append(davxml.HRef(proxyURL))
+        membersProperty = davxml.GroupMemberSet(*memberURLs)
+        (yield subPrincipal.writeProperty(membersProperty, None))
+
+
+ at inlineCallbacks
+def getProxies(principal, directory=None):
+    """
+    Returns a tuple containing the GUIDs for read proxies and write proxies
+    of the given principal
+    """
+
+    proxies = {
+        "read" : [],
+        "write" : [],
+    }
+    for proxyType in proxies.iterkeys():
+        subPrincipal = proxySubprincipal(principal, proxyType)
+        if subPrincipal is not None:
+            membersProperty = (yield subPrincipal.readProperty(davxml.GroupMemberSet, None))
+            if membersProperty.children:
+                for member in membersProperty.children:
+                    proxyPrincipal = principalForPrincipalID(str(member), directory=directory)
+                    proxies[proxyType].append(proxyPrincipal.record.guid)
+
+    returnValue((proxies['read'], proxies['write']))
+
+
+ at inlineCallbacks
 def action_removeProxy(principal, *proxyIDs, **kwargs):
     for proxyID in proxyIDs:
         proxyPrincipal = principalForPrincipalID(proxyID)
@@ -741,15 +793,17 @@
         record = directory.createRecord(recordType, **kwargs)
         kwargs['guid'] = record.guid
     else:
-        directory.updateRecord(recordType, **kwargs)
+        record = directory.updateRecord(recordType, **kwargs)
 
     augmentRecord = (yield augment.AugmentService.getAugmentRecord(kwargs['guid'], recordType))
     augmentRecord.autoSchedule = autoSchedule
     (yield augment.AugmentService.addAugmentRecords([augmentRecord]))
     directory.updateRecord(recordType, **kwargs)
 
+    returnValue(record)
 
 
 
+
 if __name__ == "__main__":
     main()

Modified: CalendarServer/trunk/calendarserver/tools/test/test_gateway.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/test/test_gateway.py	2010-08-26 18:26:29 UTC (rev 6194)
+++ CalendarServer/trunk/calendarserver/tools/test/test_gateway.py	2010-08-26 23:38:36 UTC (rev 6195)
@@ -118,6 +118,8 @@
         self.assertEquals(results["result"]["RealName"], "Created Location 01")
         self.assertEquals(results["result"]["Comment"], "Test Comment")
         self.assertEquals(results["result"]["AutoSchedule"], True)
+        self.assertEquals(set(results["result"]["ReadProxies"]), set(['user03', 'user04']))
+        self.assertEquals(set(results["result"]["WriteProxies"]), set(['user05', 'user06']))
 
     @inlineCallbacks
     def test_getResourceList(self):
@@ -130,6 +132,8 @@
         results = yield self.runCommand(command_getResourceAttributes)
         self.assertEquals(results["result"]["Comment"], "Test Comment")
         self.assertEquals(results["result"]["Type"], "Computer")
+        self.assertEquals(set(results["result"]["ReadProxies"]), set(['user03', 'user04']))
+        self.assertEquals(set(results["result"]["WriteProxies"]), set(['user05', 'user06']))
 
     @inlineCallbacks
     def test_createLocation(self):
@@ -162,6 +166,10 @@
         self.assertEquals(record.extras["country"], "USA")
         self.assertEquals(record.extras["phone"], "(408) 555-1212")
 
+        results = yield self.runCommand(command_getLocationAttributes)
+        self.assertEquals(set(results["result"]["ReadProxies"]), set(['user03', 'user04']))
+        self.assertEquals(set(results["result"]["WriteProxies"]), set(['user05', 'user06']))
+
     @inlineCallbacks
     def test_setLocationAttributes(self):
         directory = getDirectory()
@@ -191,6 +199,8 @@
 
         results = yield self.runCommand(command_getLocationAttributes)
         self.assertEquals(results["result"]["AutoSchedule"], True)
+        self.assertEquals(set(results["result"]["ReadProxies"]), set(['user03']))
+        self.assertEquals(set(results["result"]["WriteProxies"]), set(['user05', 'user06', 'user07']))
 
 
     @inlineCallbacks
@@ -325,6 +335,16 @@
         <string>USA</string>
         <key>Phone</key>
         <string>(408) 555-1212</string>
+        <key>ReadProxies</key>
+        <array>
+            <string>users:user03</string>
+            <string>users:user04</string>
+        </array>
+        <key>WriteProxies</key>
+        <array>
+            <string>users:user05</string>
+            <string>users:user06</string>
+        </array>
 </dict>
 </plist>
 """
@@ -350,6 +370,16 @@
         </array>
         <key>Comment</key>
         <string>Test Comment</string>
+        <key>ReadProxies</key>
+        <array>
+            <string>users:user03</string>
+            <string>users:user04</string>
+        </array>
+        <key>WriteProxies</key>
+        <array>
+            <string>users:user05</string>
+            <string>users:user06</string>
+        </array>
 </dict>
 </plist>
 """
@@ -488,6 +518,16 @@
         <string>Updated USA</string>
         <key>Phone</key>
         <string>(408) 555-1213</string>
+        <key>ReadProxies</key>
+        <array>
+            <string>users:user03</string>
+        </array>
+        <key>WriteProxies</key>
+        <array>
+            <string>users:user05</string>
+            <string>users:user06</string>
+            <string>users:user07</string>
+        </array>
 </dict>
 </plist>
 """

Modified: CalendarServer/trunk/calendarserver/tools/test/test_principals.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/test/test_principals.py	2010-08-26 18:26:29 UTC (rev 6194)
+++ CalendarServer/trunk/calendarserver/tools/test/test_principals.py	2010-08-26 23:38:36 UTC (rev 6195)
@@ -25,7 +25,7 @@
 from twistedcaldav.test.util import TestCase, CapturingProcessProtocol
 
 from calendarserver.tap.util import getRootResource
-from calendarserver.tools.principals import parseCreationArgs, matchStrings, updateRecord
+from calendarserver.tools.principals import parseCreationArgs, matchStrings, updateRecord, principalForPrincipalID, getProxies, setProxies
 
 
 class ManagePrincipalsTestCase(TestCase):
@@ -258,3 +258,27 @@
         directory.destroyRecord("locations", guid=guid)
         record = directory.recordWithGUID(guid)
         self.assertTrue(record is None)
+
+    @inlineCallbacks
+    def test_setProxies(self):
+        """
+        Read and Write proxies can be set en masse
+        """
+        directory = getRootResource(config).getDirectory()
+
+        principal = principalForPrincipalID("users:user01", directory=directory)
+        readProxies, writeProxies = (yield getProxies(principal, directory=directory))
+        self.assertEquals(readProxies, []) # initially empty
+        self.assertEquals(writeProxies, []) # initially empty
+
+        (yield setProxies(principal, ["users:user03", "users:user04"], ["users:user05"], directory=directory))
+        readProxies, writeProxies = (yield getProxies(principal, directory=directory))
+        self.assertEquals(set(readProxies), set(["user03", "user04"]))
+        self.assertEquals(set(writeProxies), set(["user05"]))
+
+        # Using None for a proxy list indicates a no-op
+        (yield setProxies(principal, [], None, directory=directory))
+        readProxies, writeProxies = (yield getProxies(principal, directory=directory))
+        self.assertEquals(readProxies, []) # now empty
+        self.assertEquals(set(writeProxies), set(["user05"])) # unchanged
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100826/92655ccf/attachment-0001.html>


More information about the calendarserver-changes mailing list