Skip to content

Commit

Permalink
fixing moderator overview on reshares
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Feb 18, 2022
1 parent f5d1365 commit 76281fc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
49 changes: 36 additions & 13 deletions apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
*/
namespace OCA\Files_Sharing\Controller;

use OCA\Files\Helper;
use OCA\Files_Sharing\Exceptions\SharingRightsException;
use OCA\Files_Sharing\External\Storage;
use OCA\Files\Helper;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
Expand All @@ -56,9 +56,9 @@
use OCP\AppFramework\OCSController;
use OCP\AppFramework\QueryException;
use OCP\Constants;
use OCP\Files\Folder;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IConfig;
Expand All @@ -71,12 +71,12 @@
use OCP\IUserManager;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\Share;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
use OCP\UserStatus\IManager as IUserStatusManager;
use Psr\Log\LoggerInterface;

/**
* Class Share20OCS
Expand All @@ -95,6 +95,8 @@ class ShareAPIController extends OCSController {
private $rootFolder;
/** @var IURLGenerator */
private $urlGenerator;
/** @var LoggerInterface */
private $logger;
/** @var string */
private $currentUser;
/** @var IL10N */
Expand Down Expand Up @@ -122,6 +124,7 @@ class ShareAPIController extends OCSController {
* @param IUserManager $userManager
* @param IRootFolder $rootFolder
* @param IURLGenerator $urlGenerator
* @param LoggerInterface $logger ,
* @param string $userId
* @param IL10N $l10n
* @param IConfig $config
Expand All @@ -137,6 +140,7 @@ public function __construct(
IUserManager $userManager,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator,
LoggerInterface $logger,
string $userId = null,
IL10N $l10n,
IConfig $config,
Expand All @@ -153,6 +157,7 @@ public function __construct(
$this->request = $request;
$this->rootFolder = $rootFolder;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->currentUser = $userId;
$this->l = $l10n;
$this->config = $config;
Expand Down Expand Up @@ -523,7 +528,7 @@ public function createShare(
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
} elseif ($shareType === IShare::TYPE_LINK
|| $shareType === IShare::TYPE_EMAIL) {
|| $shareType === IShare::TYPE_EMAIL) {

// Can we even share links?
if (!$this->shareManager->shareApiAllowLinks()) {
Expand All @@ -542,9 +547,9 @@ public function createShare(
}

$permissions = Constants::PERMISSION_READ |
Constants::PERMISSION_CREATE |
Constants::PERMISSION_UPDATE |
Constants::PERMISSION_DELETE;
Constants::PERMISSION_CREATE |
Constants::PERMISSION_UPDATE |
Constants::PERMISSION_DELETE;
} else {
$permissions = Constants::PERMISSION_READ;
}
Expand Down Expand Up @@ -1742,7 +1747,7 @@ private function shareProviderResharingRights(string $userId, IShare $share, $no
}

if ($share->getShareType() === IShare::TYPE_CIRCLE && \OC::$server->getAppManager()->isEnabledForUser('circles')
&& class_exists('\OCA\Circles\Api\v1\Circles')) {
&& class_exists('\OCA\Circles\CirclesManager')) {
$hasCircleId = (substr($share->getSharedWith(), -1) === ']');
$shareWithStart = ($hasCircleId ? strrpos($share->getSharedWith(), '[') + 1 : 0);
$shareWithLength = ($hasCircleId ? -1 : strpos($share->getSharedWith(), ' '));
Expand All @@ -1752,12 +1757,30 @@ private function shareProviderResharingRights(string $userId, IShare $share, $no
$sharedWith = substr($share->getSharedWith(), $shareWithStart, $shareWithLength);
}
try {
$member = \OCA\Circles\Api\v1\Circles::getMember($sharedWith, $userId, 1);
if ($member->getLevel() >= 4) {
return true;
// TODO: switch to ICirclesManager once we have it available within core
/** @var \OCA\Circles\CirclesManager $circleManager */
$circleManager = $this->serverContainer->get('\OCA\Circles\CirclesManager');
$circleManager->startSuperSession();

// We get the federatedUser linked to the userId (local user, so type=1)
// We browse the federatedUser's membership to confirm it exists and level is moderator
$federatedUser = $circleManager->getFederatedUser($userId, 1);
foreach($federatedUser->getMemberships() as $membership) {
if ($membership->getCircleId() === $sharedWith) {
return ($membership->getLevel() >= 4);
}
}
return false;
} catch (QueryException $e) {
} catch (\Exception $e) {
$this->logger->info(
'Exception while confirming resharing rights visibility',
[
'userId' => $userId,
'sharedWith' => $sharedWith,
'nodeId' => $node->getId(),
'exception' => $e,
]
);

return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\IManager;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;
use Test\TestCase;
use OCP\UserStatus\IManager as IUserStatusManager;

Expand Down Expand Up @@ -92,6 +93,9 @@ class ShareAPIControllerTest extends TestCase {
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
private $urlGenerator;

/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
private $loggerInterface;

/** @var string|\PHPUnit\Framework\MockObject\MockObject */
private $currentUser;

Expand Down Expand Up @@ -130,6 +134,7 @@ protected function setUp(): void {
$this->request = $this->createMock(IRequest::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->loggerInterface = $this->createMock(LoggerInterface::class);
$this->currentUser = 'currentUser';

$this->l = $this->createMock(IL10N::class);
Expand All @@ -155,6 +160,7 @@ protected function setUp(): void {
$this->userManager,
$this->rootFolder,
$this->urlGenerator,
$this->loggerInterface,
$this->currentUser,
$this->l,
$this->config,
Expand All @@ -178,6 +184,7 @@ private function mockFormatShare() {
$this->userManager,
$this->rootFolder,
$this->urlGenerator,
$this->loggerInterface,
$this->currentUser,
$this->l,
$this->config,
Expand Down
8 changes: 5 additions & 3 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1543,11 +1543,13 @@
<RedundantCondition occurrences="1">
<code>$permissions &amp; Constants::PERMISSION_READ</code>
</RedundantCondition>
<UndefinedClass occurrences="2">
<code>\OCA\Circles\Api\v1\Circles</code>
<UndefinedClass occurrences="1">
<code>\OCA\Circles\Api\v1\Circles</code>
</UndefinedClass>
<UndefinedDocblockClass occurrences="4">
<UndefinedDocblockClass occurrences="7">
<code>$circleManager</code>
<code>$circleManager</code>
<code>$circleManager</code>
<code>$this-&gt;getRoomShareHelper()</code>
<code>$this-&gt;getRoomShareHelper()</code>
<code>$this-&gt;getRoomShareHelper()</code>
Expand Down

0 comments on commit 76281fc

Please sign in to comment.