📄 webuser.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.wstore;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.http.*;
import org.compiere.model.*;
import org.compiere.util.*;
/**
* Web User Info.
* Assumes that Email is a direct match.
* UPDATE AD_User SET EMail=TRIM(EMail) WHERE Email<>TRIM(EMail)
* @author Jorg Janke
* @version $Id: WebUser.java,v 1.51 2006/01/23 04:55:51 jjanke Exp $
*/
public class WebUser
{
/**
* Get from request
* @param request request
* @return web user if exists
*/
public static WebUser get (HttpServletRequest request)
{
HttpSession session = request.getSession(false);
if (session == null)
return null;
return (WebUser)session.getAttribute(WebUser.NAME);
} // get
/**
* Get user unconditional from cache
* @param ctx context
* @param email email
* @return web user
*/
public static WebUser get (Properties ctx, String email)
{
return get (ctx, email, null, true);
} // get
/**
* Get user
* @param ctx context
* @param email email
* @param password optional password
* @param useCache use cache
* @return web user
*/
public static WebUser get (Properties ctx, String email, String password, boolean useCache)
{
if (!useCache)
s_cache = null;
if (s_cache != null && email != null && email.equals(s_cache.getEmail()))
{
// if password is null, don't check it
if (password == null || password.equals(s_cache.getPassword()))
return s_cache;
s_cache.setPasswordOK(false, null);
return s_cache;
}
s_cache = new WebUser (ctx, email, password);
return s_cache;
} // get
/**
* Get user unconditional (from cache)
* @param ctx context
* @param AD_User_ID BP Contact
* @return web user
*/
public static WebUser get (Properties ctx, int AD_User_ID)
{
if (s_cache != null && s_cache.getAD_User_ID() == AD_User_ID)
return s_cache;
s_cache = new WebUser (ctx, AD_User_ID, null);
return s_cache;
} // get
/** Short term Cache for immediate re-query/post (hit rate 20%) */
private static WebUser s_cache = null;
/*************************************************************************/
/** Attribute Name - also in JSPs */
public static final String NAME = "webUser";
/** Logging */
private CLogger log = CLogger.getCLogger(getClass());
/**
* Load User with password
* @param ctx context
* @param email email
* @param password password
*/
private WebUser (Properties ctx, String email, String password)
{
m_ctx = ctx;
m_AD_Client_ID = Env.getAD_Client_ID(ctx);
load (email, password);
} // WebUser
/**
* Load User with password
* @param ctx context
* @param AD_User_ID BP Contact
*/
private WebUser (Properties ctx, int AD_User_ID, String trxName)
{
m_ctx = ctx;
m_AD_Client_ID = Env.getAD_Client_ID(ctx);
load (AD_User_ID);
} // WebUser
private Properties m_ctx;
//
private MBPartner m_bp;
private MUser m_bpc;
private MBPartnerLocation m_bpl;
private MLocation m_loc;
//
private boolean m_passwordOK = false;
private String m_passwordMessage;
private String m_saveErrorMessage;
//
private int m_AD_Client_ID = 0;
private boolean m_loggedIn = false;
/**
* Load Contact
* @param email email
* @param password optional password
*/
private void load (String email, String password)
{
log.info(email + " - AD_Client_ID=" + m_AD_Client_ID);
String sql = "SELECT * "
+ "FROM AD_User "
+ "WHERE AD_Client_ID=?"
+ " AND TRIM(EMail)=?";
if (email == null)
email = "";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_AD_Client_ID);
pstmt.setString(2, email.trim());
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
m_bpc = new MUser (m_ctx, rs, null);
log.fine("Found BPC=" + m_bpc);
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
// Check Password
m_passwordOK = false;
/** User has no password
if (m_bpc != null && m_bpc.getPassword() == null)
{
if (password != null)
m_bpc.setPassword(password);
m_passwordOK = true;
} **/
// We have a password
if (m_bpc != null && password != null && password.equals(m_bpc.getPassword()))
m_passwordOK = true;
if (m_passwordOK || m_bpc == null)
m_passwordMessage = null;
else
setPasswordOK (false, password);
// Load BPartner
if (m_bpc != null)
{
m_bp = new MBPartner (m_ctx, m_bpc.getC_BPartner_ID (), null);
log.fine("Found BP=" + m_bp);
}
else
m_bp = null;
// Load Loacation
if (m_bpc != null)
{
if (m_bpc.getC_BPartner_Location_ID() != 0)
{
m_bpl = new MBPartnerLocation (m_ctx, m_bpc.getC_BPartner_Location_ID (), null);
log.fine("Found BPL=" + m_bpl);
}
else
{
MBPartnerLocation[] bpls = m_bp.getLocations(false);
if (bpls != null && bpls.length > 0)
{
m_bpl = bpls[0];
log.fine("Found BPL=" + m_bpl);
}
}
if (m_bpl != null)
{
m_loc = MLocation.get (m_ctx, m_bpl.getC_Location_ID(), null);
log.fine("Found LOC=" + m_loc);
}
else
m_loc = null;
}
else
{
m_bpl = null;
m_loc = null;
}
// Make sure that all entities exist
if (m_bpc == null)
{
m_bpc = new MUser (m_ctx, 0, null);
m_bpc.setEMail(email);
m_bpc.setPassword(password);
}
if (m_bp == null)
{
m_bp = new MBPartner (m_ctx); // template
m_bp.setIsCustomer(true);
}
if (m_bpl == null)
m_bpl = new MBPartnerLocation (m_bp);
if (m_loc == null)
m_loc = new MLocation (m_ctx, 0, null);
//
log.config(m_bp + " - " + m_bpc);
} // load
/**
* Load Contact
* @param AD_User_ID BP Contact
*/
private void load (int AD_User_ID)
{
log.info("ID=" + AD_User_ID + ", AD_Client_ID=" + m_AD_Client_ID);
String sql = "SELECT * "
+ "FROM AD_User "
+ "WHERE AD_Client_ID=?"
+ " AND AD_User_ID=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_AD_Client_ID);
pstmt.setInt(2, AD_User_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
m_bpc = new MUser (m_ctx, rs, null);
log.fine("= found BPC=" + m_bpc);
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
// Password not entered
m_passwordOK = false;
m_loggedIn = false;
// Load BPartner
if (m_bpc != null)
{
m_bp = new MBPartner (m_ctx, m_bpc.getC_BPartner_ID (), null);
log.fine("= Found BP=" + m_bp);
}
else
m_bp = null;
// Load Loacation
if (m_bpc != null)
{
if (m_bpc.getC_BPartner_Location_ID() != 0)
{
m_bpl = new MBPartnerLocation (m_ctx, m_bpc.getC_BPartner_Location_ID (), null);
log.fine("= Found BPL=" + m_bpl);
}
else
{
MBPartnerLocation[] bpls = m_bp.getLocations(false);
if (bpls != null && bpls.length > 0)
{
m_bpl = bpls[0];
log.fine("= Found BPL=" + m_bpl);
}
}
if (m_bpl != null)
{
m_loc = MLocation.get (m_ctx, m_bpl.getC_Location_ID(), null);
log.fine("= Found LOC=" + m_loc);
}
else
m_loc = null;
}
else
{
m_bpl = null;
m_loc = null;
}
// Make sure that all entities exist
if (m_bpc == null)
{
m_bpc = new MUser (m_ctx, 0, null);
m_bpc.setEMail("?");
m_bpc.setPassword("?");
}
if (m_bp == null)
{
m_bp = new MBPartner (m_ctx); // template
m_bp.setIsCustomer(true);
}
if (m_bpl == null)
m_bpl = new MBPartnerLocation (m_bp);
if (m_loc == null)
m_loc = new MLocation (m_ctx, 0, null);
//
log.info("= " + m_bp + " - " + m_bpc);
} // load
/**
* Return Valid.
* @return return true if found
*/
public boolean isValid()
{
if (m_bpc == null)
return false;
boolean ok = m_bpc.getAD_User_ID() != 0;
return ok;
} // isValid
/**
* Return Email Validation.
* @return return true if email is valid
*/
public boolean isEMailValid()
{
if (m_bpc == null || !WebUtil.exists(getEmail()))
{
log.fine(getEmail() + ", bpc=" + m_bpc);
return false;
}
//
boolean ok = m_bpc.getAD_User_ID() != 0
&& m_bpc.isEMailValid();
if (!ok)
log.fine(getEmail()
+ ", ID=" + m_bpc.getAD_User_ID()
+ ", Online=" + m_bpc.isOnline()
+ ", EMailValid=" + m_bpc.isEMailValid());
return ok;
} // isEMailValid
/**
* Return Email Verification (reply).
* @return return true if reply received
*/
public boolean isEMailVerified()
{
return m_bpc != null
&& m_bpc.isEMailVerified();
} // isEMailVerified
/**
* Info
* @return info
*/
public String toString ()
{
StringBuffer sb = new StringBuffer("WebUser[");
sb.append(getEmail())
.append(",LoggedIn=").append(m_loggedIn)
.append(",").append(m_bpc)
.append(",PasswordOK=").append(m_passwordOK)
.append(",Valid=").append(isValid())
.append(" - ").append(m_bp).append("Customer=").append(isCustomer())
.append("]");
return sb.toString();
} // toString
/**************************************************************************
* Save BPartner Objects
* @return true if saved
*/
public boolean save()
{
m_saveErrorMessage = null;
log.info("BP.Value=" + m_bp.getValue() + ", Name=" + m_bp.getName());
try
{
// check if BPartner exists ***********************************
if (m_bp.getC_BPartner_ID() == 0)
{
String sql = "SELECT * FROM C_BPartner WHERE AD_Client_ID=? AND Value=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt (1, m_AD_Client_ID);
pstmt.setString (2, m_bp.getValue());
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
m_bp = new MBPartner (m_ctx, m_bpc.getC_BPartner_ID (), null);
log.fine("BP loaded =" + m_bp);
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, "save-check", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
}
// save BPartner ***************************************
if (m_bp.getName () == null || m_bp.getName().length() == 0)
m_bp.setName (m_bpc.getName());
if (m_bp.getValue() == null || m_bp.getValue().length() == 0)
m_bp.setValue(m_bpc.getEMail());
log.fine("BP=" + m_bp);
if (!m_bp.save ())
{
m_saveErrorMessage = "Could not save Business Partner";
return false;
}
// save Location ***************************************
log.fine("LOC=" + m_loc);
m_loc.save ();
// save BP Location ***************************************
if (m_bpl.getC_BPartner_ID () != m_bp.getC_BPartner_ID())
m_bpl.setC_BPartner_ID (m_bp.getC_BPartner_ID());
if (m_bpl.getC_Location_ID () != m_loc.getC_Location_ID())
m_bpl.setC_Location_ID (m_loc.getC_Location_ID());
log.fine("BPL=" + m_bpl);
if (!m_bpl.save ())
{
m_saveErrorMessage = "Could not save Location";
return false;
}
// save Contact ***************************************
if (m_bpc.getC_BPartner_ID () != m_bp.getC_BPartner_ID())
m_bpc.setC_BPartner_ID (m_bp.getC_BPartner_ID());
if (m_bpc.getC_BPartner_Location_ID () != m_bpl.getC_BPartner_Location_ID ())
m_bpc.setC_BPartner_Location_ID (m_bpl.getC_BPartner_Location_ID ());
log.fine("BPC=" + m_bpc);
if (!m_bpc.save ())
{
m_saveErrorMessage = "Could not save Contact";
return false;
}
}
catch (Exception ex)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -