<!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>[13731] CalendarServer/branches/users/gaya/groupsharee2/txdav/caldav/datastore/sql.py</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/13731">13731</a></dd>
<dt>Author</dt> <dd>gaya@apple.com</dd>
<dt>Date</dt> <dd>2014-07-07 16:41:26 -0700 (Mon, 07 Jul 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>checkpoint, add high-level sharing interface</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesusersgayagroupsharee2txdavcaldavdatastoresqlpy">CalendarServer/branches/users/gaya/groupsharee2/txdav/caldav/datastore/sql.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesusersgayagroupsharee2txdavcaldavdatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/gaya/groupsharee2/txdav/caldav/datastore/sql.py (13730 => 13731)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/gaya/groupsharee2/txdav/caldav/datastore/sql.py        2014-07-07 16:52:57 UTC (rev 13730)
+++ CalendarServer/branches/users/gaya/groupsharee2/txdav/caldav/datastore/sql.py        2014-07-07 23:41:26 UTC (rev 13731)
</span><span class="lines">@@ -27,13 +27,8 @@
</span><span class="cx"> ]
</span><span class="cx">
</span><span class="cx"> from twext.enterprise.dal.record import fromTable
</span><del>-from twext.enterprise.dal.syntax import Delete
-from twext.enterprise.dal.syntax import Insert
-from twext.enterprise.dal.syntax import Len
-from twext.enterprise.dal.syntax import Parameter
-from twext.enterprise.dal.syntax import Select, Count, ColumnSyntax
-from twext.enterprise.dal.syntax import Update
-from twext.enterprise.dal.syntax import utcNowSQL
</del><ins>+from twext.enterprise.dal.syntax import Count, ColumnSyntax, Delete, \
+ Insert, Len, Max, Parameter, Select, Update, utcNowSQL
</ins><span class="cx"> from twext.enterprise.locking import NamedLock
</span><span class="cx"> from twext.enterprise.jobqueue import WorkItem
</span><span class="cx"> from twext.enterprise.util import parseSQLTimestamp
</span><span class="lines">@@ -89,7 +84,9 @@
</span><span class="cx"> from txdav.common.datastore.sql_tables import _ATTACHMENTS_MODE_NONE, \
</span><span class="cx"> _ATTACHMENTS_MODE_WRITE, schema, _BIND_MODE_OWN, \
</span><span class="cx"> _ATTACHMENTS_MODE_READ, _TRANSP_OPAQUE, _TRANSP_TRANSPARENT, \
</span><del>- _BIND_MODE_GROUP
</del><ins>+ _BIND_MODE_GROUP, _BIND_MODE_READ, _BIND_MODE_WRITE, _BIND_MODE_DIRECT, \
+ _BIND_STATUS_ACCEPTED, _BIND_STATUS_DECLINED, _BIND_STATUS_INVALID, \
+ _BIND_STATUS_INVITED, _BIND_STATUS_DELETED
</ins><span class="cx"> from txdav.common.icommondatastore import IndexedSearchException, \
</span><span class="cx"> InternalDataStoreError, HomeChildNameAlreadyExistsError, \
</span><span class="cx"> HomeChildNameNotAllowedError, ObjectResourceTooBigError, \
</span><span class="lines">@@ -97,7 +94,7 @@
</span><span class="cx"> ObjectResourceNameNotAllowedError, TooManyObjectResourcesError, \
</span><span class="cx"> InvalidUIDError, UIDExistsError, UIDExistsElsewhereError, \
</span><span class="cx"> InvalidResourceMove, InvalidComponentForStoreError, \
</span><del>- NoSuchObjectResourceError, ConcurrentModification, AllRetriesFailed
</del><ins>+ NoSuchObjectResourceError, ConcurrentModification
</ins><span class="cx"> from txdav.xml import element
</span><span class="cx"> from txdav.xml.parser import WebDAVDocument
</span><span class="cx">
</span><span class="lines">@@ -1781,28 +1778,29 @@
</span><span class="cx"> @return: the name of the shared calendar in the new calendar home.
</span><span class="cx"> @rtype: L{str}
</span><span class="cx"> """
</span><del>- record = (
- yield self._txn.directoryService().recordWithUID(shareeUID.decode("utf-8"))
- ) if True or (
- config.Sharing.Enabled and
- config.Sharing.Calendars.Enabled and
- config.Sharing.Calendars.Groups.Enabled
- ) else None
-
- if record is None or record.recordType != RecordType.group:
</del><ins>+ record = yield self._txn.directoryService().recordWithUID(shareeUID.decode("utf-8"))
+ if (
+ record is None or
+ record.recordType != RecordType.group or not (False and
+ config.Sharing.Enabled and
+ config.Sharing.Calendars.Enabled and
+ config.Sharing.Calendars.Groups.Enabled
+ )
+ ):
</ins><span class="cx"> shareeHome = yield self._txn.calendarHomeWithUID(shareeUID, create=True)
</span><span class="cx"> returnValue(
</span><span class="cx"> (yield self.shareWith(shareeHome, mode, status, summary, shareName))
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx"> # shareWith every member of group not already shared to
</span><del>- members = yield record.expandedMembers()
- for member in members:
- shareeHome = yield self._txn.calendarHomeWithUID(member.uid(), create=True)
</del><ins>+ groupID, _ignore_name, _ignore_membershipHash, _ignore_modDate = yield self._txn.groupByUID(record.uid)
+ memberUIDs = yield self._txn.membersOfGroup(groupID)
+ for memberUID in memberUIDs:
+ shareeHome = yield self._txn.calendarHomeWithUID(memberUID, create=True)
</ins><span class="cx"> if (yield shareeHome.childWithID(self._resourceID)) is None:
</span><span class="cx"> yield self.shareWith(shareeHome, _BIND_MODE_GROUP, status)
</span><span class="cx">
</span><del>- yield self.updateGroupLink(shareeUID, mode)
</del><ins>+ yield self.updateShareeGroupLink(shareeUID, mode)
</ins><span class="cx"> returnValue(None)
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -1815,13 +1813,9 @@
</span><span class="cx"> if (yield self.updateShareeGroupLink, groupUID):
</span><span class="cx"> record = (
</span><span class="cx"> yield self._txn.directoryService().recordWithUID(groupUID.decode("utf-8"))
</span><del>- ) if True or (
- config.Sharing.Enabled and
- config.Sharing.Calendars.Enabled and
- config.Sharing.Calendars.Groups.Enabled
- ) else None
- members = yield record.expandedMembers() if record is not None else []
- memberUIDs = set([member.uid() for member in members])
</del><ins>+ )
+ groupID, _ignore_name, _ignore_membershipHash, _ignore_modDate = yield self._txn.groupByUID(record.uid)
+ memberUIDs = yield self._txn.membersOfGroup(groupID)
</ins><span class="cx"> boundUIDs = set()
</span><span class="cx">
</span><span class="cx"> bind = schema.CALENDAR_BIND
</span><span class="lines">@@ -1842,8 +1836,8 @@
</span><span class="cx"> yield self.removeShare(shareeView)
</span><span class="cx"> changed = True
</span><span class="cx">
</span><del>- for member in [member for member in members if member.uid() not in boundUIDs]:
- shareeHome = yield self._txn.calendarHomeWithUID(member.uid(), create=True)
</del><ins>+ for memberUID in memberUIDs - boundUIDs:
+ shareeHome = yield self._txn.calendarHomeWithUID(memberUID, create=True)
</ins><span class="cx"> if (yield shareeHome.childWithID(self._resourceID)) is None:
</span><span class="cx"> yield self.shareWith(shareeHome, _BIND_MODE_GROUP)
</span><span class="cx"> changed = True
</span><span class="lines">@@ -1863,7 +1857,7 @@
</span><span class="cx"> rows = yield Select(
</span><span class="cx"> [gs.MEMBERSHIP_HASH, gs.MODE],
</span><span class="cx"> From=gs,
</span><del>- Where=gs.HOME_ID == self.ownerHome()._resourceID.And(
</del><ins>+ Where=(gs.CALENDAR_HOME_ID == self.ownerHome()._resourceID).And(
</ins><span class="cx"> gs.CALENDAR_ID == self._resourceID).And(
</span><span class="cx"> gs.GROUP_ID == groupID)
</span><span class="cx"> ).on(self._txn)
</span><span class="lines">@@ -1877,7 +1871,7 @@
</span><span class="cx"> if updateMap:
</span><span class="cx"> yield Update(
</span><span class="cx"> updateMap,
</span><del>- Where=gs.HOME_ID == self.ownerHome()._resourceID.And(
</del><ins>+ Where=(gs.CALENDAR_HOME_ID == self.ownerHome()._resourceID).And(
</ins><span class="cx"> gs.CALENDAR_ID == self._resourceID).And(
</span><span class="cx"> gs.GROUP_ID == groupID
</span><span class="cx"> )
</span><span class="lines">@@ -1887,7 +1881,7 @@
</span><span class="cx"> yield Insert({
</span><span class="cx"> gs.MEMBERSHIP_HASH: membershipHash,
</span><span class="cx"> gs.GROUP_BIND_MODE: mode,
</span><del>- gs.HOME_ID: self.ownerHome()._resourceID,
</del><ins>+ gs.CALENDAR_HOME_ID: self.ownerHome()._resourceID,
</ins><span class="cx"> gs.CALENDAR_ID: self._resourceID,
</span><span class="cx"> gs.GROUP_ID: groupID,
</span><span class="cx"> }).on(self._txn)
</span><span class="lines">@@ -1901,18 +1895,191 @@
</span><span class="cx"> if self._bindMode == _BIND_MODE_GROUP:
</span><span class="cx"> gs = schema.GROUP_SHAREE
</span><span class="cx"> rows = yield Select(
</span><del>- [gs.GROUP_BIND_MODE],
</del><ins>+ [Max(gs.GROUP_BIND_MODE)], # _BIND_MODE_WRITE > _BIND_MODE_READ
</ins><span class="cx"> From=gs,
</span><del>- Where=gs.HOME_ID == self.ownerHome()._resourceID.And(
</del><ins>+ Where=(gs.CALENDAR_HOME_ID == self.ownerHome()._resourceID).And(
</ins><span class="cx"> gs.CALENDAR_ID == self._resourceID
</span><span class="cx"> )
</span><span class="cx"> ).on(self._txn)
</span><del>- groupShareMode = rows[0][0] if rows else None
</del><ins>+ groupShareMode = rows[0][0]
</ins><span class="cx"> returnValue(groupShareMode)
</span><span class="cx"> else:
</span><span class="cx"> returnValue(self._bindMode)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @inlineCallbacks
+ def removeShare(self, shareeView):
+ """
+ Remove the shared version of this (owned) L{CommonHomeChild} from the
+ referenced L{CommonHome}.
+
+ If user share and user is in shared group, change to group share.
+
+ @see: L{CommonHomeChild.shareWith}
+
+ @param shareeView: The shared resource being removed.
+
+ @return: a L{Deferred} which will fire with the previous shareUID
+ """
+
+ # see if after share is removed, user is still shared by a group sharee
+ effectiveShareMode = None
+ oldShareMode = shareeView.shareMode()
+ if oldShareMode in (_BIND_MODE_DIRECT, _BIND_MODE_READ, _BIND_MODE_WRITE, _BIND_MODE_GROUP,):
+
+ gs = schema.GROUP_SHAREE
+ rows = yield Select(
+ [gs.GROUP_ID, gs.GROUP_BIND_MODE],
+ From=gs,
+ Where=(gs.CALENDAR_HOME_ID == self.ownerHome()._resourceID).And(
+ gs.CALENDAR_ID == self._resourceID)
+ ).on(self._txn)
+ shareeHomeUID = shareeView.viewerHome().uid()
+ for groupID, groupShareMode in rows:
+ memberUIDs = yield self._txn.membersOfGroup(groupID)
+ if shareeHomeUID in memberUIDs:
+ if effectiveShareMode is None:
+ effectiveShareMode = groupShareMode
+ elif groupShareMode > effectiveShareMode:
+ effectiveShareMode = groupShareMode
+
+ if effectiveShareMode is None:
+ # no group sharee for this user so let super do work
+ returnValue((yield super(Calendar, self).removeShare(shareeView)))
+ elif oldShareMode != _BIND_MODE_GROUP:
+ # change to group share
+ yield self.updateShare(shareeView, mode=_BIND_MODE_GROUP)
+ returnValue(None)
+
+ #TODO: effectiveShareMode may change between _BIND_MODE_READ & _BIND_MODE_READ.
+ # Is that OK?
+
+
+ @inlineCallbacks
+ def removeShareWithUID(self, shareeUID):
+ """
+ Unshare this (owned) L{CommonHomeChild} with another principal.
+
+ @param shareeUID: The UID of the sharee.
+ @type: L{str}
+ """
+ record = yield self._txn.directoryService().recordWithUID(shareeUID.decode("utf-8"))
+ if (
+ record is None or
+ record.recordType != RecordType.group or not (False and
+ config.Sharing.Enabled and
+ config.Sharing.Calendars.Enabled and
+ config.Sharing.Calendars.Groups.Enabled
+ )
+ ):
+ returnValue(None)
+
+ # get group membership
+ groupID, _ignore_name, _ignore_membershipHash, _ignore_modDate = yield self._txn.groupByUID(record.uid)
+ memberUIDs = yield self._txn.membersOfGroup(groupID)
+
+ # update groupsharee so that removeShare works
+ gs = schema.GROUP_SHAREE
+ yield Delete(
+ From=gs,
+ Where=(gs.CALENDAR_HOME_ID == self.ownerHome()._resourceID).And(
+ gs.CALENDAR_ID == self._resourceID).And(
+ gs.GROUP_ID == groupID)
+ ).on(self._txn)
+
+ for memberUID in memberUIDs:
+ shareeHome = yield self._txn.calendarHomeWithUID(memberUID, create=True)
+ shareeView = yield shareeHome.childWithID(self._resourceID)
+ yield self.removeShare(shareeView)
+
+
+ #
+ # Higher level API
+ #
+ @inlineCallbacks
+ def inviteUserToShare(self, shareeUID, mode, summary, shareName=None):
+ """
+ Invite a user to share this collection - either create the share if it does not exist, or
+ update the existing share with new values. Make sure a notification is sent as well.
+
+ @param shareeUID: UID of the sharee
+ @type shareeUID: C{str}
+ @param mode: access mode
+ @type mode: C{int}
+ @param summary: share message
+ @type summary: C{str}
+ """
+
+ # Look for existing invite and update its fields or create new one
+ shareeView = yield self.shareeView(shareeUID)
+ if shareeView is not None:
+ status = _BIND_STATUS_INVITED if shareeView.shareStatus() in (_BIND_STATUS_DECLINED, _BIND_STATUS_INVALID) else None
+ yield self.updateShare(shareeView, mode=mode, status=status, summary=summary)
+ else:
+ shareeView = yield self.createShare(shareeUID=shareeUID, mode=mode, summary=summary, shareName=shareName)
+
+ # Check for external
+ if shareeView.viewerHome().external():
+ yield self._sendExternalInvite(shareeView)
+ else:
+ # Send invite notification
+ yield self._sendInviteNotification(shareeView)
+ returnValue(shareeView)
+
+
+ @inlineCallbacks
+ def directShareWithUser(self, shareeUID, shareName=None):
+ """
+ Create a direct share with the specified user. Note it is currently up to the app layer
+ to enforce access control - this is not ideal as we really should have control of that in
+ the store. Once we do, this api will need to verify that access is allowed for a direct share.
+
+ NB no invitations are used with direct sharing.
+
+ @param shareeUID: UID of the sharee
+ @type shareeUID: C{str}
+ """
+
+ # Ignore if it already exists
+ shareeView = yield self.shareeView(shareeUID)
+ if shareeView is None:
+ shareeView = yield self.createShare(shareeUID=shareeUID, mode=_BIND_MODE_DIRECT, shareName=shareName)
+ yield shareeView.newShare()
+
+ # Check for external
+ if shareeView.viewerHome().external():
+ yield self._sendExternalInvite(shareeView)
+
+ returnValue(shareeView)
+
+
+ @inlineCallbacks
+ def uninviteUserFromShare(self, shareeUID):
+ """
+ Remove a user from a share. Make sure a notification is sent as well.
+
+ @param shareeUID: UID of the sharee
+ @type shareeUID: C{str}
+ """
+ # Cancel invites - we'll just use whatever userid we are given
+
+ shareeView = yield self.shareeView(shareeUID)
+ if shareeView is not None:
+ if shareeView.viewerHome().external():
+ yield self._sendExternalUninvite(shareeView)
+ else:
+ # If current user state is accepted then we send an invite with the new state, otherwise
+ # we cancel any existing invites for the user
+ if not shareeView.direct():
+ if shareeView.shareStatus() != _BIND_STATUS_ACCEPTED:
+ yield self._removeInviteNotification(shareeView)
+ else:
+ yield self._sendInviteNotification(shareeView, notificationState=_BIND_STATUS_DELETED)
+
+ # Remove the bind
+ yield self.removeShare(shareeView)
+
+
</ins><span class="cx"> icalfbtype_to_indexfbtype = {
</span><span class="cx"> "UNKNOWN" : 0,
</span><span class="cx"> "FREE" : 1,
</span><span class="lines">@@ -2112,7 +2279,12 @@
</span><span class="cx">
</span><span class="cx"> groupRecord = yield self.directoryService().recordWithCalendarUserAddress(groupCUA)
</span><span class="cx"> if groupRecord:
</span><del>- members = yield groupRecord.expandedMembers()
</del><ins>+ # get members from cached membership
+ groupID, _ignore_name, _ignore_membershipHash, _ignore_modDate = yield self._txn.groupByUID(groupRecord.uid)
+ members = [(yield self.directoryService().recordWithUID(memberUID))
+ for memberUID in
+ (yield self._txn.membersOfGroup(groupID))
+ ]
</ins><span class="cx"> groupCUAToAttendeeMemberPropMap[groupRecord.canonicalCalendarUserAddress()] = tuple(
</span><span class="cx"> [member.attendeeProperty(params={"MEMBER": groupCUA}) for member in sorted(members, key=lambda x: x.uid)]
</span><span class="cx"> )
</span></span></pre>
</div>
</div>
</body>
</html>