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
@@ -275,8 +291,14 @@ public function update(IShare $share) {
275291 ->set ('permissions ' , $ qb ->createNamedParameter ($ share ->getPermissions ()))
276292 ->set ('item_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
277293 ->set ('file_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
278- ->set ('expiration ' , $ qb ->createNamedParameter ($ share ->getExpirationDate (), IQueryBuilder::PARAM_DATE ))
279- ->execute ();
294+ ->set ('expiration ' , $ qb ->createNamedParameter ($ share ->getExpirationDate (), IQueryBuilder::PARAM_DATE ));
295+
296+ $ shareAttributes = $ this ->formatShareAttributes ($ share ->getAttributes ());
297+ if ($ shareAttributes !== null ) {
298+ $ qb ->set ('attributes ' , $ qb ->createNamedParameter ($ shareAttributes ));
299+ }
300+
301+ $ qb ->executeStatement ();
280302
281303 /*
282304 * Update all user defined group shares
@@ -288,8 +310,13 @@ public function update(IShare $share) {
288310 ->set ('uid_initiator ' , $ qb ->createNamedParameter ($ share ->getSharedBy ()))
289311 ->set ('item_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
290312 ->set ('file_source ' , $ qb ->createNamedParameter ($ share ->getNode ()->getId ()))
291- ->set ('expiration ' , $ qb ->createNamedParameter ($ share ->getExpirationDate (), IQueryBuilder::PARAM_DATE ))
292- ->execute ();
313+ ->set ('expiration ' , $ qb ->createNamedParameter ($ share ->getExpirationDate (), IQueryBuilder::PARAM_DATE ));
314+
315+ if ($ shareAttributes !== null ) {
316+ $ qb ->set ('attributes ' , $ qb ->createNamedParameter ($ shareAttributes ));
317+ }
318+
319+ $ qb ->executeStatement ();
293320
294321 /*
295322 * Now update the permissions for all children that have not set it to 0
@@ -298,8 +325,13 @@ public function update(IShare $share) {
298325 $ qb ->update ('share ' )
299326 ->where ($ qb ->expr ()->eq ('parent ' , $ qb ->createNamedParameter ($ share ->getId ())))
300327 ->andWhere ($ qb ->expr ()->neq ('permissions ' , $ qb ->createNamedParameter (0 )))
301- ->set ('permissions ' , $ qb ->createNamedParameter ($ share ->getPermissions ()))
302- ->execute ();
328+ ->set ('permissions ' , $ qb ->createNamedParameter ($ share ->getPermissions ()));
329+
330+ if ($ shareAttributes !== null ) {
331+ $ qb ->set ('attributes ' , $ qb ->createNamedParameter ($ shareAttributes ));
332+ }
333+
334+ $ qb ->executeStatement ();
303335
304336 return $ share ;
305337 }
@@ -1026,6 +1058,42 @@ public function getAllShares(): iterable {
10261058 $ cursor ->closeCursor ();
10271059 }
10281060
1061+ protected function updateShareAttributes (IShare $ share , ?string $ data ): IShare {
1062+ if ($ data !== null && $ data !== '' ) {
1063+ $ attributes = $ share ->getAttributes () ?? $ share ->newAttributes ();
1064+ $ compressedAttributes = \json_decode ($ data , true );
1065+ if ($ compressedAttributes === false || $ compressedAttributes === null ) {
1066+ return $ share ;
1067+ }
1068+ foreach ($ compressedAttributes as $ compressedAttribute ) {
1069+ $ attributes ->setAttribute (
1070+ $ compressedAttribute [0 ],
1071+ $ compressedAttribute [1 ],
1072+ $ compressedAttribute [2 ]
1073+ );
1074+ }
1075+ $ share ->setAttributes ($ attributes );
1076+ }
1077+
1078+ return $ share ;
1079+ }
1080+
1081+ protected function formatShareAttributes (?IAttributes $ attributes ): ?string {
1082+ if ($ attributes === null || empty ($ attributes ->toArray ())) {
1083+ return null ;
1084+ }
1085+
1086+ $ compressedAttributes = [];
1087+ foreach ($ attributes ->toArray () as $ attribute ) {
1088+ $ compressedAttributes [] = [
1089+ 0 => $ attribute ['scope ' ],
1090+ 1 => $ attribute ['key ' ],
1091+ 2 => $ attribute ['value ' ]
1092+ ];
1093+ }
1094+ return \json_encode ($ compressedAttributes ) ?: null ;
1095+ }
1096+
10291097 public function getOrphanedAttachmentShares (): array {
10301098 $ allCardIds = $ this ->cardMapper ->getAllCardIds ();
10311099
0 commit comments