简体   繁体   中英

Current logged-in user in stackmob

I'm trying to get a list of contacts for the user that is logged in, but I can't figure out how to find the current user. I know from Parse.com you use Parse.User.current() does Stackmob have anything like this?

Code below to demonstrate what i'm looking for.

$(window).load(function () {
        var user = StackMob.User;
        var Contacts = StackMob.Model.extend({ schemaName: 'contacts' });
        var myContacts = new Contacts();
        var q = new StackMob.Collection.Query();
        q.equals('sm_owner', user);
        q.orderAsc('FirstName'); //sort by title in ascending order
        q.setRange(0, 9); //get the first 10.  second 10 would be setRange(10,19)
        myContacts.query(q, {
            success: function (model) {
                console.debug(model.toJSON()); //JSON array of matching Todo objects
            },
            error: function (model, response) {
                console.debug(response);
            }
        });
    });

I got what i was looking for using the code below but having ('sm_owner','user/thomas') is sort of useless.

$(window).load(function () {            
        var Contacts = StackMob.Model.extend({ schemaName: 'contacts' });
        var myContacts = new Contacts();
        var q = new StackMob.Collection.Query();
        q.equals('sm_owner', 'user/thomas');
        q.orderAsc('FirstName'); //sort by title in ascending order
        q.setRange(0, 9); //get the first 10.  second 10 would be setRange(10,19)
        myContacts.query(q, {
            success: function (model) {
                console.debug(model.toJSON()); //JSON array of matching Todo objects
            },
            error: function (model, response) {
                console.debug(response);
            }
        });
    });

It sounds like you want to do this using StackMob's schema permissions .

For example, you can set up permissions so that your users can only read contact objects that they themselves have created.

When a user creates an object, the sm_owner field is automatically set to the currently logged-in user. To take advantage of this, you can set up the contacts schema to only allow a user to read their own contacts. This way, filtering by current user is done server-side and you don't have to specifically query for it on the client.

How to set this up:

Go go Schema Configuration and Edit select your schema. Under Schema Permissions, choose Logged In Permissions -> Allow to sm_owner for Read , Update , and Delete .

Now when you query the contacts schema, only contact objects where ( sm_owner == the current user) will be returned. Then you can simply remove this line:

q.equals('sm_owner', 'user/thomas');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM