
Hey, I just want to update the permission of a document in PHP for a user to for example read. How do I do that? I cant get it to work. My current solution is this: ```$databaseId = 'games'; $collectionId = 'pool'; $documentId = $_GET['document_id'] ?? null; $userId = $_GET['user_id'] ?? null;
$database = new Databases($client);
try {
// Retrieve the existing document
$document = $database->getDocument($databaseId, $collectionId, $documentId);
print_r($document);
$readPermissions = $document['$permissions']['read'] ?? [];
// Add the user to the read permissions if not already present
if (!in_array("user:$userId", $readPermissions)) {
$readPermissions[] = "user:$userId";
}
// Update the document with the new read permissions
$result = $database->updateDocument($databaseId, $collectionId, $documentId, $document, $readPermissions);
echo "Document updated successfully.";
} catch (Exception $e) {
echo "Error updating document: " . $e->getMessage();
}```

I get the error: Error updating document: Invalid permissions: Permission "user:X" is not allowed. Must be one of: read, update, delete, write.

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

ah sorry

I think you should add him to the ['read']
part of the array
if (!in_array("user:$userId", $readPermissions)) {
$readPermissions['read'][] = "user:$userId";
}

And create this part if it doesn't exist

Error updating document: Invalid permissions: Every permission must be of type string.

I also tried it with the [] gone but then the same error occurs as in the question.

You might want to take a look at what $document['$permissions']
is. Also, see: https://appwrite.io/docs/permissions

So I basically have to add use Appwrite\Models\Permission;
and then use Permission::delete(Permission::ROLE_USER, '5c1f88b42259e'),
?

Erre

Close

Whats missing?

Role::User('id')
And, btw, permissions is a list (rather than associative array)

ah okay


thanks that helped a lot! its working now.

[SOLVED] PHP Update Document Permission
Recommended threads
- Realtime Disconnects and Error: INVALID_...
Hi Support, we are still experiencing the issue and we are a subscriber on your platform under the account of charlesbcalague@gmail.com I just want to ask here...
- Collection Permission issue
I am facing issue in my Pro account. "Add" button is disabled while adding permission in DB collection settings.
- Opened my website after long time and Ba...
I built a website around a year back and and used appwrite for making the backend. At that time the website was working fine but now when i open it the images a...
