[CLOSED] Comparing User ID in Authorization with Document Owner in PHP Server Function
- 1
- Databases
- Functions
- Users
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?
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:
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.";
}```
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?
sure, as mentioned above, you would need documentID for that
Okay, thank you
[CLOSED] Comparing User ID in Authorization with Document Owner in PHP Server Function
Thanks, it works
awesomeeeee
glad to know
Recommended threads
- Appwrite console is too heavy
The Appwrite console is too heavy And all of my services broken Any support , please
- router_deployment_not_found
I updated my function a few times and now i am getting the error: router_deployment_not_found I even reverted back to my original code but i am still getting th...
- Usage of the new Client() and dealing wi...
Hey guys, just a quick one - we had some web traffic the other day and it ended up bombing out - To put in perspective of how the app works, we have a Nuxt Ap...