Skip to content

Commit 6055c02

Browse files
authored
Merge pull request #47520 from nextcloud/backport/47325/stable26
2 parents 59e92dd + f735db9 commit 6055c02

File tree

3 files changed

+74
-20
lines changed

3 files changed

+74
-20
lines changed

lib/private/Share20/DefaultShareProvider.php

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use OCP\Mail\IMailer;
5454
use OCP\Share\Exceptions\ShareNotFound;
5555
use OCP\Share\IAttributes;
56+
use OCP\Share\IManager;
5657
use OCP\Share\IShare;
5758
use OCP\Share\IShareProvider;
5859
use function str_starts_with;
@@ -94,15 +95,17 @@ class DefaultShareProvider implements IShareProvider {
9495
private $config;
9596

9697
public function __construct(
97-
IDBConnection $connection,
98-
IUserManager $userManager,
99-
IGroupManager $groupManager,
100-
IRootFolder $rootFolder,
101-
IMailer $mailer,
102-
Defaults $defaults,
103-
IFactory $l10nFactory,
104-
IURLGenerator $urlGenerator,
105-
IConfig $config) {
98+
IDBConnection $connection,
99+
IUserManager $userManager,
100+
IGroupManager $groupManager,
101+
IRootFolder $rootFolder,
102+
IMailer $mailer,
103+
Defaults $defaults,
104+
IFactory $l10nFactory,
105+
IURLGenerator $urlGenerator,
106+
IConfig $config,
107+
private IManager $shareManager,
108+
) {
106109
$this->dbConn = $connection;
107110
$this->userManager = $userManager;
108111
$this->groupManager = $groupManager;
@@ -1302,6 +1305,7 @@ public function groupDeleted($gid) {
13021305
*
13031306
* @param string $uid
13041307
* @param string $gid
1308+
* @return void
13051309
*/
13061310
public function userDeletedFromGroup($uid, $gid) {
13071311
/*
@@ -1313,7 +1317,7 @@ public function userDeletedFromGroup($uid, $gid) {
13131317
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)))
13141318
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
13151319

1316-
$cursor = $qb->execute();
1320+
$cursor = $qb->executeQuery();
13171321
$ids = [];
13181322
while ($row = $cursor->fetch()) {
13191323
$ids[] = (int)$row['id'];
@@ -1330,7 +1334,45 @@ public function userDeletedFromGroup($uid, $gid) {
13301334
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)))
13311335
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
13321336
->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1333-
$qb->execute();
1337+
$qb->executeStatement();
1338+
}
1339+
}
1340+
1341+
if ($this->shareManager->shareWithGroupMembersOnly()) {
1342+
$user = $this->userManager->get($uid);
1343+
if ($user === null) {
1344+
return;
1345+
}
1346+
$userGroups = $this->groupManager->getUserGroupIds($user);
1347+
1348+
// Delete user shares received by the user from users in the group.
1349+
$userReceivedShares = $this->shareManager->getSharedWith($uid, IShare::TYPE_USER, null, -1);
1350+
foreach ($userReceivedShares as $share) {
1351+
$owner = $this->userManager->get($share->getSharedBy());
1352+
if ($owner === null) {
1353+
continue;
1354+
}
1355+
$ownerGroups = $this->groupManager->getUserGroupIds($owner);
1356+
$mutualGroups = array_intersect($userGroups, $ownerGroups);
1357+
1358+
if (count($mutualGroups) === 0) {
1359+
$this->shareManager->deleteShare($share);
1360+
}
1361+
}
1362+
1363+
// Delete user shares from the user to users in the group.
1364+
$userEmittedShares = $this->shareManager->getSharesBy($uid, IShare::TYPE_USER, null, true, -1);
1365+
foreach ($userEmittedShares as $share) {
1366+
$recipient = $this->userManager->get($share->getSharedWith());
1367+
if ($recipient === null) {
1368+
continue;
1369+
}
1370+
$recipientGroups = $this->groupManager->getUserGroupIds($recipient);
1371+
$mutualGroups = array_intersect($userGroups, $recipientGroups);
1372+
1373+
if (count($mutualGroups) === 0) {
1374+
$this->shareManager->deleteShare($share);
1375+
}
13341376
}
13351377
}
13361378
}

lib/private/Share20/ProviderFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ protected function defaultShareProvider() {
104104
$this->serverContainer->query(Defaults::class),
105105
$this->serverContainer->getL10NFactory(),
106106
$this->serverContainer->getURLGenerator(),
107-
$this->serverContainer->getConfig()
107+
$this->serverContainer->getConfig(),
108+
$this->serverContainer->get(IManager::class),
108109
);
109110
}
110111

tests/lib/Share20/DefaultShareProviderTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use OCP\IUserManager;
4040
use OCP\L10N\IFactory;
4141
use OCP\Mail\IMailer;
42+
use OCP\Share\IManager as IShareManager;
4243
use OCP\Share\IShare;
4344
use PHPUnit\Framework\MockObject\MockObject;
4445

@@ -82,6 +83,9 @@ class DefaultShareProviderTest extends \Test\TestCase {
8283
/** @var IConfig|MockObject */
8384
protected $config;
8485

86+
/** @var IShareManager&MockObject */
87+
protected $shareManager;
88+
8589
protected function setUp(): void {
8690
$this->dbConn = \OC::$server->getDatabaseConnection();
8791
$this->userManager = $this->createMock(IUserManager::class);
@@ -93,6 +97,7 @@ protected function setUp(): void {
9397
$this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
9498
$this->urlGenerator = $this->createMock(IURLGenerator::class);
9599
$this->config = $this->createMock(IConfig::class);
100+
$this->shareManager = $this->createMock(IShareManager::class);
96101

97102
$this->userManager->expects($this->any())->method('userExists')->willReturn(true);
98103

@@ -108,7 +113,8 @@ protected function setUp(): void {
108113
$this->defaults,
109114
$this->l10nFactory,
110115
$this->urlGenerator,
111-
$this->config
116+
$this->config,
117+
$this->shareManager,
112118
);
113119
}
114120

@@ -132,8 +138,8 @@ protected function tearDown(): void {
132138
* @return int
133139
*/
134140
private function addShareToDB($shareType, $sharedWith, $sharedBy, $shareOwner,
135-
$itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
136-
$parent = null) {
141+
$itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
142+
$parent = null) {
137143
$qb = $this->dbConn->getQueryBuilder();
138144
$qb->insert('share');
139145

@@ -469,7 +475,8 @@ public function testDeleteSingleShare() {
469475
$this->defaults,
470476
$this->l10nFactory,
471477
$this->urlGenerator,
472-
$this->config
478+
$this->config,
479+
$this->shareManager,
473480
])
474481
->setMethods(['getShareById'])
475482
->getMock();
@@ -564,7 +571,8 @@ public function testDeleteGroupShareWithUserGroupShares() {
564571
$this->defaults,
565572
$this->l10nFactory,
566573
$this->urlGenerator,
567-
$this->config
574+
$this->config,
575+
$this->shareManager,
568576
])
569577
->setMethods(['getShareById'])
570578
->getMock();
@@ -2524,7 +2532,8 @@ public function testGetSharesInFolder() {
25242532
$this->defaults,
25252533
$this->l10nFactory,
25262534
$this->urlGenerator,
2527-
$this->config
2535+
$this->config,
2536+
$this->shareManager,
25282537
);
25292538

25302539
$password = md5(time());
@@ -2622,7 +2631,8 @@ public function testGetAccessListNoCurrentAccessRequired() {
26222631
$this->defaults,
26232632
$this->l10nFactory,
26242633
$this->urlGenerator,
2625-
$this->config
2634+
$this->config,
2635+
$this->shareManager,
26262636
);
26272637

26282638
$u1 = $userManager->createUser('testShare1', 'test');
@@ -2718,7 +2728,8 @@ public function testGetAccessListCurrentAccessRequired() {
27182728
$this->defaults,
27192729
$this->l10nFactory,
27202730
$this->urlGenerator,
2721-
$this->config
2731+
$this->config,
2732+
$this->shareManager,
27222733
);
27232734

27242735
$u1 = $userManager->createUser('testShare1', 'test');

0 commit comments

Comments
 (0)