Skip to content

Commit

Permalink
fix(FileListener): Use IUserMountCache instead of Share\IManager to g…
Browse files Browse the repository at this point in the history
…et access list

fixes #1118

Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed May 27, 2024
1 parent 7192f6a commit 8b214da
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions lib/Hooks/FileListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Cache\CacheEntryInsertedEvent;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\NodeCreatedEvent;
Expand All @@ -34,34 +36,27 @@
use OCP\Files\NotFoundException;
use OCP\Share\Events\ShareCreatedEvent;
use OCP\Share\Events\ShareDeletedEvent;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

/**
* @template-implements IEventListener<Event>
*/
class FileListener implements IEventListener {
private FaceDetectionMapper $faceDetectionMapper;
private LoggerInterface $logger;
private QueueService $queue;
private IgnoreService $ignoreService;
private ?bool $movingFromIgnoredTerritory;
private StorageService $storageService;
private IManager $shareManager;
private IRootFolder $rootFolder;
/** @var list<string> */
private array $sourceUserIds;
private ?Node $source = null;

public function __construct(FaceDetectionMapper $faceDetectionMapper, LoggerInterface $logger, QueueService $queue, IgnoreService $ignoreService, StorageService $storageService, IManager $shareManager, IRootFolder $rootFolder) {
$this->faceDetectionMapper = $faceDetectionMapper;
$this->logger = $logger;
$this->queue = $queue;
$this->ignoreService = $ignoreService;
public function __construct(
private FaceDetectionMapper $faceDetectionMapper,
private LoggerInterface $logger,
private QueueService $queue,
private IgnoreService $ignoreService,
private StorageService $storageService,
private IRootFolder $rootFolder,
private IUserMountCache $userMountCache,
) {
$this->movingFromIgnoredTerritory = null;
$this->storageService = $storageService;
$this->shareManager = $shareManager;
$this->rootFolder = $rootFolder;
$this->sourceUserIds = [];
}

Expand All @@ -72,9 +67,10 @@ public function handle(Event $event): void {
$ownerId = $share->getShareOwner();
$node = $share->getNode();

/** @var array{users:array<string,array{node_id:int, node_path: string}>, remote: array<string,array{node_id:int, node_path: string}>, mail: array<string,array{node_id:int, node_path: string}>} $accessList */
$accessList = $this->shareManager->getAccessList($node, true, true);
$userIds = array_map(fn ($id) => strval($id), array_keys($accessList['users']));
$mountInfos = $this->userMountCache->getMountsForFileId($node->getId());
$userIds = array_map(static function (ICachedMountInfo $mountInfo) {
return $mountInfo->getUser()->getUID();
}, $mountInfos);

if ($node->getType() === FileInfo::TYPE_FOLDER) {
$mount = $node->getMountPoint();
Expand Down Expand Up @@ -109,9 +105,10 @@ public function handle(Event $event): void {
$share = $event->getShare();
$node = $share->getNode();

/** @var array{users:array<string,array{node_id:int, node_path: string}>, remote: array<string,array{node_id:int, node_path: string}>, mail: array<string,array{node_id:int, node_path: string}>} $accessList */
$accessList = $this->shareManager->getAccessList($node, true, true);
$userIds = array_keys($accessList['users']);
$mountInfos = $this->userMountCache->getMountsForFileId($node->getId());
$userIds = array_map(static function (ICachedMountInfo $mountInfo) {
return $mountInfo->getUser()->getUID();
}, $mountInfos);

if ($node->getType() === FileInfo::TYPE_FOLDER) {
$mount = $node->getMountPoint();
Expand All @@ -138,9 +135,10 @@ public function handle(Event $event): void {
} else {
$this->movingFromIgnoredTerritory = false;
}
/** @var array{users:array<string,array{node_id:int, node_path: string}>, remote: array<string,array{node_id:int, node_path: string}>, mail: array<string,array{node_id:int, node_path: string}>} $sourceAccessList */
$sourceAccessList = $this->shareManager->getAccessList($event->getSource(), true, true);
$this->sourceUserIds = array_map(fn ($id) => strval($id), array_keys($sourceAccessList['users']));
$mountInfos = $this->userMountCache->getMountsForFileId($event->getSource()->getId());
$this->sourceUserIds = array_map(static function (ICachedMountInfo $mountInfo) {

Check failure on line 139 in lib/Hooks/FileListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud (8.1, dev-stable29)

PropertyTypeCoercion

lib/Hooks/FileListener.php:139:28: PropertyTypeCoercion: $this->sourceUserIds expects 'list<string>', parent type 'array<array-key, string>' provided (see https://psalm.dev/198)

Check failure on line 139 in lib/Hooks/FileListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud (8.1, dev-master)

PropertyTypeCoercion

lib/Hooks/FileListener.php:139:28: PropertyTypeCoercion: $this->sourceUserIds expects 'list<string>', parent type 'array<array-key, string>' provided (see https://psalm.dev/198)

Check failure on line 139 in lib/Hooks/FileListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud (8.2, dev-master)

PropertyTypeCoercion

lib/Hooks/FileListener.php:139:28: PropertyTypeCoercion: $this->sourceUserIds expects 'list<string>', parent type 'array<array-key, string>' provided (see https://psalm.dev/198)

Check failure on line 139 in lib/Hooks/FileListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud (8.2, dev-stable29)

PropertyTypeCoercion

lib/Hooks/FileListener.php:139:28: PropertyTypeCoercion: $this->sourceUserIds expects 'list<string>', parent type 'array<array-key, string>' provided (see https://psalm.dev/198)

Check failure on line 139 in lib/Hooks/FileListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud (8.3, dev-master)

PropertyTypeCoercion

lib/Hooks/FileListener.php:139:28: PropertyTypeCoercion: $this->sourceUserIds expects 'list<string>', parent type 'array<array-key, string>' provided (see https://psalm.dev/198)

Check failure on line 139 in lib/Hooks/FileListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud (8.3, dev-stable29)

PropertyTypeCoercion

lib/Hooks/FileListener.php:139:28: PropertyTypeCoercion: $this->sourceUserIds expects 'list<string>', parent type 'array<array-key, string>' provided (see https://psalm.dev/198)
return $mountInfo->getUser()->getUID();
}, $mountInfos);
$this->source = $event->getSource();
return;
}
Expand Down Expand Up @@ -336,9 +334,10 @@ public function postInsert(Node $node, bool $recurse = true): void {
}

public function postRename(Node $source, Node $target): void {
/** @var array{users:array<string,array{node_id:int, node_path: string}>, remote: array<string,array{node_id:int, node_path: string}>, mail: array<string,array{node_id:int, node_path: string}>} $targetAccessList */
$targetAccessList = $this->shareManager->getAccessList($target, true, true);
$targetUserIds = array_map(fn ($id) => strval($id), array_keys($targetAccessList['users']));
$mountInfos = $this->userMountCache->getMountsForFileId($target->getId());
$targetUserIds = array_map(static function (ICachedMountInfo $mountInfo) {
return $mountInfo->getUser()->getUID();
}, $mountInfos);

$usersToAdd = array_diff($targetUserIds, $this->sourceUserIds);
$existingUsers = array_diff($targetUserIds, $usersToAdd);
Expand Down

0 comments on commit 8b214da

Please sign in to comment.