public List<String> getGrantedRoles(String name)
{
Set<String> userRoles = new HashSet<String>();
InitialLdapContext ctx = null;
try
{
ctx = initialiseContext();
String userFilter = "(" + getUserNameAttribute() + "={0})";
String[] roleAttr = { getUserRoleAttribute() };
SearchControls controls = new SearchControls();
controls.setSearchScope(searchScope);
controls.setReturningAttributes(roleAttr);
controls.setTimeLimit(getSearchTimeLimit());
Object[] filterArgs = {name};
NamingEnumeration answer = ctx.search(getUserContextDN(), userFilter, filterArgs, controls);
while (answer.hasMore())
{
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
Attribute roles = attrs.get( getUserRoleAttribute() );
if (roles != null)
{
for (int r = 0; r < roles.size(); r++)
{
Object value = roles.get(r);
String roleName = null;
if (getRoleAttributeIsDN() == true)
{
String roleDN = value.toString();
String[] returnAttribute = {getRoleNameAttribute()};
try
{
Attributes result2 = ctx.getAttributes(roleDN, returnAttribute);
Attribute roles2 = result2.get(getRoleNameAttribute());
if( roles2 != null )
{
for(int m = 0; m < roles2.size(); m ++)
{
roleName = (String) roles2.get(m);
userRoles.add(roleName);
}
}
}
catch (NamingException ex)
{
throw new IdentityManagementException("Failed to query roles", ex);
}
}
else
{
// The role attribute value is the role name
roleName = value.toString();
userRoles.add(roleName);
}
}
}
}
answer.close();
return new ArrayList<String>(userRoles);
}
catch (NamingException ex)
{
throw new IdentityManagementException("Error getting roles", ex);
}
finally
{
if (ctx != null)
{
try
{
ctx.close();
}
catch (NamingException ex) {}
}
}
}