Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

so here is the situation i currently am having trouble with.

I want to check if a user has permission to view a page, with one single function.

So I have an array with key/values where i store permissions in.

{"authorities":[{"role":"gebruikersbeheer"},{"role":"kijken"},{"role":"plannen"}]};

Stored in service.currentUser, which can be called by using service.currentUser.authorities.

I have a function:

hasPermission: function (permission) {
            var role = { "role" : permission };
            for(perm in service.currentUser.authorities)
            {
                var test = service.currentUser.authorities[perm];
                if(test === role)
                {
                    return (!!(service.currentUser) && true);
                }
            }
            return false;
        }

I created the test variable to debug its value. And the permission parameter has the value 'gebruikersbeheer'.

Now i want to compare the value of the perm key with the role and check if it is true. Now i have searched along the internet to do so, and i only found solutions which are not viable for me nor implementable.

When i start debugging my perm has an integer value. Why is it not the name of the key? (Which must be "role") Also, when i use Watch on the role and test variables they are completely the same but still i cant compare them and get a true.

(Dont mind the return statement at this point.) Also i cannot modify the array. It is returned by spring security this way.

Is there any (nicer) way to check if authorities contains role? It might be duplicate, but i couldnt find anything for my specific situation.

Cheers.

share|improve this question
up vote 2 down vote accepted

You currently are check a object with another object, is best check the string with the string, below show an little example of the same function but using the some method from arrays;

var currentUser =  {"authorities":[{"role":"gebruikersbeheer"},{"role":"kijken"},{"role":"plannen"}]};

function hasPermission(permission) {
  return currentUser.authorities.some(function(v){
    return v.role === permission;
  });
}

alert("Have permission? "+ hasPermission("gebruikersbeheer"))

share|improve this answer
    
Works like a charm. We didnt come up with the "some" method. This is the shortest piece of code so i marked this as the answer. Thanks a lot! – Clemenz Mar 5 '15 at 14:32

service.currentUser.authorities is an array this is why you're getting an integer for perm in for(perm in service.currentUser.authorities).

The other problem is that you can't compare all the properties in the object using === (including prototype properties), so you need to compare explicit the values for the properties... or create a custom function to compare your objects.

You can try with:

     hasPermission: function (permission) {
        var role = { "role" : permission };
        for(perm in service.currentUser.authorities)
        {
            var test = service.currentUser.authorities[perm];
            if(test["role"] === role["role"])
            {
                return (!!(service.currentUser) && true);
            }
        }
        return false;
    }

Hope this helps,

share|improve this answer
    
This also works. I marked the other answer as the solution because it is shorter. But this did help me understand arrays and key values. Thanks! – Clemenz Mar 5 '15 at 14:33
    
@Clemenz no problem :), nice to help you. – albciff Mar 5 '15 at 15:08

Since you are just concerned with whether something exists or not, you can use the Array.Some function. It still loops through the array, but will stop upon hitting the first instance that is true. Your method is basically doing the same thing already, but this way it a bit cleaner, since it uses the actual elements of the array (instead of an index).

hasPermission: function (permission) {
    var authArray = service.currentUser.authorities;

    var hasAuth = authArray.some(function(kvp) {
        return kvp.role == permission; //function to determine a match
    });
    return hasAuth; 
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.