* @param roleName - the role to add to user's roleEntities
*/
private void addRole(UserEntity user, String roleName) {
log.info("Using default role name " + roleName);
RoleEntity roleEntity = roleDAO.findByName(roleName);
if (roleEntity == null) {
log.info("Role " + roleName + " not present in local DB - creating");
roleEntity = new RoleEntity();
roleEntity.setRoleName(roleName);
roleDAO.create(roleEntity);
roleEntity = roleDAO.findByName(roleEntity.getRoleName());
}
UserEntity userEntity = userDAO.findLdapUserByName(user.getUserName());
if (userEntity == null) {
userDAO.create(user);
userEntity = userDAO.findLdapUserByName(user.getUserName());
}
if (!userEntity.getRoleEntities().contains(roleEntity)) {
userEntity.getRoleEntities().add(roleEntity);
roleEntity.getUserEntities().add(userEntity);
roleDAO.merge(roleEntity);
userDAO.merge(userEntity);
}
}