@@ -181,22 +181,15 @@ public function userHasAccessTo(int $fileId, ?string $userId): bool {
181
181
* @return bool
182
182
*/
183
183
public function groupHasAccessTo (string $ userId , Node $ fileNode , ?string $ groupId ): bool {
184
- $ groupShares = $ this ->shareManager ->getSharesBy ($ userId , ISHARE ::TYPE_GROUP , $ fileNode );
185
- foreach ($ groupShares as $ groupShare ) {
186
- if ($ groupShare ->getSharedWith () === $ groupId ) {
187
- return true ;
188
- }
189
- }
190
- $ folderNode = $ fileNode ->getParent ();
191
- while ($ folderNode ->getParentId () !== -1 ) {
192
- $ groupShares = $ this ->shareManager ->getSharesBy ($ userId , ISHARE ::TYPE_GROUP , $ folderNode );
184
+ do {
185
+ $ groupShares = $ this ->shareManager ->getSharesBy ($ userId , ISHARE ::TYPE_GROUP , $ fileNode );
193
186
foreach ($ groupShares as $ groupShare ) {
194
187
if ($ groupShare ->getSharedWith () === $ groupId ) {
195
188
return true ;
196
189
}
197
190
}
198
- $ folderNode = $ folderNode ->getParent ();
199
- }
191
+ $ fileNode = $ fileNode ->getParent ();
192
+ } while ( $ fileNode -> getParentId () !== - 1 );
200
193
return false ;
201
194
}
202
195
@@ -250,22 +243,50 @@ public function deleteTag(int $id): array {
250
243
* Find users that need a file to be shared with, so all members of the group have it
251
244
* Also says if group share is the correct choice.
252
245
*
253
- * @param int $fileId of the file to check what
246
+ * @param Node $node of the file to check
254
247
* @param string|null $groupId the id of the group
255
248
* @return array
256
249
*/
257
- public function usersNeedShare (int $ fileId , ?string $ groupId ): array {
250
+ public function usersNeedShare (Node $ node , ?string $ groupId ): array {
251
+ $ group = $ this ->groupManager ->get ($ groupId );
252
+ $ groupUsers = $ group ->getUsers ();
253
+ $ usersSet = [];
254
+ $ groupsSet = []; // Stores the user if a share does not exist directly otherwise it is false
255
+ foreach ($ groupUsers as $ groupUser ) {
256
+ $ usersSet [$ groupUser ->getUID ()] = $ groupUser ;
257
+ }
258
+ $ ownerid = $ node ->getOwner ()->getUID ();
259
+ do {
260
+ foreach ($ this ->shareManager ->getSharesBy ($ ownerid , ISHARE ::TYPE_GROUP , $ node ) as $ share ) {
261
+ $ groupsSet [$ share ->getSharedWith ()] = true ;
262
+ }
263
+ foreach ($ this ->shareManager ->getSharesBy ($ ownerid , ISHARE ::TYPE_USER , $ node ) as $ share ) {
264
+ if (isset ($ usersSet [$ share ->getSharedWith ()])) {
265
+ $ usersSet [$ share ->getSharedWith ()] = false ;
266
+ }
267
+ }
268
+ $ node = $ node ->getParent ();
269
+ } while ($ node ->getParentId () !== -1 );
270
+
258
271
$ groupShare = true ;
259
272
$ users = [];
260
- $ group = $ this ->groupManager ->get ($ groupId );
261
- if ($ group instanceof IGroup) {
262
- foreach ($ group ->getUsers () as $ groupUser ) {
263
- if ($ this ->userHasAccessTo ($ fileId , $ groupUser ->getUID ())) {
264
- $ groupShare = false ;
265
- } else {
266
- $ users [] = $ groupUser ->getUID ();
273
+ foreach ($ usersSet as $ uid => $ hasShare ) {
274
+ if ($ hasShare !== false ) { // User has no share
275
+ // Checks if the user is in a group that is being shared with
276
+ $ groupList = $ this ->groupManager ->getUserGroupIds ($ hasShare );
277
+ $ groupFound = false ;
278
+ foreach ($ groupList as $ groupId ) {
279
+ if (isset ($ groups [$ groupId ])) {
280
+ $ groupFound = true ;
281
+ break ;
282
+ }
283
+ }
284
+ if (!$ groupFound ) { // User is not in a group that is being shared with
285
+ $ users [] = $ uid ;
286
+ continue ;
267
287
}
268
288
}
289
+ $ groupShare = false ;
269
290
}
270
291
return ['groupShare ' => $ groupShare , 'users ' => $ users ];
271
292
}
0 commit comments