Back

[SOLVED] User permissions on document creation.

  • 0
  • Web
Camo
31 May, 2023, 14:23

How would I properly set permissions on a new document to a specific user? This seems to not work as intended.

TypeScript
async function createArticle(data) {
        return await databases.createDocument('default', 'rss_articles', sdk.ID.unique(), {
            title: data.title,
            content: data.content,
            url: data.url,
            $permissions: [
                `read("user:${data.userId})`,
                `update("user:${data.userId})`,
                `delete("user:${data.userId}")`
            ]
        });
    }
TL;DR
The user was having issues with user permissions on document creation. They were seeing all posts on both ends when using the realtime database. After investigating, they found a problem with the permission settings. They also wanted to know how to get the ID of the document owner without manually creating a field each time. The solution provided was to delegate read permission to a user using `Permission.read()`. Additionally, they were advised to check the Appwrite documentation for examples on setting permissions on a new document to a specific user. The solution for properly setting permissions on a new document was shared using the `$permissions` field in the `createDocument` function.
Camo
31 May, 2023, 14:31

@Dakshie thanks, i have missed that out

Camo
31 May, 2023, 14:39

idk, doesn't work. js async function createArticle(data) { return await databases.createDocument( 'default', 'rss_articles', sdk.ID.unique(), { title: data.title, content: data.content, url: data.url, }, [ sdk.Permission.read(sdk.Role.user(data.userId)) ]) }

Haimantika
31 May, 2023, 14:52

Did you try adding permissions from the console?

Camo
31 May, 2023, 14:54

i had some progress getting user:null

Camo
31 May, 2023, 14:54

will have to take a look on the input if the data is corect, but atleast the permissions are set to null right now

Camo
31 May, 2023, 14:57

is there a way to read a permission from document ?

Camo
31 May, 2023, 14:58

i want to get the owner of a document from one collection, and create a post/document that belongs to that owner in another collection

Haimantika
31 May, 2023, 15:06

You want to delegate the read permission to a user right? You can use - Permission.read()

Camo
31 May, 2023, 15:08

no i want to know the owner of the document

Camo
31 May, 2023, 15:09

then according to the owner of the document, create a post, as there might be several owners on different documents

Camo
31 May, 2023, 15:10

I am using Permission.read(Role.user(data.userId)) to setup the userid, but the way i'm passing it, i have to create another field on creation, i don't want to do that, i want to understand on how to get the ID of the owner from the document itself, not create it manually each time

Camo
31 May, 2023, 15:10

it will save a lot of pain in the future

Camo
31 May, 2023, 15:21

i think i found the problem on permission setting, but the question about how to get who owns the document is still relevant

Camo
31 May, 2023, 22:20

I'm trying to understand what's going on. I finally got the ID of the owner of the document, and it seems that it is working, but now realtime returns everything even though i got set document security permissions on.

Camo
31 May, 2023, 22:24
TypeScript
function extract_user_id(permissions) {
        for (let command of permissions) {
            const start = command.indexOf(":") + 1;
            const end = command.indexOf("\")");
            if (start !== -1 && end !== -1) {
                return command.substring(start, end);
            }
        }
        return null; // null if no user ID was found
    }
Camo
31 May, 2023, 22:24

when loggin in with user1, i see only posts to user1, when loggin in user2 i see only posts to user two, but when the realtime db hits up, i see all posts on both ends

Camo
31 May, 2023, 22:25

doesn't realtime apply the security of documents?

Camo
31 May, 2023, 22:26
Camo
31 May, 2023, 22:30

strange now it works, i guess must have been in the same session at the first time

Camo
31 May, 2023, 22:36

[SOLVED] User permissions on document creation.

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