{
// use admin
TestContainer.getInstance().getTestContext().useAdminForRequests();
// now give create
RoleResource role = null;
// first try to retrieve
for (RoleResource roleResource : roleUtil.getList()) {
if (roleResource.getName().equals(newRoleName)) {
role = roleResource;
role.getPrivileges().clear();
for (String priv : permissions) {
role.addPrivilege(priv);
}
// update the permissions
RoleMessageUtil.update(role);
break;
}
}
// if doesn't exist, create it
if (role == null) {
role = new RoleResource();
role.setDescription(newRoleName);
role.setName(newRoleName);
role.setSessionTimeout(60);
for (String priv : permissions) {
role.addPrivilege(priv);
}
// save it
role = this.roleUtil.createRole(role);
}
// add it
UserResource testUser = this.userUtil.getUser(userId);
testUser.getRoles().clear();
testUser.addRole(role.getId());
this.userUtil.updateUser(testUser);
}