Get Permission scopes in JS Policy

Hello,

I have created a JS policy and associated it to a Scope based permission, but when trying to get the permission scopes using $evaluation.getPermission().getScopes(), I get unexpected behaviour. Although js detects that scopes is an array of ScopeAdapters, when trying to fetch the first scope it returns undefined.

Code:

var scopes = $evaluation.getPermission().getScopes();

print('SCOPES: ' + scopes);
print('SCOPES: ' + scopes[0]);
print('SCOPES: ' + scopes[0].getName());

Result

SCOPES: [org.keycloak.models.cache.infinispan.authorization.ScopeAdapter@70b5a43f]
SCOPES: undefined
(error stack trace)
TypeError: Cannot read property "getName" from undefined in <eval>
``
1 Like

Hi, Pedro, I faced the same issue. Do you have some news about it?

We had to use this workaround:

var requestedScopes = toJsArray($evaluation.getPermission().getScopes(), function (scope) {
    return scope.getName();
});

function toJsArray(array, modifier) {
    var result = [];
    array.forEach(function (element) {
        result.push(modifier(element));
    });
    return result;
}
1 Like

Thank you so much! It works now