Back

[CLOSED] Comparing User ID in Authorization with Document Owner in PHP Server Function

  • 1
  • Databases
  • Functions
  • Users
sidney
9 Aug, 2023, 08:40

I'm currently working on a PHP server function that involves deleting a document and setting permissions. I want to compare the user ID from the authorization with the owner of the document. If they match, the user should have permission to delete the document. What is the correct way to compare them?

TL;DR
To compare the user ID from the authorization with the owner of the document in PHP, follow these steps: 1. Use the JWT (Token after user logs in) to make a request and get the user's details. 2. Retrieve the document using the document ID with the `databasesGetDocument` function or use `databasesListDocuments` if you don't have the ID and need to loop through documents. 3. Compare the user ID from the authorization with the document's owner using an if statement. 4. If they match, delete the document using the `deleteDocument` function. 5. If they don't match,
joeyouss
9 Aug, 2023, 08:54

Hi, here is me thinking out loud:

You want to compare the logged-in user's ID with the document's owner. To do that, you first need the logged-in user's ID.

Assuming the frontend sends a request to your PHP server function with the JWT (Token after user logs in):

Use JWT to make a request to get user's details

Assuming you have the documentID, you can fetch the document using https://appwrite.io/docs/server/databases?sdk=php-default#databasesGetDocument

if you do not have ID, ammm, first of all, make some criteria to know the documentID and or use listDocuments https://appwrite.io/docs/server/databases?sdk=php-default#databasesListDocuments and maybe you can loop. The more specific your criteria, the fewer documents you'll likely have to loop through

Finally, compare the user ID from the authorization with the document's owner. If they match, delete the document something like:

TypeScript
if ($document['$owner'] == $userId) {
    // Delete the document
    $database->deleteDocument('YOUR_COLLECTION_ID', $documentId);
    echo "Document deleted";
} else {
    echo "You are not the owner, cannot delete.";
}```
sidney
9 Aug, 2023, 09:12

I was thinking about another way, to use the function getUser and after you get the userId than compare with the owner of the document? Can this solution be correct also?

joeyouss
9 Aug, 2023, 09:17

sure, as mentioned above, you would need documentID for that

sidney
9 Aug, 2023, 10:03

Okay, thank you

joeyouss
9 Aug, 2023, 10:58

[CLOSED] Comparing User ID in Authorization with Document Owner in PHP Server Function

sidney
9 Aug, 2023, 11:03

Thanks, it works

joeyouss
9 Aug, 2023, 11:05

awesomeeeee

joeyouss
9 Aug, 2023, 11:05

glad to know

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