Back

[SOLVED] PHP Update Document Permission

  • 0
  • Databases
  • Users
yannic
23 Mar, 2023, 20:53

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;

TypeScript
$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();
}```
TL;DR
The user is trying to update the permission of a document in PHP. They are retrieving the existing document and trying to add the user to the read permissions. However, they are encountering an error saying "Invalid permissions: Every permission must be of type string." A suggestion is made to check the value of `$document['$permissions']` and refer to the Appwrite documentation on permissions. It is also recommended to add the user to the `['read']` part of the array. To resolve the issue, the user needs to ensure that they have included the line `use Appwrite\Models\Permission;` and use `Permission
yannic
23 Mar, 2023, 20:55

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

Drake
23 Mar, 2023, 20:58

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

yannic
23 Mar, 2023, 20:58

ah sorry

Binyamin
23 Mar, 2023, 21:02

I think you should add him to the ['read'] part of the array

TypeScript
if (!in_array("user:$userId", $readPermissions)) {
  $readPermissions['read'][] = "user:$userId";
}
Binyamin
23 Mar, 2023, 21:02

And create this part if it doesn't exist

yannic
23 Mar, 2023, 21:03

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

yannic
23 Mar, 2023, 21:04

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

Drake
23 Mar, 2023, 21:04

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

yannic
23 Mar, 2023, 21:07

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

Drake
23 Mar, 2023, 21:07

Erre

Drake
23 Mar, 2023, 21:07

Close

yannic
23 Mar, 2023, 21:08

Whats missing?

Drake
23 Mar, 2023, 21:08

Role::User('id')

And, btw, permissions is a list (rather than associative array)

yannic
23 Mar, 2023, 21:09

ah okay

yannic
23 Mar, 2023, 21:56

thanks that helped a lot! its working now.

yannic
24 Mar, 2023, 11:55

[SOLVED] PHP Update Document Permission

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more