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
- Type Mismatch in AppwriteException
There is a discrepancy in the TypeScript type definitions for AppwriteException. The response property is defined as a string in the type definitions, but in pr...
- What Query's are valid for GetDocument?
Documentation shows that Queries are valid here, but doesn't explain which queries are valid. At first I presumed this to be a bug, but before creating a githu...
- Appwrite exception: user_unauthorized, t...
After refreshing the app it is working perfectly