<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12829] CalendarServer/branches/users/sagen/move2who</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/12829">12829</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-03-05 20:18:19 -0800 (Wed, 05 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Modifying delegate assignments via PROPPATCH works</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesuserssagenmove2whotwistedcaldavdirectorycalendaruserproxypy">CalendarServer/branches/users/sagen/move2who/twistedcaldav/directory/calendaruserproxy.py</a></li>
<li><a href="#CalendarServerbranchesuserssagenmove2whotxdavcommondatastoresqlpy">CalendarServer/branches/users/sagen/move2who/txdav/common/datastore/sql.py</a></li>
<li><a href="#CalendarServerbranchesuserssagenmove2whotxdavdpsclientpy">CalendarServer/branches/users/sagen/move2who/txdav/dps/client.py</a></li>
<li><a href="#CalendarServerbranchesuserssagenmove2whotxdavdpscommandspy">CalendarServer/branches/users/sagen/move2who/txdav/dps/commands.py</a></li>
<li><a href="#CalendarServerbranchesuserssagenmove2whotxdavdpsserverpy">CalendarServer/branches/users/sagen/move2who/txdav/dps/server.py</a></li>
<li><a href="#CalendarServerbranchesuserssagenmove2whotxdavwhoaugmentpy">CalendarServer/branches/users/sagen/move2who/txdav/who/augment.py</a></li>
<li><a href="#CalendarServerbranchesuserssagenmove2whotxdavwhodelegatespy">CalendarServer/branches/users/sagen/move2who/txdav/who/delegates.py</a></li>
<li><a href="#CalendarServerbranchesuserssagenmove2whotxdavwhogroupspy">CalendarServer/branches/users/sagen/move2who/txdav/who/groups.py</a></li>
<li><a href="#CalendarServerbranchesuserssagenmove2whotxdavwhotesttest_delegatespy">CalendarServer/branches/users/sagen/move2who/txdav/who/test/test_delegates.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesuserssagenmove2whotwistedcaldavdirectorycalendaruserproxypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/twistedcaldav/directory/calendaruserproxy.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/twistedcaldav/directory/calendaruserproxy.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/twistedcaldav/directory/calendaruserproxy.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -284,7 +284,7 @@
</span><span class="cx">         principals = []
</span><span class="cx">         newUIDs = set()
</span><span class="cx">         for uri in members:
</span><del>-            principal = self.pcollection._principalForURI(uri)
</del><ins>+            principal = yield self.pcollection._principalForURI(uri)
</ins><span class="cx">             # Invalid principals MUST result in an error.
</span><span class="cx">             if principal is None or principal.principalURL() != uri:
</span><span class="cx">                 raise HTTPError(StatusResponse(
</span><span class="lines">@@ -298,7 +298,7 @@
</span><span class="cx">         # Get the old set of UIDs
</span><span class="cx">         # oldUIDs = (yield self._index().getMembers(self.uid))
</span><span class="cx">         oldPrincipals = yield self.groupMembers()
</span><del>-        oldUIDs = [p.uid for p in oldPrincipals]
</del><ins>+        oldUIDs = [p.principalUID() for p in oldPrincipals]
</ins><span class="cx"> 
</span><span class="cx">         # Change membership
</span><span class="cx">         yield self.setGroupMemberSetPrincipals(principals)
</span><span class="lines">@@ -309,19 +309,24 @@
</span><span class="cx"> 
</span><span class="cx">         changedUIDs = newUIDs.symmetric_difference(oldUIDs)
</span><span class="cx">         for uid in changedUIDs:
</span><del>-            principal = self.pcollection.principalForUID(uid)
</del><ins>+            principal = yield self.pcollection.principalForUID(uid)
</ins><span class="cx">             if principal:
</span><span class="cx">                 yield principal.cacheNotifier.changed()
</span><span class="cx"> 
</span><span class="cx">         returnValue(True)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
</ins><span class="cx">     def setGroupMemberSetPrincipals(self, principals):
</span><del>-        # Map the principals to UIDs.
-        return self._index().setGroupMembers(
-            self.uid,
-            [p.principalUID() for p in principals],
</del><ins>+
+        # Find our pseudo-record
+        record = yield self.parent.record.service.recordWithShortName(
+            self._recordTypeFromProxyType(),
+            self.parent.principalUID()
</ins><span class="cx">         )
</span><ins>+        # Set the members
+        memberRecords = [p.record for p in principals]
+        yield record.setMembers(memberRecords)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     ##
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagenmove2whotxdavcommondatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/txdav/common/datastore/sql.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/txdav/common/datastore/sql.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/txdav/common/datastore/sql.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -1244,6 +1244,19 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @classproperty
</span><ins>+    def _removeDelegatesQuery(cls):
+        de = schema.DELEGATES
+        return Delete(
+            From=de,
+            Where=(
+                de.DELEGATOR == Parameter(&quot;delegator&quot;)
+            ).And(
+                de.READ_WRITE == Parameter(&quot;readWrite&quot;)
+            )
+        )
+
+
+    @classproperty
</ins><span class="cx">     def _removeDelegateGroupQuery(cls):
</span><span class="cx">         ds = schema.DELEGATE_GROUPS
</span><span class="cx">         return Delete(
</span><span class="lines">@@ -1259,6 +1272,19 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @classproperty
</span><ins>+    def _removeDelegateGroupsQuery(cls):
+        ds = schema.DELEGATE_GROUPS
+        return Delete(
+            From=ds,
+            Where=(
+                ds.DELEGATOR == Parameter(&quot;delegator&quot;)
+            ).And(
+                ds.READ_WRITE == Parameter(&quot;readWrite&quot;)
+            )
+        )
+
+
+    @classproperty
</ins><span class="cx">     def _selectDelegatesQuery(cls):
</span><span class="cx">         de = schema.DELEGATES
</span><span class="cx">         return Select(
</span><span class="lines">@@ -1463,6 +1489,24 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def removeDelegates(self, delegator, readWrite):
+        &quot;&quot;&quot;
+        Removes all rows for this delegator/readWrite combination from the
+        DELEGATES table.
+
+        @param delegator: the UID of the delegator
+        @type delegator: C{unicode}
+        @param readWrite: remove read and write access if True, otherwise
+            read-only access
+        @type readWrite: C{boolean}
+        &quot;&quot;&quot;
+        return self._removeDelegatesQuery.on(
+            self,
+            delegator=delegator.encode(&quot;utf-8&quot;),
+            readWrite=1 if readWrite else 0
+        )
+
+
</ins><span class="cx">     def removeDelegateGroup(self, delegator, delegateGroupID, readWrite):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Removes a row from the DELEGATE_GROUPS table.  The delegate should be a
</span><span class="lines">@@ -1484,6 +1528,24 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def removeDelegateGroups(self, delegator, readWrite):
+        &quot;&quot;&quot;
+        Removes all rows for this delegator/readWrite combination from the
+        DELEGATE_GROUPS table.
+
+        @param delegator: the UID of the delegator
+        @type delegator: C{unicode}
+        @param readWrite: remove read and write access if True, otherwise
+            read-only access
+        @type readWrite: C{boolean}
+        &quot;&quot;&quot;
+        return self._removeDelegateGroupsQuery.on(
+            self,
+            delegator=delegator.encode(&quot;utf-8&quot;),
+            readWrite=1 if readWrite else 0
+        )
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def delegates(self, delegator, readWrite, expanded=False):
</span><span class="cx">         &quot;&quot;&quot;
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagenmove2whotxdavdpsclientpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/txdav/dps/client.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/txdav/dps/client.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/txdav/dps/client.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -35,7 +35,7 @@
</span><span class="cx"> from txdav.dps.commands import (
</span><span class="cx">     RecordWithShortNameCommand, RecordWithUIDCommand, RecordWithGUIDCommand,
</span><span class="cx">     RecordsWithRecordTypeCommand, RecordsWithEmailAddressCommand,
</span><del>-    MembersCommand, GroupsCommand,
</del><ins>+    MembersCommand, GroupsCommand, SetMembersCommand,
</ins><span class="cx">     VerifyPlaintextPasswordCommand, VerifyHTTPDigestCommand
</span><span class="cx"> )
</span><span class="cx"> import txdav.who.delegates
</span><span class="lines">@@ -290,7 +290,7 @@
</span><span class="cx">     def verifyCredentials(self, credentials):
</span><span class="cx"> 
</span><span class="cx">         # XYZZY REMOVE THIS, it bypasses all authentication!:
</span><del>-        returnValue(True)
</del><ins>+        # returnValue(True)
</ins><span class="cx"> 
</span><span class="cx">         if isinstance(credentials, UsernamePassword):
</span><span class="cx">             log.debug(&quot;UsernamePassword&quot;)
</span><span class="lines">@@ -362,6 +362,17 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def setMembers(self, members):
+        log.debug(&quot;DPS Client setMembers&quot;)
+        memberUIDs = [m.uid.encode(&quot;utf-8&quot;) for m in members]
+        return self.service._call(
+            SetMembersCommand,
+            lambda x: x['success'],
+            uid=self.uid.encode(&quot;utf-8&quot;),
+            memberUIDs=memberUIDs
+        )
+
+
</ins><span class="cx">     @property
</span><span class="cx">     def calendarUserAddresses(self):
</span><span class="cx">         if not self.hasCalendars:
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagenmove2whotxdavdpscommandspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/txdav/dps/commands.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/txdav/dps/commands.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/txdav/dps/commands.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -98,6 +98,7 @@
</span><span class="cx">     ]
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> class GroupsCommand(amp.Command):
</span><span class="cx">     arguments = [
</span><span class="cx">         ('uid', amp.String()),
</span><span class="lines">@@ -108,6 +109,17 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class SetMembersCommand(amp.Command):
+    arguments = [
+        ('uid', amp.String()),
+        ('memberUIDs', amp.ListOf(amp.String())),
+    ]
+    response = [
+        ('success', amp.Boolean()),
+    ]
+
+
+
</ins><span class="cx"> class VerifyPlaintextPasswordCommand(amp.Command):
</span><span class="cx">     arguments = [
</span><span class="cx">         ('uid', amp.String()),
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagenmove2whotxdavdpsserverpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/txdav/dps/server.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/txdav/dps/server.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/txdav/dps/server.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -39,7 +39,7 @@
</span><span class="cx"> from txdav.dps.commands import (
</span><span class="cx">     RecordWithShortNameCommand, RecordWithUIDCommand, RecordWithGUIDCommand,
</span><span class="cx">     RecordsWithRecordTypeCommand, RecordsWithEmailAddressCommand,
</span><del>-    MembersCommand, GroupsCommand,
</del><ins>+    MembersCommand, GroupsCommand, SetMembersCommand,
</ins><span class="cx">     VerifyPlaintextPasswordCommand, VerifyHTTPDigestCommand,
</span><span class="cx">     # UpdateRecordsCommand, RemoveRecordsCommand
</span><span class="cx"> )
</span><span class="lines">@@ -194,6 +194,36 @@
</span><span class="cx">         returnValue(response)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @SetMembersCommand.responder
+    @inlineCallbacks
+    def setMembers(self, uid, memberUIDs):
+        uid = uid.decode(&quot;utf-8&quot;)
+        memberUIDs = [m.decode(&quot;utf-8&quot;) for m in memberUIDs]
+        log.debug(&quot;Set Members: {u} -&gt; {m}&quot;, u=uid, m=memberUIDs)
+        try:
+            record = (yield self._directory.recordWithUID(uid))
+        except Exception as e:
+            log.error(&quot;Failed in setMembers&quot;, error=e)
+            record = None
+
+        if record is not None:
+            memberRecords = []
+            for memberUID in memberUIDs:
+                memberRecord = yield self._directory.recordWithUID(memberUID)
+                if memberRecord is not None:
+                    memberRecords.append(memberRecord)
+            yield record.setMembers(memberRecords)
+            success = True
+        else:
+            success = False
+
+        response = {
+            &quot;success&quot;: success,
+        }
+        log.debug(&quot;Responding with: {response}&quot;, response=response)
+        returnValue(response)
+
+
</ins><span class="cx">     @GroupsCommand.responder
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def groups(self, uid):
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagenmove2whotxdavwhoaugmentpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/txdav/who/augment.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/txdav/who/augment.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/txdav/who/augment.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -54,7 +54,6 @@
</span><span class="cx">         augmented = []
</span><span class="cx">         txn = self.service._store.newTransaction()
</span><span class="cx">         groupUIDs = yield txn.groupsFor(self.uid)
</span><del>-        print(&quot;XYZZY GROU UIDS&quot;, groupUIDs)
</del><span class="cx">         for groupUID in groupUIDs:
</span><span class="cx">             groupRecord = yield self.service.recordWithShortName(
</span><span class="cx">                 RecordType.group, groupUID
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagenmove2whotxdavwhodelegatespy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/txdav/who/delegates.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/txdav/who/delegates.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/txdav/who/delegates.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -25,7 +25,9 @@
</span><span class="cx">     DirectoryRecord as BaseDirectoryRecord
</span><span class="cx"> )
</span><span class="cx"> from twext.who.expression import MatchExpression, MatchType
</span><del>-from twext.who.idirectory import RecordType as BaseRecordType, FieldName
</del><ins>+from twext.who.idirectory import (
+    RecordType as BaseRecordType, FieldName, NotAllowedError
+)
</ins><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</span><span class="cx"> from twisted.python.constants import Names, NamedConstant
</span><span class="cx"> 
</span><span class="lines">@@ -64,19 +66,6 @@
</span><span class="cx">         the members will consist of the records who have delegated *to*
</span><span class="cx">         this record.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        # Here are some delegate assignments to test with:
-        # txn = self.service._store.newTransaction()
-        # yield txn.addDelegate(u&quot;E415DBA7-40B5-49F5-A7CC-ACC81E4DEC79&quot;, u&quot;494E462A-B16A-4A90-B77F-B9019DD73DAA&quot;, True)
-        # yield txn.addDelegate(u&quot;__wsanchez__&quot;, u&quot;__cdaboo__&quot;, True)
-        # yield txn.addDelegate(u&quot;__wsanchez__&quot;, u&quot;__dre__&quot;, False)
-        # groupID, name, membershipHash = (
-        #     yield txn.groupByUID(u&quot;494E462A-B16A-4A90-B77F-B9019DD73DAA&quot;)
-        # )
-        # print(&quot;XYZZY&quot;, groupID, name, membershipHash)
-        # yield txn.addDelegateGroup(u&quot;E415DBA7-40B5-49F5-A7CC-ACC81E4DEC79&quot;, groupID, False)
-        # yield txn.commit()
-        ###
-
</del><span class="cx">         parentUID, proxyType = self.uid.split(&quot;#&quot;)
</span><span class="cx">         txn = self.service._store.newTransaction()
</span><span class="cx"> 
</span><span class="lines">@@ -105,6 +94,45 @@
</span><span class="cx">         returnValue(records)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
+    @inlineCallbacks
+    def setMembers(self, memberRecords):
+        &quot;&quot;&quot;
+        Replace the members of this group with the new members.
+
+        @param memberRecords: The new members of the group
+        @type memberRecords: iterable of L{iDirectoryRecord}s
+        &quot;&quot;&quot;
+
+        if self.recordType not in (
+            RecordType.readDelegateGroup, RecordType.writeDelegateGroup
+        ):
+            raise NotAllowedError(&quot;Setting members not supported&quot;)
+
+        parentUID, proxyType = self.uid.split(&quot;#&quot;)
+        readWrite = (self.recordType is RecordType.writeDelegateGroup)
+
+        log.debug(
+            &quot;Setting delegate assignments for {u} ({rw}) to {m}&quot;.format(
+                u=parentUID, rw=(&quot;write&quot; if readWrite else &quot;read&quot;),
+                m=[r.uid for r in memberRecords]
+            )
+        )
+
+        txn = self.service._store.newTransaction()
+
+        yield txn.removeDelegates(parentUID, readWrite)
+        yield txn.removeDelegateGroups(parentUID, readWrite)
+
+        delegator = yield self.service._masterDirectory.recordWithUID(parentUID)
+
+        for delegate in memberRecords:
+            yield addDelegate(txn, delegator, delegate, readWrite)
+
+        yield txn.commit()
+
+
+
</ins><span class="cx"> def recordTypeToProxyType(recordType):
</span><span class="cx">     return {
</span><span class="cx">         RecordType.readDelegateGroup: &quot;calendar-proxy-read&quot;,
</span><span class="lines">@@ -276,11 +304,3 @@
</span><span class="cx">             if record is not None:
</span><span class="cx">                 records.append(record)
</span><span class="cx">     returnValue(records)
</span><del>-
-
-def allGroupDelegates(txn):
-    &quot;&quot;&quot;
-    @return: the UIDs of all groups which are currently delegated to
-    @rtype: a Deferred which fires with a set() of UIDs C{unicode}
-    &quot;&quot;&quot;
-    return txn.allGroupDelegates()
</del></span></pre></div>
<a id="CalendarServerbranchesuserssagenmove2whotxdavwhogroupspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/txdav/who/groups.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/txdav/who/groups.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/txdav/who/groups.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -22,7 +22,6 @@
</span><span class="cx"> from twext.enterprise.dal.record import fromTable
</span><span class="cx"> from twext.enterprise.dal.syntax import Delete, Select
</span><span class="cx"> from twext.enterprise.jobqueue import WorkItem, PeerConnectionPool
</span><del>-from txdav.who.delegates import allGroupDelegates
</del><span class="cx"> from twext.who.idirectory import RecordType
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue
</span><span class="cx"> from txdav.common.datastore.sql_tables import schema
</span><span class="lines">@@ -447,7 +446,7 @@
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def groupsToRefresh(self, txn):
</span><del>-        delegatedUIDs = set((yield allGroupDelegates(txn)))
</del><ins>+        delegatedUIDs = set((yield txn.allGroupDelegates()))
</ins><span class="cx">         self.log.info(
</span><span class="cx">             &quot;There are {count} group delegates&quot;, count=len(delegatedUIDs)
</span><span class="cx">         )
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagenmove2whotxdavwhotesttest_delegatespy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/move2who/txdav/who/test/test_delegates.py (12828 => 12829)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/sagen/move2who/txdav/who/test/test_delegates.py        2014-03-06 04:17:29 UTC (rev 12828)
+++ CalendarServer/branches/users/sagen/move2who/txdav/who/test/test_delegates.py        2014-03-06 04:18:19 UTC (rev 12829)
</span><span class="lines">@@ -19,14 +19,14 @@
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx"> from txdav.who.delegates import (
</span><del>-    addDelegate, removeDelegate, delegatesOf, delegatedTo, allGroupDelegates
</del><ins>+    addDelegate, removeDelegate, delegatesOf, delegatedTo,
+    DirectoryService, RecordType as DelegateRecordType
</ins><span class="cx"> )
</span><span class="cx"> from txdav.who.groups import GroupCacher
</span><span class="cx"> from twext.who.idirectory import RecordType
</span><span class="cx"> from twext.who.test.test_xml import xmlService
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks
</span><span class="cx"> from twistedcaldav.test.util import StoreTestCase
</span><del>-from uuid import UUID
</del><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> class DelegationTest(StoreTestCase):
</span><span class="lines">@@ -34,14 +34,19 @@
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def setUp(self):
</span><span class="cx">         yield super(DelegationTest, self).setUp()
</span><ins>+        self.store = self.storeUnderTest()
</ins><span class="cx">         self.xmlService = xmlService(self.mktemp(), xmlData=testXMLConfig)
</span><span class="cx">         self.groupCacher = GroupCacher(self.xmlService)
</span><ins>+        self.delegateService = DirectoryService(
+            self.xmlService.realmName,
+            self.store
+        )
+        self.delegateService.setMasterDirectory(self.xmlService)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_directDelegation(self):
</span><del>-        store = self.storeUnderTest()
-        txn = store.newTransaction()
</del><ins>+        txn = self.store.newTransaction()
</ins><span class="cx"> 
</span><span class="cx">         delegator = yield self.xmlService.recordWithUID(u&quot;__wsanchez__&quot;)
</span><span class="cx">         delegate1 = yield self.xmlService.recordWithUID(u&quot;__sagen__&quot;)
</span><span class="lines">@@ -50,24 +55,68 @@
</span><span class="cx">         # Add 1 delegate
</span><span class="cx">         yield addDelegate(txn, delegator, delegate1, True)
</span><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True))
</span><del>-        self.assertEquals([&quot;sagen&quot;], [d.shortNames[0] for d in delegates])
</del><ins>+        self.assertEquals([&quot;__sagen__&quot;], [d.uid for d in delegates])
</ins><span class="cx">         delegators = (yield delegatedTo(txn, delegate1, True))
</span><del>-        self.assertEquals([&quot;wsanchez&quot;], [d.shortNames[0] for d in delegators])
</del><ins>+        self.assertEquals([&quot;__wsanchez__&quot;], [d.uid for d in delegators])
</ins><span class="cx"> 
</span><ins>+        yield txn.commit()  # So delegateService will see the changes
+        txn = self.store.newTransaction()
+
+        # The &quot;proxy-write&quot; pseudoGroup will have one member
+        pseudoGroup = yield self.delegateService.recordWithShortName(
+            DelegateRecordType.writeDelegateGroup,
+            u&quot;__wsanchez__&quot;
+        )
+        self.assertEquals(pseudoGroup.uid, u&quot;__wsanchez__#calendar-proxy-write&quot;)
+        self.assertEquals(
+            [r.uid for r in (yield pseudoGroup.members())],
+            [u&quot;__sagen__&quot;]
+        )
+        # The &quot;proxy-read&quot; pseudoGroup will have no members
+        pseudoGroup = yield self.delegateService.recordWithShortName(
+            DelegateRecordType.readDelegateGroup,
+            u&quot;__wsanchez__&quot;
+        )
+        self.assertEquals(pseudoGroup.uid, u&quot;__wsanchez__#calendar-proxy-read&quot;)
+        self.assertEquals(
+            [r.uid for r in (yield pseudoGroup.members())],
+            []
+        )
+        # The &quot;proxy-write-for&quot; pseudoGroup will have one member
+        pseudoGroup = yield self.delegateService.recordWithShortName(
+            DelegateRecordType.writeDelegatorGroup,
+            u&quot;__sagen__&quot;
+        )
+        self.assertEquals(pseudoGroup.uid, u&quot;__sagen__#calendar-proxy-write-for&quot;)
+        self.assertEquals(
+            [r.uid for r in (yield pseudoGroup.members())],
+            [u&quot;__wsanchez__&quot;]
+        )
+        # The &quot;proxy-read-for&quot; pseudoGroup will have no members
+        pseudoGroup = yield self.delegateService.recordWithShortName(
+            DelegateRecordType.readDelegatorGroup,
+            u&quot;__sagen__&quot;
+        )
+        self.assertEquals(pseudoGroup.uid, u&quot;__sagen__#calendar-proxy-read-for&quot;)
+        self.assertEquals(
+            [r.uid for r in (yield pseudoGroup.members())],
+            []
+        )
+
</ins><span class="cx">         # Add another delegate
</span><span class="cx">         yield addDelegate(txn, delegator, delegate2, True)
</span><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True))
</span><span class="cx">         self.assertEquals(
</span><del>-            set([&quot;sagen&quot;, &quot;cdaboo&quot;]),
-            set([d.shortNames[0] for d in delegates])
</del><ins>+            set([&quot;__sagen__&quot;, &quot;__cdaboo__&quot;]),
+            set([d.uid for d in delegates])
</ins><span class="cx">         )
</span><span class="cx">         delegators = (yield delegatedTo(txn, delegate2, True))
</span><del>-        self.assertEquals([&quot;wsanchez&quot;], [d.shortNames[0] for d in delegators])
</del><ins>+        self.assertEquals([&quot;__wsanchez__&quot;], [d.uid for d in delegators])
</ins><span class="cx"> 
</span><span class="cx">         # Remove 1 delegate
</span><span class="cx">         yield removeDelegate(txn, delegator, delegate1, True)
</span><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True))
</span><del>-        self.assertEquals([&quot;cdaboo&quot;], [d.shortNames[0] for d in delegates])
</del><ins>+        self.assertEquals([&quot;__cdaboo__&quot;], [d.uid for d in delegates])
</ins><span class="cx">         delegators = (yield delegatedTo(txn, delegate1, True))
</span><span class="cx">         self.assertEquals(0, len(delegators))
</span><span class="cx"> 
</span><span class="lines">@@ -78,11 +127,40 @@
</span><span class="cx">         delegators = (yield delegatedTo(txn, delegate2, True))
</span><span class="cx">         self.assertEquals(0, len(delegators))
</span><span class="cx"> 
</span><ins>+        yield txn.commit()  # So delegateService will see the changes
</ins><span class="cx"> 
</span><ins>+        # Now set delegate assignments by using pseudoGroup.setMembers()
+        pseudoGroup = yield self.delegateService.recordWithShortName(
+            DelegateRecordType.writeDelegateGroup,
+            u&quot;__wsanchez__&quot;
+        )
+        yield pseudoGroup.setMembers([delegate1, delegate2])
+
+        # Verify the assignments were made
+        txn = self.store.newTransaction()
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(
+            set([&quot;__sagen__&quot;, &quot;__cdaboo__&quot;]),
+            set([d.uid for d in delegates])
+        )
+        yield txn.commit()
+
+        # Set a different group of assignments:
+        yield pseudoGroup.setMembers([delegate2])
+
+        # Verify the assignments were made
+        txn = self.store.newTransaction()
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(
+            set([&quot;__cdaboo__&quot;]),
+            set([d.uid for d in delegates])
+        )
+        yield txn.commit()
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_indirectDelegation(self):
</span><del>-        store = self.storeUnderTest()
-        txn = store.newTransaction()
</del><ins>+        txn = self.store.newTransaction()
</ins><span class="cx"> 
</span><span class="cx">         delegator = yield self.xmlService.recordWithUID(u&quot;__wsanchez__&quot;)
</span><span class="cx">         delegate1 = yield self.xmlService.recordWithUID(u&quot;__sagen__&quot;)
</span><span class="lines">@@ -108,15 +186,15 @@
</span><span class="cx">         yield self.groupCacher.refreshGroup(txn, group2.uid)
</span><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True, expanded=True))
</span><span class="cx">         self.assertEquals(
</span><del>-            set([&quot;sagen&quot;, &quot;cdaboo&quot;, &quot;glyph&quot;]),
-            set([d.shortNames[0] for d in delegates])
</del><ins>+            set([&quot;__sagen__&quot;, &quot;__cdaboo__&quot;, &quot;__glyph__&quot;]),
+            set([d.uid for d in delegates])
</ins><span class="cx">         )
</span><span class="cx">         delegators = (yield delegatedTo(txn, delegate1, True))
</span><del>-        self.assertEquals([&quot;wsanchez&quot;], [d.shortNames[0] for d in delegators])
</del><ins>+        self.assertEquals([&quot;__wsanchez__&quot;], [d.uid for d in delegators])
</ins><span class="cx"> 
</span><span class="cx">         # Verify we can ask for all delegated-to groups
</span><span class="cx">         yield addDelegate(txn, delegator, group2, True)
</span><del>-        groups = (yield allGroupDelegates(txn))
</del><ins>+        groups = (yield txn.allGroupDelegates())
</ins><span class="cx">         self.assertEquals(
</span><span class="cx">             set([u'__sub_group_1__', u'__top_group_1__']), set(groups)
</span><span class="cx">         )
</span><span class="lines">@@ -125,8 +203,8 @@
</span><span class="cx">         yield addDelegate(txn, delegator, delegate1, True)
</span><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True, expanded=True))
</span><span class="cx">         self.assertEquals(
</span><del>-            set([&quot;sagen&quot;, &quot;cdaboo&quot;, &quot;glyph&quot;]),
-            set([d.shortNames[0] for d in delegates])
</del><ins>+            set([&quot;__sagen__&quot;, &quot;__cdaboo__&quot;, &quot;__glyph__&quot;]),
+            set([d.uid for d in delegates])
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">         # Add a member to the group; they become a delegate
</span><span class="lines">@@ -142,28 +220,31 @@
</span><span class="cx">         )
</span><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True, expanded=True))
</span><span class="cx">         self.assertEquals(
</span><del>-            set([&quot;sagen&quot;, &quot;cdaboo&quot;, &quot;glyph&quot;, &quot;dre&quot;]),
-            set([d.shortNames[0] for d in delegates])
</del><ins>+            set([&quot;__sagen__&quot;, &quot;__cdaboo__&quot;, &quot;__glyph__&quot;, &quot;__dre__&quot;]),
+            set([d.uid for d in delegates])
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">         # Remove delegate access from the top group
</span><span class="cx">         yield removeDelegate(txn, delegator, group1, True)
</span><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True, expanded=True))
</span><span class="cx">         self.assertEquals(
</span><del>-            set([&quot;sagen&quot;, &quot;cdaboo&quot;]),
-            set([d.shortNames[0] for d in delegates])
</del><ins>+            set([&quot;__sagen__&quot;, &quot;__cdaboo__&quot;]),
+            set([d.uid for d in delegates])
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">         # Remove delegate access from the sub group
</span><span class="cx">         yield removeDelegate(txn, delegator, group2, True)
</span><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True, expanded=True))
</span><span class="cx">         self.assertEquals(
</span><del>-            set([&quot;sagen&quot;]),
-            set([d.shortNames[0] for d in delegates])
</del><ins>+            set([&quot;__sagen__&quot;]),
+            set([d.uid for d in delegates])
</ins><span class="cx">         )
</span><ins>+        yield txn.commit()
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> testXMLConfig = &quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
</span><span class="cx"> 
</span><span class="cx"> &lt;directory realm=&quot;xyzzy&quot;&gt;
</span></span></pre>
</div>
</div>

</body>
</html>