Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if User has Role - Parse Cloud Code

Writing a Parse Cloud Function (which uses Parse Javascript SDK) and I am having trouble checking to see if the current user has role "Admin". I'm looking at the web view of the Role class and a role with the name "Admin" exists, if I click "View Relations" for users, it shows the current user. I doubt it should matter, but "Admin" is the only role and the current user is the only user with a role. Lastly, the "Admin" role has an ACL of Public Read, so that shouldn't be causing any issues either.

Code is as follows:

...
var queryRole = new Parse.Query(Parse.Role);
queryRole.equalTo('name', 'Admin'); 
queryRole.equalTo("users", Parse.User.current());
queryRole.first({
    success: function(result) { // Role Object
        var role = result;
        role ? authorized = true : console.log('Shiet, user not Admin');
    },
    error: function(error) {
        console.log("Bruh, queryRole error");
    }
})
console.log('After test: Auth = ' + authorized);
if (!authorized) {
    response.error("You ain't no admin, measly user");
    return;    
}
...

This results in the following in the log:

Before test: Auth = false

After test: Auth = false

like image 547
Sorry-Im-a-N00b Avatar asked Mar 21 '15 00:03

Sorry-Im-a-N00b


People also ask

What is parse cloud code?

Cloud Code is easy to use because it’s built on the same Parse JavaScript SDK that powers thousands of apps. The only difference is that this code runs in your Parse Server rather than running on the user’s mobile device. When you update your Cloud Code, it becomes available to all mobile environments instantly.

How to check for user roles in Java?

The first way to check for user roles in Java is to use the @PreAuthorize annotation provided by Spring Security. This annotation can be applied to a class or method, and it accepts a single string value that represents a SpEL expression. Before we can use this annotation, we must first enable global method security.

What is Parse Server and how does it work?

Parse Server is a great, quick way to create an app backend without requiring years of knowledge and time. There are a few additional steps you can do to ensure that your code is the best it can be, and be assured that your Parse Server always runs as smoothly as possible, even as your Cloud Code continues to grow.

How do I validate my Cloud code functions?

The validation function will run prior to your Cloud Code Functions. You can use async and promises here, but try to keep the validation as simple and fast as possible so your cloud requests resolve quickly. As previously mentioned, cloud validator objects will not validate if a masterKey is provided, unless validateMasterKey:trueis set.


2 Answers

Give this a shot:

var authorized = false;
console.log('Before test: Auth = ' + authorized);

var queryRole = new Parse.Query(Parse.Role);
queryRole.equalTo('name', 'Admin');
queryRole.first({
    success: function(result) { // Role Object
        console.log("Okay, that's a start... in success 1 with results: " + result);

        var role = result;
        var adminRelation = new Parse.Relation(role, 'users');
        var queryAdmins = adminRelation.query();

        queryAdmins.equalTo('objectId', Parse.User.current().id);
        queryAdmins.first({
            success: function(result) {    // User Object
                var user = result;
                user ? authorized = true : console.log('Shiet, user not Admin');
            }
        });
    },
    error: function(error) {
        console.log("Bruh, can't find the Admin role");
    }
}).then(function() {
    console.log('After test: Auth = ' + authorized);
});
like image 70
Bryan Bailes Avatar answered Sep 22 '22 18:09

Bryan Bailes


I got a simpler solution, give this a try:

var adminRoleQuery = new Parse.Query(Parse.Role);
adminRoleQuery.equalTo('name', 'admin');
adminRoleQuery.equalTo('users', req.user);

return adminRoleQuery.first().then(function(adminRole) {
  if (!adminRole) {
    throw new Error('Not an admin');
  }
});
like image 28
gfpacheco Avatar answered Sep 21 '22 18:09

gfpacheco