Mongo Rules how to pass user email to check rule?

Viewed 16

I have created Mongo App Service with sample dataset from Mongo MFLIX, I'm trying to create comments rules, so loggedin user can see all comments post new one but edit only own comment.

For that purposes I created two roles in following order

  1. User which should be true if user's email is === to email from editing comment

  2. ReadAndWriteAllComments which is always true

This is Mongo Rules code

 {
      "roles": [
        {
          "name": "User",
          "apply_when": {
            "email": "%%user.email"
          },
          "fields": {
            "text": {
              "read": true,
              "write": true
            }
          }
        },
        {
          "name": "ReadAndInsertAll",
          "apply_when": {},
          "read": true,
          "write": true,
          "insert": true,
          "delete": false,
          "search": true
        }
      ]
    }

I'm using graphql client sdk to make queries for fetching comments, inserting and editing.

For posting comments user first needs to login.

This is query I'm sending for fetching movies comments and adding comment, together with auth token in POST request

comments(query:{ movie_id: "573a1391f29313caabcd8d7b" }) { _id name email text } }

mutation { insertOneComment(data: { movie_id:"573a1391f29313caabcd95fb", email: "test1@tt.team", name: "Tester", text: "lorem ipsum" }) { _id date email movie_id name text } }

So I have error

reason="error executing match expression: cannot compare to undefined"; code="InternalServerError"; untrusted="failed to check role User"; details=map[roleName:User]: {"response":{"data":{"insertOneComment":null},"errors":[{"message":"reason="error executing match expression: cannot compare to undefined"; code="InternalServerError"; untrusted="failed to check role User";

I suppose variable user.email is undefined and my question is how should I pass this user.email variable so Mongo can check condition apply_when?

In their documentation there isn't anything about passing this info only general about rules.

https://www.mongodb.com/docs/atlas/app-services/rules/roles/

0 Answers
Related