Accessing Backendless Data Services in Custom Events for records owned by user

We have a situation where we need to selectively access a Backendless table from within in a Java Custom Event handler. In the cases were we need to limit the results to only records owned by the authenticated user raising the custom event, which is the preferred method in Backendless?

  1. Should we retrieve the current user, perhaps using the API described in the Android/Java docs:

https://backendless.com/documentation/users/android/users_get_current_user.htm

and then explicitly query for records with an “ownerId” column value that matches the “ownerId” value of the current user? (Any additional info about the latter would also be appreciated.)

  1. Or should we Implicity rely on Backendless’s builtin roles and permissions system to limit the results returned by a query to only those records owned by the current authenticated user raising the Custom Event? Here, either the documentation on these pages:

https://backendless.com/documentation/users/android/users_user_roles.htm
https://backendless.com/documentation/users/android/users_global_permissions.htm
https://backendless.com/documentation/users/android/users_asset_container_permissions.htm
https://backendless.com/documentation/users/android/users_asset_permissions.htm

or on pages 63-69 of the current Backendless API for Android document here:

C

would appear to be relevant.

Thanks in advance for any info.

The link on that last URL is correct, but the text of the link should be:

https://backendless.com/documentation/Backendless%20API%20for%20Android.pdf

Thanks.

Kevin,

The first argument in your custom event handler is the RunnerContext class:

RunnerContext object you get provides access to:

    userId of the user that's logged in on the client side where the event had been dispatched (assuming a user is logged in and you sent the "user-token" header in the API to dispatch the event) userToken - the same user-token I described above. user roles - list of roles the logged in user belongs to
If you use our SDK for Java in your custom event handler, you can setup the "context" of the same user (but now on the server-side) using the API call below:
HeadersManager.getInstance().addHeader( HeadersManager.HeadersEnum.USER_TOKEN_KEY, userToken );

Hope this helps.

Regards,
Mark

Hi Mark, thanks for this detailed info. We’ll give it a try.

Mark,

What’s the best way to do this with the Javascript library?

I’m writing a custom event handler with Backendless.ServerCode.customEvent, and would like to connect to the data layer via the Backendless SDK with the authenticated user token.

Similarly to your Java example, I know I can access the user ID / token via the request context. How do I inform the Backendless library to send this token in requests I make with SDK functions?

Within my function, I’ve tried to log several different pieces of the library to try and figure this out:

 

console.log(req.context.userId); // returns my correct user ID
 

console.log(req.context.userToken); // returns my correct user token
 

console.log(Backendless.LocalCache.getAll()); // returns: {}
console.log(Backendless.LocalCache.get("user-token")); // returns: undefined
console.log(Backendless.UserService.getCurrentUser()); // returns: null
 


Clearly the library doesn’t seem to automatically add this information. I’ve searched the Github repository for the Backendless JS SDK but have been unable to locate a function that adds additional headers to the request. What’s the best way to proceed?

How do you do this for Javascript?

@Cameron and @Brian,

Please try the following. It will set the user token corresponding to the user who made the original invocation. Please note that in Backendless 4 this is done automatically.

Backendless.LocalCache.set('user-token', req.context.userToken);

Let me know if it does not work.

Regards,
Mark

Thanks. It works!