Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable26] Apply group limit on remove from group #47520

Merged
merged 7 commits into from
Sep 4, 2024
64 changes: 53 additions & 11 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use OCP\Mail\IMailer;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IAttributes;
use OCP\Share\IManager;
use OCP\Share\IShare;
use OCP\Share\IShareProvider;
use function str_starts_with;
Expand Down Expand Up @@ -94,15 +95,17 @@ class DefaultShareProvider implements IShareProvider {
private $config;

public function __construct(
IDBConnection $connection,
IUserManager $userManager,
IGroupManager $groupManager,
IRootFolder $rootFolder,
IMailer $mailer,
Defaults $defaults,
IFactory $l10nFactory,
IURLGenerator $urlGenerator,
IConfig $config) {
IDBConnection $connection,
IUserManager $userManager,
IGroupManager $groupManager,
IRootFolder $rootFolder,
IMailer $mailer,
Defaults $defaults,
IFactory $l10nFactory,
IURLGenerator $urlGenerator,
IConfig $config,
private IManager $shareManager,
) {
$this->dbConn = $connection;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
Expand Down Expand Up @@ -1302,6 +1305,7 @@ public function groupDeleted($gid) {
*
* @param string $uid
* @param string $gid
* @return void
*/
public function userDeletedFromGroup($uid, $gid) {
/*
Expand All @@ -1313,7 +1317,7 @@ public function userDeletedFromGroup($uid, $gid) {
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)))
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));

$cursor = $qb->execute();
$cursor = $qb->executeQuery();
$ids = [];
while ($row = $cursor->fetch()) {
$ids[] = (int)$row['id'];
Expand All @@ -1330,7 +1334,45 @@ public function userDeletedFromGroup($uid, $gid) {
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)))
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
$qb->execute();
$qb->executeStatement();
}
}

if ($this->shareManager->shareWithGroupMembersOnly()) {
$user = $this->userManager->get($uid);
if ($user === null) {
return;
}
$userGroups = $this->groupManager->getUserGroupIds($user);

// Delete user shares received by the user from users in the group.
$userReceivedShares = $this->shareManager->getSharedWith($uid, IShare::TYPE_USER, null, -1);
foreach ($userReceivedShares as $share) {
$owner = $this->userManager->get($share->getSharedBy());
if ($owner === null) {
continue;
}
$ownerGroups = $this->groupManager->getUserGroupIds($owner);
$mutualGroups = array_intersect($userGroups, $ownerGroups);

if (count($mutualGroups) === 0) {
$this->shareManager->deleteShare($share);
}
}

// Delete user shares from the user to users in the group.
$userEmittedShares = $this->shareManager->getSharesBy($uid, IShare::TYPE_USER, null, true, -1);
foreach ($userEmittedShares as $share) {
$recipient = $this->userManager->get($share->getSharedWith());
if ($recipient === null) {
continue;
}
$recipientGroups = $this->groupManager->getUserGroupIds($recipient);
$mutualGroups = array_intersect($userGroups, $recipientGroups);

if (count($mutualGroups) === 0) {
$this->shareManager->deleteShare($share);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Share20/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ protected function defaultShareProvider() {
$this->serverContainer->query(Defaults::class),
$this->serverContainer->getL10NFactory(),
$this->serverContainer->getURLGenerator(),
$this->serverContainer->getConfig()
$this->serverContainer->getConfig(),
$this->serverContainer->get(IManager::class),
);
}

Expand Down
27 changes: 19 additions & 8 deletions tests/lib/Share20/DefaultShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
use PHPUnit\Framework\MockObject\MockObject;

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

/** @var IShareManager&MockObject */
protected $shareManager;

protected function setUp(): void {
$this->dbConn = \OC::$server->getDatabaseConnection();
$this->userManager = $this->createMock(IUserManager::class);
Expand All @@ -93,6 +97,7 @@ protected function setUp(): void {
$this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->config = $this->createMock(IConfig::class);
$this->shareManager = $this->createMock(IShareManager::class);

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

Expand All @@ -108,7 +113,8 @@ protected function setUp(): void {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->config,
$this->shareManager,
);
}

Expand All @@ -132,8 +138,8 @@ protected function tearDown(): void {
* @return int
*/
private function addShareToDB($shareType, $sharedWith, $sharedBy, $shareOwner,
$itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
$parent = null) {
$itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
$parent = null) {
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('share');

Expand Down Expand Up @@ -469,7 +475,8 @@ public function testDeleteSingleShare() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->config,
$this->shareManager,
])
->setMethods(['getShareById'])
->getMock();
Expand Down Expand Up @@ -564,7 +571,8 @@ public function testDeleteGroupShareWithUserGroupShares() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->config,
$this->shareManager,
])
->setMethods(['getShareById'])
->getMock();
Expand Down Expand Up @@ -2524,7 +2532,8 @@ public function testGetSharesInFolder() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->config,
$this->shareManager,
);

$password = md5(time());
Expand Down Expand Up @@ -2622,7 +2631,8 @@ public function testGetAccessListNoCurrentAccessRequired() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->config,
$this->shareManager,
);

$u1 = $userManager->createUser('testShare1', 'test');
Expand Down Expand Up @@ -2718,7 +2728,8 @@ public function testGetAccessListCurrentAccessRequired() {
$this->defaults,
$this->l10nFactory,
$this->urlGenerator,
$this->config
$this->config,
$this->shareManager,
);

$u1 = $userManager->createUser('testShare1', 'test');
Expand Down
Loading