[英]Comparing objects from key/value array with another object (Key/value)
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
. 存储在
service.currentUser
,可以使用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. 我创建了
test
变量以调试其值。 And the permission
parameter has the value 'gebruikersbeheer'. permission
参数的值为'gebruikersbeheer'。
Now i want to compare the value of the perm
key with the role
and check if it is true. 现在,我想将
perm
键的值与role
进行比较,并检查它是否为真。 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. 当我开始调试时,我的
perm
有一个整数值。 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
. (必须是“角色”)此外,当我在
role
和test
变量上使用Watch时,它们是完全相同的,但仍然无法比较它们并得出true
。
(Dont mind the return statement at this point.) Also i cannot modify the array. (此时不要在意return语句。)我也无法修改数组。 It is returned by spring security this way.
这样,Spring Security会将其返回。
Is there any (nicer) way to check if authorities
contains role
? 有什么(更简单的方法)检查
authorities
包含role
? It might be duplicate, but i couldnt find anything for my specific situation. 它可能是重复的,但我找不到适合自己特定情况的任何东西。
Cheers. 干杯。
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; 您当前正在用另一个对象检查一个对象,最好是用该字符串检查该字符串,下面显示了相同功能的一个小例子,但使用了数组中的some方法;
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"))
service.currentUser.authorities
is an array this is why you're getting an integer for perm
in for(perm in service.currentUser.authorities)
. service.currentUser.authorities
是一个数组,这就是为什么要在for(perm in service.currentUser.authorities)
获得perm
的整数的原因。
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, 希望这可以帮助,
Since you are just concerned with whether something exists or not, you can use the Array.Some function. 由于您只关心是否存在某些内容,因此可以使用Array.Some函数。 It still loops through the array, but will stop upon hitting the first instance that is true.
它仍然循环遍历数组,但是在遇到第一个为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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.