Page 1 of 2

How can I allow only certain users to see specific items in a collection

Posted: Wed Dec 31, 2014 8:20 pm
by SimonChi

Hie. I have a small app (WhatsForSupper) with three users, Peter James and John and a collection with three items Bread, Fish and Rice. The items in the collection will be populated from a csv file. i.e The users cannot add anything to the collection , they can only view the food items that have been chosen for them. Basically when each user logs in he should only see what he will have for supper. E.g Peter must see bread, James must see rice and John must see fish. However when i'm testing the app the wrong food items are appearing .
For example I've tried editing the ACL column for the "Bread" row in the collection so that when Peter logs in he knows he's having bread for supper.

e.g. {"54a3f9b3e4b0bca36d4f394a":{"read":true,"write":false}}

where "54a3f9b3e4b0bca36d4f394a" is Peter's id

However upon testing the app, a list with rice and fish appears, precisely what Peter is not having for supper ! Peter must have bread for supper and when he logs in he must see bread . How can I make sure that this happens.

I patiently await your usual great support !


How can I allow only certain users to see specific items in a collection

Posted: Thu Jan 01, 2015 1:08 am
by Yurii Orishchuk

Hi Simon,

I guess you can try following way:

  1. Add "user" fields which is pointer to users collection, to your collection.

  2. Filter items by current user using $inQuery clause. See details here: http://devcenter.appery.io/documentat...

    Regards.


How can I allow only certain users to see specific items in a collection

Posted: Tue Jan 06, 2015 1:25 pm
by SimonChi

Thank you so much Yurii. I think I may be headed in the right direction now.
However I still need your help.
I have attempted to follow the instructions in the documentation and have taken these steps

  1. I added a "user" column to my meals collection and I made this column a pointer to the users collection. I then copied and pasted the user ids for the users collection to the "user" column in the meals selection

    Image

  2. I added a query service to the "supper" page.

  3. I opened the settings for WhatsForSupperDB_Meals_query_service and entered the following code for the where parameter

    {"User":{"$in": [54aadf25e4b08ad5d6711fac]}}

    where "54aadf25e4b08ad5d6711fac" is the user id for the user Peter

    However upon testing the service I'm getting the following response
    {
    "status":400,
    "uri":"https://api.appery.io/rest/1/db/colle...",
    "response":{
    "code":"DBSQ219",
    "description":"Incorrect query"
    }
    }

    Please can you help me

    Regards


How can I allow only certain users to see specific items in a collection

Posted: Wed Jan 07, 2015 12:44 am
by Yurii Orishchuk

Hi Simon,

Okay, you did all things correct except the query string.

Pointer fields must be filter with "inQuery" clause. Here is correct where parameter:

pre

{"User": {"$inQuery": {_id: "54aadf25e4b08ad5d6711fac" } } }

/pre

Please read more details about "inQuery" clause here: http://devcenter.appery.io/documentat...

Regards.


How can I allow only certain users to see specific items in a collection

Posted: Thu Jan 08, 2015 10:49 pm
by SimonChi

Thanks a lot for that.
I've corrected the where parameter and tested the service and got the following response
[
{
"id":"54aadf07e4b08ad5d6711fab",
"Meal":"Fish",
"createdAt":"2015-01-05 18:59:19.683",
"updatedAt":"2015-01-06 10:33:08.833",
"User":{
"collName":"users",
"_id":"54aadf39e4b08ad5d6711fb0"
}
}
]

I invoked the query service upon page show then tested the app. I expected it to now show only the food item chosen for the logged in user.
However the supper page is still listing all three food items for all three users.
Image

How do i resolve this.


How can I allow only certain users to see specific items in a collection

Posted: Fri Jan 09, 2015 8:59 am
by Ihor Didevych

Hi Simon,

Here is what we tried:
http://gyazo.com/8f17bde90cbb8212e799...
http://gyazo.com/752c2b2ca05b64d62ada... - this works for us.

Could you show us the screenshots of your service's tabs?


How can I allow only certain users to see specific items in a collection

Posted: Fri Jan 09, 2015 9:35 am
by SimonChi

Hie Ihor here are the screenshots.

WhatsForSupperDB_Meals_query_service_SettingsTab

Image

WhatsForSupperDB_Meals_query_service_RequestTab

Image

WhatsForSupperDB_Meals_query_service_ResponseTab

Image

WhatsForSupperDB_Meals_query_service_TestTab

Image

WhatsForSupperDB_Meals_query_service_EchoTab

Image


How can I allow only certain users to see specific items in a collection

Posted: Fri Jan 09, 2015 3:12 pm
by Evgene Karachevtsev

Hello Simon,

Thanks, response area for test tab looks good.
This is the correct response as wanted.


How can I allow only certain users to see specific items in a collection

Posted: Sat Jan 10, 2015 8:37 am
by SimonChi

Thanks Evgene , However I'm still stuck because when i'm testing the app ,
it is displaying all the items in the collection instead of showing only the items assigned to each user who has logged in. What can I do.

Image


How can I allow only certain users to see specific items in a collection

Posted: Mon Jan 12, 2015 10:26 pm
by Yurii Orishchuk

Hi Simon,

It seems you have wrong "where" parameter in service datasource(service on the certain page).

So:

  1. open datasource "before send" mapping.

  2. Click on "where" request parameter JS button.

  3. Populate it with following JS code:

    pre

    //Here you should place your user id.
    var userId = "52fe1b6fe4b0a25c11c899e0";

    var whereObject = {
    User: {"$inQuery": {_id: whereObject } }
    };

    return JSON.stringify(whereObject);

    /pre

    Regards.