@Override
  protected final UserDetails retrieveUser(
      String username, UsernamePasswordAuthenticationToken authentication)
      throws AuthenticationException {
    UserDetails loadedUser;

    try {
      loadedUser = this.getUserDetailsService().loadUserByUsername(username);

      ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
      grantedAuthorities.add(ROLE_USER);

      com.sfr.applications.cooper.core.identity.models.User user =
          identityService.getUserByUserName(username);
      logger.debug("user found for (username={} ): {}", username, user);
      if (user != null && user.getGroups() != null && STATUS_ENABLED.equals(user.getStatus())) {
        Set<Group> groups = user.getGroups();
        logger.debug("groups found for (username={} ): {}", username, groups);

        for (Iterator<Group> iterator = groups.iterator(); iterator.hasNext(); ) {
          Group group = iterator.next();
          logger.debug("group: {}", group);
          String role = StringUtils.capitalize("ROLE_" + group.getGroupName());
          grantedAuthorities.add(new GrantedAuthorityImpl(role));
        }

        user.setLasConnectionDate(new Date());
        identityService.updateUser(user);
        logger.debug("user updated for (username={} ): {}", username, user);
      } else {
        throw new UsernameNotFoundException("user with username " + username + " is not found");
      }

      UserDetails userDetails =
          new User(username, loadedUser.getPassword(), true, true, true, true, grantedAuthorities);
      logger.debug("loadUserDetails(username={} ): {}", username, userDetails);
      loadedUser = userDetails;
    } catch (DataAccessException repositoryProblem) {
      throw new AuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem);
    }

    if (loadedUser == null) {
      throw new AuthenticationServiceException(
          "UserDetailsService returned null, which is an interface contract violation");
    }
    return loadedUser;
  }
 public boolean isEnabled() {
   return STATUS_ENABLED.equals(status);
 }