/**
   * Do modify the reset password attribute
   *
   * @param user the DatabaseUser
   * @param bNewValue the new value
   * @param plugin the plugin
   */
  public void doModifyResetPassword(DatabaseUser user, boolean bNewValue, Plugin plugin) {
    DatabaseUser userStored = DatabaseUserHome.findByPrimaryKey(user.getUserId(), plugin);

    if (userStored != null) {
      DatabaseUserHome.updateResetPassword(userStored, bNewValue, plugin);
    }
  }
  /**
   * Update the user expiration date with new values, and notify him with an email.
   *
   * @param nIdUser Id of the user to update
   * @param plugin The plugin
   */
  @SuppressWarnings("deprecation")
  public void updateUserExpirationDate(int nIdUser, Plugin plugin) {
    // We update the user account
    int nbMailSend = DatabaseUserHome.getNbAccountLifeTimeNotification(nIdUser, plugin);
    Timestamp newExpirationDate = SecurityUtils.getAccountMaxValidDate(_userParamService, plugin);
    DatabaseUserHome.updateUserExpirationDate(nIdUser, newExpirationDate, plugin);

    // We notify the user
    DatabaseAccountLifeTimeService accountLifeTimeService = new DatabaseAccountLifeTimeService();
    String strUserMail = accountLifeTimeService.getUserMainEmail(nIdUser);

    if ((nbMailSend > 0) && StringUtils.isNotBlank(strUserMail)) {
      String strBody =
          DatabaseTemplateService.getTemplateFromKey(PARAMETER_ACCOUNT_REACTIVATED_MAIL_BODY);

      ReferenceItem referenceItem =
          _userParamService.findByKey(PARAMETER_ACCOUNT_REACTIVATED_MAIL_SENDER, plugin);
      String strSender = (referenceItem == null) ? StringUtils.EMPTY : referenceItem.getName();

      referenceItem =
          _userParamService.findByKey(PARAMETER_ACCOUNT_REACTIVATED_MAIL_SUBJECT, plugin);

      String strSubject = (referenceItem == null) ? StringUtils.EMPTY : referenceItem.getName();

      Map<String, String> model = new HashMap<String, String>();
      accountLifeTimeService.addParametersToModel(model, nIdUser);

      HtmlTemplate template =
          AppTemplateService.getTemplateFromStringFtl(strBody, Locale.getDefault(), model);
      MailService.sendMailHtml(strUserMail, strSender, strSender, strSubject, template.getHtml());
    }
  }
  /**
   * Check the password
   *
   * @param strUserGuid the user guid
   * @param strPassword the password
   * @param plugin the plugin
   * @return true if the password is the same as stored in the database, false otherwise
   */
  public boolean checkPassword(String strUserGuid, String strPassword, Plugin plugin) {
    String strEncryptedPassword = strPassword;

    if (_userParamService.isPasswordEncrypted(plugin)) {
      String strAlgorithm = _userParamService.getEncryptionAlgorithm(plugin);
      strEncryptedPassword = CryptoService.encrypt(strPassword, strAlgorithm);
    }

    return DatabaseUserHome.checkPassword(strUserGuid, strEncryptedPassword, plugin);
  }
  /**
   * Do modify the password
   *
   * @param user the DatabaseUser
   * @param strPassword the new password not encrypted
   * @param plugin the plugin
   */
  public void doModifyPassword(DatabaseUser user, String strPassword, Plugin plugin) {
    // Updates password
    if (StringUtils.isNotBlank(strPassword)) {
      // Encrypts password or not
      String strEncryptedPassword = strPassword;

      if (_userParamService.isPasswordEncrypted(plugin)) {
        String strAlgorithm = _userParamService.getEncryptionAlgorithm(plugin);
        strEncryptedPassword = CryptoService.encrypt(strPassword, strAlgorithm);
      }

      DatabaseUser userStored = DatabaseUserHome.findByPrimaryKey(user.getUserId(), plugin);

      if (userStored != null) {
        userStored.setPasswordMaxValidDate(
            SecurityUtils.getPasswordMaxValidDate(_userParamService, plugin));
        DatabaseUserHome.updatePassword(userStored, strEncryptedPassword, plugin);
      }
    }
  }
  /**
   * Get authorized users list
   *
   * @param adminUser the admin user
   * @param plugin the plugin
   * @return a list of users
   */
  public List<DatabaseUser> getAuthorizedUsers(AdminUser adminUser, Plugin plugin) {
    Collection<DatabaseUser> userList = DatabaseUserHome.findDatabaseUsersList(plugin);
    List<DatabaseUser> authorizedUserList = new ArrayList<DatabaseUser>();

    for (DatabaseUser user : userList) {
      if (isAuthorized(user, adminUser, plugin)) {
        authorizedUserList.add(user);
      }
    }

    return authorizedUserList;
  }
  /**
   * Check if the user is active or not
   *
   * @param strUserName the user name
   * @param plugin the plugin
   * @return true if it is active, false otherwise
   */
  public boolean isUserActive(String strUserName, Plugin plugin) {
    boolean bIsActive = false;

    List<DatabaseUser> listUsers =
        (List<DatabaseUser>) DatabaseUserHome.findDatabaseUsersListForLogin(strUserName, plugin);

    if ((listUsers != null) && !listUsers.isEmpty()) {
      DatabaseUser user = listUsers.get(0);
      bIsActive = user.isActive();
    }

    return bIsActive;
  }
  /**
   * Do create a new database user
   *
   * @param user the user
   * @param strPassword the password
   * @param plugin the plugin
   * @return the new database user with a new ID
   */
  public DatabaseUser doCreateUser(DatabaseUser user, String strPassword, Plugin plugin) {
    String strEncryptedPassword = strPassword;

    if (_userParamService.isPasswordEncrypted(plugin)) {
      String strAlgorithm = _userParamService.getEncryptionAlgorithm(plugin);
      strEncryptedPassword = CryptoService.encrypt(strPassword, strAlgorithm);
    }

    user.setPasswordMaxValidDate(SecurityUtils.getPasswordMaxValidDate(_userParamService, plugin));
    user.setAccountMaxValidDate(SecurityUtils.getAccountMaxValidDate(_userParamService, plugin));

    return DatabaseUserHome.create(user, strEncryptedPassword, plugin);
  }
  /**
   * Change all user's password and notify them with an email.
   *
   * @param strBaseURL The base url of the application
   * @param plugin The plugin
   * @param locale The locale to use
   */
  public void changeUserPasswordAndNotify(String strBaseURL, Plugin plugin, Locale locale) {
    // Alert all users their password have been reinitialized.
    Collection<DatabaseUser> listUsers = DatabaseUserHome.findDatabaseUsersList(plugin);

    for (DatabaseUser user : listUsers) {
      // Makes password
      String strPassword = SecurityUtils.makePassword(_userParamService, plugin);
      doModifyPassword(user, strPassword, plugin);

      if (StringUtils.isNotBlank(user.getEmail())) {
        // Sends password by e-mail
        ReferenceItem referenceItem =
            _userParamService.findByKey(PARAMETER_MAIL_PASSWORD_ENCRYPTION_CHANGED_SENDER, plugin);
        String strSenderEmail =
            (referenceItem == null) ? StringUtils.EMPTY : referenceItem.getName();
        referenceItem =
            _userParamService.findByKey(PARAMETER_MAIL_PASSWORD_ENCRYPTION_CHANGED_SUBJECT, plugin);

        String strEmailSubject =
            (referenceItem == null) ? StringUtils.EMPTY : referenceItem.getName();

        Map<String, Object> model = new HashMap<String, Object>();
        model.put(MARK_NEW_PASSWORD, strPassword);
        model.put(
            MARK_LOGIN_URL,
            strBaseURL + AdminAuthenticationService.getInstance().getLoginPageUrl());
        model.put(MARK_SITE_LINK, MailService.getSiteLink(strBaseURL, true));

        String strTemplate =
            DatabaseTemplateService.getTemplateFromKey(PARAMETER_MAIL_PASSWORD_ENCRYPTION_CHANGED);

        HtmlTemplate template =
            AppTemplateService.getTemplateFromStringFtl(strTemplate, locale, model);

        MailService.sendMailHtml(
            user.getEmail(), strSenderEmail, strSenderEmail, strEmailSubject, template.getHtml());
      }
    }
  }
  /**
   * Get th list of filteredUsers
   *
   * @param request the HTTP request
   * @param duFilter the filter
   * @param listUsers the list of users
   * @return a list of {@link DatabaseUser}
   */
  public List<DatabaseUser> getListFilteredUsers(
      HttpServletRequest request, DatabaseUserFilter duFilter, List<DatabaseUser> listUsers) {
    Plugin plugin = PluginService.getPlugin(DatabasePlugin.PLUGIN_NAME);

    List<DatabaseUser> listFilteredUsers =
        DatabaseUserHome.findDatabaseUsersListByFilter(duFilter, plugin);
    List<DatabaseUser> listAvailableUsers = new ArrayList<DatabaseUser>();

    for (DatabaseUser filteredUser : listFilteredUsers) {
      for (DatabaseUser user : listUsers) {
        if (filteredUser.getUserId() == user.getUserId()) {
          listAvailableUsers.add(user);
        }
      }
    }

    Plugin myLutecePlugin = PluginService.getPlugin(MyLutecePlugin.PLUGIN_NAME);
    List<DatabaseUser> filteredUsers = new ArrayList<DatabaseUser>();

    MyLuteceUserFieldFilter mlFieldFilter = new MyLuteceUserFieldFilter();
    mlFieldFilter.setMyLuteceUserFieldFilter(request, request.getLocale());

    List<Integer> listFilteredUserIdsByUserFields =
        MyLuteceUserFieldHome.findUsersByFilter(mlFieldFilter, myLutecePlugin);

    if (listFilteredUserIdsByUserFields != null) {
      for (DatabaseUser filteredUser : listAvailableUsers) {
        for (Integer nFilteredUserIdByUserField : listFilteredUserIdsByUserFields) {
          if (filteredUser.getUserId() == nFilteredUserIdByUserField) {
            filteredUsers.add(filteredUser);
          }
        }
      }
    } else {
      filteredUsers = listAvailableUsers;
    }

    return filteredUsers;
  }
 /**
  * Update a user last login date.
  *
  * @param strLogin Login of the user to update
  * @param plugin The plugin
  */
 public void updateUserLastLoginDate(String strLogin, Plugin plugin) {
   DatabaseUserHome.updateUserLastLoginDate(strLogin, new Date(), plugin);
 }
 /**
  * Log a password change in the password history
  *
  * @param strPassword New password of the user
  * @param nUserId Id of the user
  * @param plugin The plugin
  */
 public void doInsertNewPasswordInHistory(String strPassword, int nUserId, Plugin plugin) {
   strPassword = SecurityUtils.buildPassword(_userParamService, plugin, strPassword);
   DatabaseUserHome.insertNewPasswordInHistory(strPassword, nUserId, plugin);
 }
 /**
  * Update the info of the user
  *
  * @param user the user
  * @param plugin the plugin
  */
 public void doUpdateUser(DatabaseUser user, Plugin plugin) {
   DatabaseUserHome.update(user, plugin);
 }