3232use OCP \IL10N ;
3333use OCP \Share \Exceptions \GenericShareException ;
3434use OCP \Share \Exceptions \ShareNotFound ;
35+ use OCP \Share \IAttributes ;
3536use OCP \Share \IManager ;
3637use OCP \Share \IShare ;
3738
@@ -113,6 +114,11 @@ public function create(IShare $share) {
113114 )
114115 );*/
115116
117+ // set share attributes
118+ $ shareAttributes = $ this ->formatShareAttributes (
119+ $ share ->getAttributes ()
120+ );
121+
116122 $ shareId = $ this ->addShareToDB (
117123 $ share ->getSharedWith (),
118124 $ share ->getSharedBy (),
@@ -122,7 +128,8 @@ public function create(IShare $share) {
122128 $ share ->getTarget (),
123129 $ share ->getPermissions (),
124130 $ share ->getToken () ?? '' ,
125- $ share ->getExpirationDate ()
131+ $ share ->getExpirationDate (),
132+ $ shareAttributes
126133 );
127134 $ data = $ this ->getRawShare ($ shareId );
128135
@@ -143,6 +150,7 @@ public function create(IShare $share) {
143150 * @param int $permissions
144151 * @param string $token
145152 * @param \DateTime|null $expirationDate
153+ * @param string|null $attributes
146154 * @return int
147155 */
148156 private function addShareToDB (
@@ -155,6 +163,7 @@ private function addShareToDB(
155163 int $ permissions ,
156164 string $ token ,
157165 ?\DateTime $ expirationDate ,
166+ ?string $ attributes = null ,
158167 ): int {
159168 $ qb = $ this ->dbConnection ->getQueryBuilder ();
160169 $ qb ->insert ('share ' )
@@ -174,6 +183,10 @@ private function addShareToDB(
174183 $ qb ->setValue ('expiration ' , $ qb ->createNamedParameter ($ expirationDate , 'datetime ' ));
175184 }
176185
186+ if ($ attributes !== null ) {
187+ $ qb ->setValue ('attributes ' , $ qb ->createNamedParameter ($ attributes ));
188+ }
189+
177190 $ qb ->executeStatement ();
178191
179192 return $ qb ->getLastInsertId ();
@@ -244,6 +257,9 @@ private function createShareObject(array $data): IShare {
244257 $ entryData ['parent ' ] = $ entryData ['f_parent ' ];
245258 $ share ->setNodeCacheEntry (Cache::cacheEntryFromData ($ entryData , $ this ->mimeTypeLoader ));
246259 }
260+
261+ $ share = $ this ->updateShareAttributes ($ share , $ data ['attributes ' ] ?? null );
262+
247263 return $ share ;
248264 }
249265
@@ -267,39 +283,62 @@ private function applyBoardPermission($share, $permissions, $userId) {
267283 * @inheritDoc
268284 */
269285 public function update (IShare $ share ) {
270- $ qb = $ this ->dbConnection ->getQueryBuilder ();
271- $ qb ->update ('share ' )
272- ->where ($ qb ->expr ()->eq ('id ' , $ qb ->createNamedParameter ($ share ->getId ())))
273- ->set ('uid_owner ' , $ qb ->createNamedParameter ($ share ->getShareOwner ()))
274- ->set ('uid_initiator ' , $ qb ->createNamedParameter ($ share ->getSharedBy ()))
275- ->set ('permissions ' , $ qb ->createNamedParameter ($ share ->getPermissions ()))
276- ->set ('item_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
277- ->set ('file_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
278- ->set ('expiration ' , $ qb ->createNamedParameter ($ share ->getExpirationDate (), IQueryBuilder::PARAM_DATE ))
279- ->execute ();
286+ $ this ->dbConnection ->beginTransaction ();
287+ try {
288+ $ qb = $ this ->dbConnection ->getQueryBuilder ();
289+ $ qb ->update ('share ' )
290+ ->where ($ qb ->expr ()->eq ('id ' , $ qb ->createNamedParameter ($ share ->getId ())))
291+ ->set ('uid_owner ' , $ qb ->createNamedParameter ($ share ->getShareOwner ()))
292+ ->set ('uid_initiator ' , $ qb ->createNamedParameter ($ share ->getSharedBy ()))
293+ ->set ('permissions ' , $ qb ->createNamedParameter ($ share ->getPermissions ()))
294+ ->set ('item_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
295+ ->set ('file_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
296+ ->set ('expiration ' , $ qb ->createNamedParameter ($ share ->getExpirationDate (), IQueryBuilder::PARAM_DATE ));
297+
298+ $ shareAttributes = $ this ->formatShareAttributes ($ share ->getAttributes ());
299+ if ($ shareAttributes !== null ) {
300+ $ qb ->set ('attributes ' , $ qb ->createNamedParameter ($ shareAttributes ));
301+ }
280302
281- /*
282- * Update all user defined group shares
283- */
284- $ qb = $ this ->dbConnection ->getQueryBuilder ();
285- $ qb ->update ('share ' )
286- ->where ($ qb ->expr ()->eq ('parent ' , $ qb ->createNamedParameter ($ share ->getId ())))
287- ->set ('uid_owner ' , $ qb ->createNamedParameter ($ share ->getShareOwner ()))
288- ->set ('uid_initiator ' , $ qb ->createNamedParameter ($ share ->getSharedBy ()))
289- ->set ('item_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
290- ->set ('file_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
291- ->set ('expiration ' , $ qb ->createNamedParameter ($ share ->getExpirationDate (), IQueryBuilder::PARAM_DATE ))
292- ->execute ();
303+ $ qb ->executeStatement ();
293304
294- /*
295- * Now update the permissions for all children that have not set it to 0
296- */
297- $ qb = $ this ->dbConnection ->getQueryBuilder ();
298- $ qb ->update ('share ' )
299- ->where ($ qb ->expr ()->eq ('parent ' , $ qb ->createNamedParameter ($ share ->getId ())))
300- ->andWhere ($ qb ->expr ()->neq ('permissions ' , $ qb ->createNamedParameter (0 )))
301- ->set ('permissions ' , $ qb ->createNamedParameter ($ share ->getPermissions ()))
302- ->execute ();
305+ /*
306+ * Update all user defined group shares
307+ */
308+ $ qb = $ this ->dbConnection ->getQueryBuilder ();
309+ $ qb ->update ('share ' )
310+ ->where ($ qb ->expr ()->eq ('parent ' , $ qb ->createNamedParameter ($ share ->getId ())))
311+ ->set ('uid_owner ' , $ qb ->createNamedParameter ($ share ->getShareOwner ()))
312+ ->set ('uid_initiator ' , $ qb ->createNamedParameter ($ share ->getSharedBy ()))
313+ ->set ('item_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
314+ ->set ('file_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
315+ ->set ('expiration ' , $ qb ->createNamedParameter ($ share ->getExpirationDate (), IQueryBuilder::PARAM_DATE ));
316+
317+ if ($ shareAttributes !== null ) {
318+ $ qb ->set ('attributes ' , $ qb ->createNamedParameter ($ shareAttributes ));
319+ }
320+
321+ $ qb ->executeStatement ();
322+
323+ /*
324+ * Now update the permissions for all children that have not set it to 0
325+ */
326+ $ qb = $ this ->dbConnection ->getQueryBuilder ();
327+ $ qb ->update ('share ' )
328+ ->where ($ qb ->expr ()->eq ('parent ' , $ qb ->createNamedParameter ($ share ->getId ())))
329+ ->andWhere ($ qb ->expr ()->neq ('permissions ' , $ qb ->createNamedParameter (0 )))
330+ ->set ('permissions ' , $ qb ->createNamedParameter ($ share ->getPermissions ()));
331+
332+ if ($ shareAttributes !== null ) {
333+ $ qb ->set ('attributes ' , $ qb ->createNamedParameter ($ shareAttributes ));
334+ }
335+
336+ $ qb ->executeStatement ();
337+ } catch (\Exception $ e ) {
338+ $ this ->dbConnection ->rollBack ();
339+ throw $ e ;
340+ }
341+ $ this ->dbConnection ->commit ();
303342
304343 return $ share ;
305344 }
@@ -1026,6 +1065,42 @@ public function getAllShares(): iterable {
10261065 $ cursor ->closeCursor ();
10271066 }
10281067
1068+ protected function updateShareAttributes (IShare $ share , ?string $ data ): IShare {
1069+ if ($ data === null || $ data === '' ) {
1070+ return $ share ;
1071+ }
1072+ $ attributes = $ share ->getAttributes () ?? $ share ->newAttributes ();
1073+ $ compressedAttributes = \json_decode ($ data , true );
1074+ if ($ compressedAttributes === false || $ compressedAttributes === null ) {
1075+ return $ share ;
1076+ }
1077+ foreach ($ compressedAttributes as $ compressedAttribute ) {
1078+ $ attributes ->setAttribute (
1079+ $ compressedAttribute [0 ],
1080+ $ compressedAttribute [1 ],
1081+ $ compressedAttribute [2 ]
1082+ );
1083+ }
1084+ $ share ->setAttributes ($ attributes );
1085+ return $ share ;
1086+ }
1087+
1088+ protected function formatShareAttributes (?IAttributes $ attributes ): ?string {
1089+ if ($ attributes === null || empty ($ attributes ->toArray ())) {
1090+ return null ;
1091+ }
1092+
1093+ $ compressedAttributes = [];
1094+ foreach ($ attributes ->toArray () as $ attribute ) {
1095+ $ compressedAttributes [] = [
1096+ 0 => $ attribute ['scope ' ],
1097+ 1 => $ attribute ['key ' ],
1098+ 2 => $ attribute ['value ' ]
1099+ ];
1100+ }
1101+ return \json_encode ($ compressedAttributes ) ?: null ;
1102+ }
1103+
10291104 public function getOrphanedAttachmentShares (): array {
10301105 $ allCardIds = $ this ->cardMapper ->getAllCardIds ();
10311106
0 commit comments