1111
1212use OCA \DAV \CalDAV \Federation \Protocol \CalendarFederationProtocolV1 ;
1313use OCA \DAV \DAV \RemoteUserPrincipalBackend ;
14+ use OCA \DAV \DAV \Sharing \Backend ;
1415use OCA \DAV \DAV \Sharing \IShareable ;
1516use OCA \DAV \DAV \Sharing \SharingMapper ;
1617use OCP \AppFramework \Http ;
@@ -62,7 +63,7 @@ private function decodeRemoteUserPrincipal(string $principal): ?string {
6263
6364 /**
6465 * Send a calendar share to a remote instance and create a federated share locally if it is
65- * accepted.
66+ * accepted. Downgrading an existing share to read-only is applied locally in any case.
6667 *
6768 * @param IShareable $shareable The calendar to be shared.
6869 * @param string $principal The principal to share with (should be a remote user principal).
@@ -71,14 +72,6 @@ private function decodeRemoteUserPrincipal(string $principal): ?string {
7172 public function shareWith (IShareable $ shareable , string $ principal , int $ access ): void {
7273 $ baseError = 'Failed to create federated calendar share: ' ;
7374
74- if (!$ this ->config ->isOutgoingServer2serverShareEnabled ()) {
75- $ this ->logger ->error ('cannot share with remote user because federated sharing is disabled on this instance ' , [
76- 'shareable ' => $ shareable ->getName (),
77- 'encodedShareWith ' => $ principal ,
78- ]);
79- return ;
80- }
81-
8275 // 1. Validate share data
8376 $ shareWith = $ this ->decodeRemoteUserPrincipal ($ principal );
8477 if ($ shareWith === null ) {
@@ -114,7 +107,43 @@ public function shareWith(IShareable $shareable, string $principal, int $access)
114107
115108 $ displayName = $ getProp ('{DAV:}displayname ' ) ?? '' ;
116109
117- $ token = $ this ->random ->generate (32 );
110+ // The remote server only accepts changes to an existing share that carry its current token
111+ $ shareWithEncoded = base64_encode ($ shareWith );
112+ $ shareWithPrincipalUri = RemoteUserPrincipalBackend::PRINCIPAL_PREFIX . '/ ' . $ shareWithEncoded ;
113+ $ existingShares = $ this ->sharingMapper ->getSharesByPrincipalsAndResource (
114+ [$ shareWithPrincipalUri ],
115+ $ shareable ->getResourceId (),
116+ 'calendar ' ,
117+ );
118+ $ existingShare = $ existingShares [0 ] ?? null ;
119+
120+ // A downgrade to read-only must not depend on the remote server accepting the new offer
121+ if ($ existingShare !== null
122+ && $ access === Backend::ACCESS_READ
123+ && (int )$ existingShare ['access ' ] !== Backend::ACCESS_READ ) {
124+ $ this ->sharingMapper ->deleteShare (
125+ $ shareable ->getResourceId (),
126+ 'calendar ' ,
127+ $ shareWithPrincipalUri ,
128+ );
129+ $ this ->sharingMapper ->shareWithToken (
130+ $ shareable ->getResourceId (),
131+ 'calendar ' ,
132+ Backend::ACCESS_READ ,
133+ $ shareWithPrincipalUri ,
134+ $ existingShare ['token ' ],
135+ );
136+ }
137+
138+ if (!$ this ->config ->isOutgoingServer2serverShareEnabled ()) {
139+ $ this ->logger ->error ('cannot share with remote user because federated sharing is disabled on this instance ' , [
140+ 'shareable ' => $ shareable ->getName (),
141+ 'encodedShareWith ' => $ principal ,
142+ ]);
143+ return ;
144+ }
145+
146+ $ token = $ existingShare ['token ' ] ?? $ this ->random ->generate (32 );
118147 $ share = $ this ->federationFactory ->getCloudFederationShare (
119148 $ shareWith ,
120149 $ shareable ->getName (),
@@ -131,7 +160,6 @@ public function shareWith(IShareable $shareable, string $principal, int $access)
131160 );
132161
133162 // 2. Send share to federated instance
134- $ shareWithEncoded = base64_encode ($ shareWith );
135163 $ relativeCalendarUrl = "remote-calendars/ $ shareWithEncoded/ " . $ calendar ->getName () . '_shared_by_ ' . $ ownerUid ;
136164 $ calendarUrl = $ this ->url ->linkTo ('' , 'remote.php ' ) . "/dav/ $ relativeCalendarUrl " ;
137165 $ calendarUrl = $ this ->url ->getAbsoluteURL ($ calendarUrl );
@@ -172,7 +200,6 @@ public function shareWith(IShareable $shareable, string $principal, int $access)
172200 }
173201
174202 // 3. Create a local DAV share to track the token for authentication
175- $ shareWithPrincipalUri = RemoteUserPrincipalBackend::PRINCIPAL_PREFIX . '/ ' . $ shareWithEncoded ;
176203 $ this ->sharingMapper ->deleteShare (
177204 $ shareable ->getResourceId (),
178205 'calendar ' ,
0 commit comments