IDEMPIERE-375 Implement Forgot my Password / Fix problems when login with email

This commit is contained in:
Carlos Ruiz 2012-09-13 18:54:59 -05:00
parent f9ca3bc142
commit a688a3808d
1 changed files with 85 additions and 87 deletions

View File

@ -81,6 +81,8 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
private boolean m_noSecurityQuestion;
/** Tries Counter */
private int counter;
/** EMail Login preference */
boolean m_email_login = false;
private Label lblSecurityQuestion;
private Label lblAnswer;
@ -97,6 +99,7 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
m_ctx = ctx;
m_userName = userName;
m_noSecurityQuestion = noSecurityQuestion;
m_email_login = MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN, false);
initComponents();
init();
@ -132,8 +135,6 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
image.setSrc(ThemeManager.getLargeLogo());
td.appendChild(image);
if (m_noSecurityQuestion)
{
tr = new Tr();
tr.setId("rowUser");
table.appendChild(tr);
@ -145,9 +146,7 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
td.setSclass(ITheme.LOGIN_FIELD_CLASS);
tr.appendChild(td);
td.appendChild(txtUserId);
}
else
{
tr = new Tr();
tr.setId("rowEmail");
table.appendChild(tr);
@ -160,6 +159,16 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
tr.appendChild(td);
td.appendChild(txtEmail);
if (m_email_login) {
lblEmail.setVisible(false);
txtEmail.setVisible(false);
} else {
lblUserId.setVisible(false);
txtUserId.setVisible(false);
}
if (! m_noSecurityQuestion)
{
tr = new Tr();
tr.setId("rowSecurityQuestion");
table.appendChild(tr);
@ -198,35 +207,11 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
}
private void initComponents()
{
if (m_noSecurityQuestion)
{
boolean email_login = MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN, false);
lblUserId = new Label();
lblUserId.setId("lblUserId");
lblUserId.setValue(email_login ? Msg.getMsg(m_ctx, "Name") : Msg.getMsg(m_ctx, "EMail"));
txtUserId = new Textbox();
txtUserId.setId("txtUserId");
txtUserId.setAttribute(AdempiereIdGenerator.ZK_COMPONENT_PREFIX_ATTRIBUTE, "unq" + txtUserId.getId());
txtUserId.setCols(25);
txtUserId.setMaxlength(40);
txtUserId.setWidth("220px");
}
else
{
lblEmail = new Label();
lblEmail.setId("lblEmail");
lblEmail.setValue(Msg.getMsg(m_ctx, "EMail"));
lblSecurityQuestion = new Label();
lblSecurityQuestion.setId("lblSecurityQuestion");
lblSecurityQuestion.setValue(Msg.getMsg(m_ctx, "SecurityQuestion"));
lblAnswer = new Label();
lblAnswer.setId("lblAnswer");
lblAnswer.setValue(Msg.getMsg(m_ctx, "Answer"));
txtEmail = new Textbox();
txtEmail.setId("txtEmail");
txtEmail.setAttribute(AdempiereIdGenerator.ZK_COMPONENT_PREFIX_ATTRIBUTE, "unq" + txtEmail.getId());
@ -234,6 +219,26 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
txtEmail.setWidth("220px");
txtEmail.setReadonly(false);
lblUserId = new Label();
lblUserId.setId("lblUserId");
lblUserId.setValue(Msg.getMsg(m_ctx, "User"));
txtUserId = new Textbox();
txtUserId.setId("txtUserId");
txtUserId.setAttribute(AdempiereIdGenerator.ZK_COMPONENT_PREFIX_ATTRIBUTE, "unq" + txtUserId.getId());
txtUserId.setCols(25);
txtUserId.setMaxlength(40);
txtUserId.setWidth("220px");
if (! m_noSecurityQuestion)
{
lblSecurityQuestion = new Label();
lblSecurityQuestion.setId("lblSecurityQuestion");
lblSecurityQuestion.setValue(Msg.getMsg(m_ctx, "SecurityQuestion"));
lblAnswer = new Label();
lblAnswer.setId("lblAnswer");
lblAnswer.setValue(Msg.getMsg(m_ctx, "Answer"));
txtSecurityQuestion = new Textbox();
txtSecurityQuestion.setId("txtSecurityQuestion");
txtSecurityQuestion.setAttribute(AdempiereIdGenerator.ZK_COMPONENT_PREFIX_ATTRIBUTE, "unq" + txtSecurityQuestion.getId());
@ -253,34 +258,31 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
private void loadData()
{
boolean email_login = MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN, false);
if (email_login)
if (m_email_login)
{
txtEmail.setText(m_userName);
loadSecurityQuestion();
} else {
txtUserId.setText(m_userName);
}
}
private void loadSecurityQuestion()
{
String email = txtEmail.getValue();
if (Util.isEmpty(email))
throw new IllegalArgumentException(Msg.getMsg(m_ctx, "FillMandatory") + " " + lblEmail.getValue());
String userid = txtUserId.getValue();
if (Util.isEmpty(email) || Util.isEmpty(userid))
throw new IllegalArgumentException(Msg.getMsg(m_ctx, "FillMandatory") + " " + (m_email_login ? lblUserId.getValue() : lblEmail.getValue()));
// TODO: Validation for user with same email uses the same password and security question
StringBuilder sql = new StringBuilder("SELECT SecurityQuestion ");
sql.append("FROM AD_User ");
sql.append("WHERE IsActive='Y' ");
boolean email_login = MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN, false);
if (email_login)
sql.append("AND EMail=? ");
else
sql.append("AND COALESCE(LDAPUser,Name)=? ");
sql.append("AND EMail=? ");
sql.append("AND SecurityQuestion IS NOT NULL ");
sql.append("ORDER BY AD_Client_ID DESC");
String securityQuestion = DB.getSQLValueString(null, sql.toString(), m_userName, email);
String securityQuestion = DB.getSQLValueString(null, sql.toString(), userid, email);
txtSecurityQuestion.setValue(securityQuestion);
txtEmail.setReadonly(true);
@ -306,8 +308,9 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
private void validateEmail()
{
String email = txtEmail.getValue();
if (Util.isEmpty(email))
throw new IllegalArgumentException(Msg.getMsg(m_ctx, "FillMandatory") + " " + lblEmail.getValue());
String userid = txtUserId.getValue();
if (Util.isEmpty(email) || Util.isEmpty(userid))
throw new IllegalArgumentException(Msg.getMsg(m_ctx, "FillMandatory") + " " + (m_email_login ? lblUserId.getValue() : lblEmail.getValue()));
StringBuilder whereClause = new StringBuilder("Password IS NOT NULL ");
whereClause.append("AND COALESCE(LDAPUser,Name)=? ");
@ -322,7 +325,7 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
.append(" AD_User.IsActive='Y'");
List<MUser> users = new Query(m_ctx, MUser.Table_Name, whereClause.toString(), null)
.setParameters(m_userName, email)
.setParameters(userid, email)
.setOrderBy(MUser.COLUMNNAME_AD_User_ID)
.list();
@ -334,25 +337,16 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
private void validateResetPassword()
{
String email = txtEmail.getValue();
String userid = txtUserId.getValue();
if (Util.isEmpty(email) || Util.isEmpty(userid))
throw new IllegalArgumentException(Msg.getMsg(m_ctx, "FillMandatory") + " " + (m_email_login ? lblUserId.getValue() : lblEmail.getValue()));
List<MUser> users = null;
if (m_noSecurityQuestion)
{
String userId = txtUserId.getValue();
if (Util.isEmpty(userId))
throw new IllegalArgumentException(Msg.getMsg(m_ctx, "FillMandatory") + " " + lblUserId.getValue());
StringBuilder whereClause = new StringBuilder("Password IS NOT NULL ");
boolean email_login = MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN, false);
if (email_login)
{
whereClause.append("AND EMail=? ");
whereClause.append("AND COALESCE(LDAPUser,Name)=? ");
}
else
{
whereClause.append("AND COALESCE(LDAPUser,Name)=? ");
whereClause.append("AND EMail=? ");
}
whereClause.append(" AND")
.append(" EXISTS (SELECT * FROM AD_User_Roles ur")
.append(" INNER JOIN AD_Role r ON (ur.AD_Role_ID=r.AD_Role_ID)")
@ -363,13 +357,12 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
.append(" AD_User.IsActive='Y'");
users = new Query(m_ctx, MUser.Table_Name, whereClause.toString(), null)
.setParameters(m_userName, userId)
.setParameters(userid, email)
.setOrderBy(MUser.COLUMNNAME_AD_User_ID)
.list();
}
else
{
String email = txtEmail.getValue();
String securityQuestion = txtSecurityQuestion.getValue();
String answer = txtAnswer.getValue();
@ -490,6 +483,11 @@ public class ResetPasswordPanel extends Window implements EventListener<Event>
mailText.setUser(to);
String message = mailText.getMailText(true);
message = Env.parseVariable(message, to, to.get_TrxName(), true);
/* BORRAR DEBUG ?? */
System.out.println(message);
if (true) return true;
EMail email = client.createEMail(to.getEMail(), mailText.getMailHeader(), message, mailText.isHtml());
if (mailText.isHtml())
email.setMessageHTML(mailText.getMailHeader(), message);