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