I’m testing sharing with read and create file permission. So first, I logged in as owner
, call shareFolder()
and then setReadPermission()
. The folder is shared successully, but setReadPermission()
returns an error: Node for share not found, fileid: 1394
. If I logged in as assgined_to
, the permission changed to readonly. I think it’s because I delete folder many times to test sharing, so there’s cache or trashbin something. What should I fix?
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function shareFolder()
{
try {
$owner = $this->request->getParam(“owner”);
$assigned_to = $this->request->getParam(“assigned_to”);
$work_id = $this->request->getParam(“work_id”);
$userFolder = $this->rootFolder->getUserFolder($owner);
$this->authorizationService->hasAccessWork($work_id);
if (!$userFolder->nodeExists("QLCV")) {
$userFolder->newFolder("QLCV");
}
$qlcvFolder = $userFolder->get("QLCV");
if (!$qlcvFolder->nodeExists($work_id)) {
$qlcvFolder->newFolder($work_id);
$workFolder = $qlcvFolder->get($work_id);
$share = $this->shareManager->newShare();
$share->setSharedBy($owner);
$share->setSharedWith($assigned_to);
$share->setNode($workFolder);
$share->setShareType(IShare::TYPE_USER);
$share->setPermissions(
Constants::PERMISSION_READ | Constants::PERMISSION_CREATE
);
$this->shareManager->createShare($share);
}
return new JSONResponse([
"success" => true,
]);
} catch (\Exception $e) {
return new JSONResponse(
["error" => $e->getMessage()],
$e->getCode()
);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function setReadPermission($work_id, $assigned_to, $owner)
{
try {
$this->authorizationService->hasAccessWork($work_id);
$userFolder = $this->rootFolder->getUserFolder($owner);
$qlcvFolder = $userFolder->get("QLCV");
if ($qlcvFolder->nodeExists($work_id)) {
$workFolder = $qlcvFolder->get($work_id);
$shares = $this->shareManager->getSharesBy(
$owner,
IShare::TYPE_USER
);
foreach ($shares as $share) {
if (
$share->getNode()->getId() === $workFolder->getId() &&
$share->getSharedWith() === $assigned_to
) {
$share->setPermissions(Constants::PERMISSION_READ);
$this->shareManager->updateShare($share);
return new JSONResponse([
"success" => true,
]);
}
}
}
return new JSONResponse(
[
"success" => false,
],
Http::STATUS_BAD_REQUEST
);
} catch (\Exception $e) {
return new JSONResponse(
["error" => $e->getMessage()],
$e->getCode()
);
}
}
I updated setReadPermission()
, logged in as owner
and it works. But I dont know why. If it works, it means getSharedWith()
is executed. But in this case, the sharer is owner
, isn’t it?
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function setReadPermission($work_id, $assigned_to, $owner)
{
try {
$user = $this->userSession->getUser();
$currentUserId = $user->getUID();
$this->authorizationService->hasAccessWork($work_id);
$userFolder = $this->rootFolder->getUserFolder($owner);
$qlcvFolder = $userFolder->get("QLCV");
if ($qlcvFolder->nodeExists($work_id)) {
$workFolder = $qlcvFolder->get($work_id);
if ($currentUserId !== $owner) {
$shares = $this->shareManager->getSharesBy(
$owner,
IShare::TYPE_USER
);
}
else {
$shares = $this->shareManager->getSharedWith(
$assigned_to,
IShare::TYPE_USER
);
}
foreach ($shares as $share) {
if (
$share->getNode()->getId() === $workFolder->getId() &&
$share->getSharedWith() === $assigned_to
) {
$share->setPermissions(Constants::PERMISSION_READ);
$this->shareManager->updateShare($share);
return new JSONResponse([
"success" => true,
]);
}
}
}
return new JSONResponse(
[
"success" => false,
],
Http::STATUS_BAD_REQUEST
);
} catch (\Exception $e) {
return new JSONResponse(
["error" => $e->getMessage()],
$e->getCode()
);
}
}
Still waiting for answer… ;-;
I diffed the two implementations and came up with
$ diff -u a.php b.php
--- a.php 2024-06-27 07:55:45.592767800 +0200
+++ b.php 2024-06-27 07:56:04.569182900 +0200
@@ -5,16 +5,27 @@
public function setReadPermission($work_id, $assigned_to, $owner)
{
try {
+ $user = $this->userSession->getUser();
+ $currentUserId = $user->getUID();
+
$this->authorizationService->hasAccessWork($work_id);
$userFolder = $this->rootFolder->getUserFolder($owner);
$qlcvFolder = $userFolder->get("QLCV");
if ($qlcvFolder->nodeExists($work_id)) {
$workFolder = $qlcvFolder->get($work_id);
- $shares = $this->shareManager->getSharesBy(
- $owner,
- IShare::TYPE_USER
- );
+ if ($currentUserId !== $owner) {
+ $shares = $this->shareManager->getSharesBy(
+ $owner,
+ IShare::TYPE_USER
+ );
+ }
+ else {
+ $shares = $this->shareManager->getSharedWith(
+ $assigned_to,
+ IShare::TYPE_USER
+ );
+ }
foreach ($shares as $share) {
if (
$share->getNode()->getId() === $workFolder->getId() &&
Most probably your issue arises in the red lines. However, I do not see a reason from the code why this should trigger an error just from looking at the code. A quick glance shows no obvious flaws and the crude documentation in the server repo does not tell me much either here.
My way to go was to enable step debugging and check where the error is actually triggered. This is for once the line in your code that fails but also digging into the server code at this location.