role = role.toUpperCase();
UserEntity userEntity = getUserEntity(email);
if (null == userEntity) {
throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST,
"Could not find user with email [" + email + "]");
}
RoleEntity roleEntity = new RoleRepository().getRoleEntity(role);
if (null == roleEntity) {
throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST,
"Could not find role [" + role + "]");
}
if (null == userEntity.roles)
userEntity.roles = new ArrayList<Ref<RoleEntity>>();
int matchingIndex = -1;
boolean alreadyAssigned = false;
for (int i = 0; i < userEntity.roles.size(); i++) {
if (role.endsWith(userEntity.roles.get(i).get().name)) {
alreadyAssigned = true;
matchingIndex = i;
}
}
if (assign && alreadyAssigned) {
throw new APIException(Status.ERROR_RESOURCE_ALREADY_EXISTS,
"role [" + role + "] is already assigned to user [" + email + "]");
}
if (!assign && !alreadyAssigned) {
throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST,
"role [" + role + "] is not assigned to user [" + email + "]");
}
if (assign) {
userEntity.roles.add(Ref.create(roleEntity));