public void updateRolesOfUser(String userName, String[] roleList) throws UserAdminException {
try {
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equals(userName)) {
log.error("Security Alert! Carbon anonymous user is being manipulated");
throw new UserAdminException("Invalid data");// obscure error
// message
}
UserStoreManager admin = realm.getUserStoreManager();
String[] oldRoleList = admin.getRoleListOfUser(userName);
Arrays.sort(roleList);
Arrays.sort(oldRoleList);
List<String> delRoles = new ArrayList<String>();
List<String> addRoles = new ArrayList<String>();
for (String name : roleList) {
int oldindex = Arrays.binarySearch(oldRoleList, name);
if (oldindex < 0) {
addRoles.add(name);
}
}
for (String name : oldRoleList) {
int newindex = Arrays.binarySearch(roleList, name);
if (newindex < 0) {
if (realm.getRealmConfiguration().getEveryOneRoleName().equals(name)) {
log.error("Security Alert! Carbon everyone role is being manipulated");
throw new UserAdminException("Invalid data");// obscure
// error
// message
}
delRoles.add(name);
}
}
admin.updateRoleListOfUser(userName, delRoles.toArray(new String[delRoles.size()]),
addRoles.toArray(new String[addRoles.size()]));
} catch (UserStoreException e) {
// previously logged so logging not needed
throw new UserAdminException(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new UserAdminException(e.getMessage(), e);
}
}