Skip to content

Commit fc7463a

Browse files
committed
feat: allow getting shares by group id
Signed-off-by: Robin Appelman <[email protected]>
1 parent 58a7906 commit fc7463a

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

apps/files_sharing/lib/Listener/UserAddedToGroupListener.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
namespace OCA\Files_Sharing\Listener;
1010

11+
use OC\Share20\DefaultShareProvider;
1112
use OCA\Files_Sharing\AppInfo\Application;
1213
use OCP\EventDispatcher\Event;
1314
use OCP\EventDispatcher\IEventDispatcher;
@@ -16,7 +17,6 @@
1617
use OCP\IConfig;
1718
use OCP\Share\Events\UserAddedToShareEvent;
1819
use OCP\Share\IManager;
19-
use OCP\Share\IShare;
2020

2121
/** @template-implements IEventListener<UserAddedEvent> */
2222
class UserAddedToGroupListener implements IEventListener {
@@ -25,6 +25,7 @@ public function __construct(
2525
private readonly IManager $shareManager,
2626
private readonly IConfig $config,
2727
private readonly IEventDispatcher $eventDispatcher,
28+
private readonly DefaultShareProvider $shareProvider,
2829
) {
2930
}
3031

@@ -36,16 +37,9 @@ public function handle(Event $event): void {
3637
$user = $event->getUser();
3738
$group = $event->getGroup();
3839

39-
// todo: add a way to get shares by group id
40-
// Get all group shares this user has access to now to filter later
41-
$shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1);
40+
$shares = $this->shareProvider->getSharedWithGroup($group->getGID());
4241

4342
foreach ($shares as $share) {
44-
// If this is not the new group we can skip it
45-
if ($share->getSharedWith() !== $group->getGID()) {
46-
continue;
47-
}
48-
4943
// Accept the share if needed
5044
if ($this->hasAutoAccept($user->getUID())) {
5145
$this->shareManager->acceptShare($share, $user->getUID());

apps/files_sharing/lib/Listener/UserRemovedFromGroupListener.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@
88
*/
99
namespace OCA\Files_Sharing\Listener;
1010

11+
use OC\Share20\DefaultShareProvider;
1112
use OCP\EventDispatcher\Event;
1213
use OCP\EventDispatcher\IEventDispatcher;
1314
use OCP\EventDispatcher\IEventListener;
1415
use OCP\Group\Events\UserRemovedEvent;
1516
use OCP\Share\Events\UserRemovedFromShareEvent;
16-
use OCP\Share\IManager;
17-
use OCP\Share\IShare;
1817

1918
/** @template-implements IEventListener<UserRemovedEvent> */
2019
class UserRemovedFromGroupListener implements IEventListener {
2120

2221
public function __construct(
23-
private readonly IManager $shareManager,
2422
private readonly IEventDispatcher $eventDispatcher,
23+
private readonly DefaultShareProvider $shareProvider,
2524
) {
2625
}
2726

@@ -33,23 +32,9 @@ public function handle(Event $event): void {
3332
$user = $event->getUser();
3433
$group = $event->getGroup();
3534

36-
// todo: add a way to get shares by group id
37-
$groupUsers = $group->getUsers();
38-
$groupUser = current($groupUsers);
39-
if ($groupUser) {
40-
// get the shares from a user still in the group
41-
$shares = $this->shareManager->getSharedWith($groupUser->getUID(), IShare::TYPE_GROUP, null, -1);
42-
} else {
43-
// if nobody is in the group anymore we current have to go through all shares
44-
$shares = $this->shareManager->getAllShares();
45-
}
35+
$shares = $this->shareProvider->getSharedWithGroup($group->getGID());
4636

4737
foreach ($shares as $share) {
48-
// If this is not the new group we can skip it
49-
if ($share->getShareType() === IShare::TYPE_GROUP && $share->getSharedWith() !== $group->getGID()) {
50-
continue;
51-
}
52-
5338
$this->eventDispatcher->dispatchTyped(new UserRemovedFromShareEvent($share, $user));
5439
}
5540
}

lib/private/Share20/DefaultShareProvider.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,39 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
948948
return $shares;
949949
}
950950

951+
/**
952+
* Get all shares shared with a group
953+
*
954+
* @return Share[]
955+
*/
956+
public function getSharedWithGroup(string $groupId): array {
957+
/** @var Share[] $shares */
958+
$shares = [];
959+
$qb = $this->dbConn->getQueryBuilder();
960+
$qb->select('s.*',
961+
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
962+
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
963+
'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
964+
)
965+
->selectAlias('st.id', 'storage_string_id')
966+
->from('share', 's')
967+
->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
968+
->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
969+
->orderBy('s.id');
970+
971+
$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)))
972+
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($groupId)))
973+
->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
974+
975+
$cursor = $qb->executeQuery();
976+
while ($data = $cursor->fetch()) {
977+
$share = $this->createShare($data);
978+
$shares[$share->getId()] = $share;
979+
}
980+
981+
return $shares;
982+
}
983+
951984
/**
952985
* Get a share by token
953986
*

0 commit comments

Comments
 (0)