IDEMPIERE-2944 Preserve iDempiere session between sucessive webservice calls / fix Context Lost issue

This commit is contained in:
Carlos Ruiz 2018-07-09 14:43:18 +02:00
parent 31e1dead2c
commit 3f1026519f
3 changed files with 14 additions and 3 deletions

View File

@ -1324,6 +1324,7 @@ public class Login
user.setIsLocked(false); user.setIsLocked(false);
user.setDateAccountLocked(null); user.setDateAccountLocked(null);
user.setFailedLoginCount(0); user.setFailedLoginCount(0);
Env.setContext(Env.getCtx(), "#AD_Client_ID", user.getAD_Client_ID());
if (!user.save()) if (!user.save())
log.severe("Failed to unlock user account"); log.severe("Failed to unlock user account");
} }
@ -1337,6 +1338,7 @@ public class Login
{ {
user.setIsLocked(true); user.setIsLocked(true);
user.setDateAccountLocked(new Timestamp(now)); user.setDateAccountLocked(new Timestamp(now));
Env.setContext(Env.getCtx(), "#AD_Client_ID", user.getAD_Client_ID());
if (!user.save()) if (!user.save())
log.severe("Failed to lock user account"); log.severe("Failed to lock user account");
} }
@ -1441,6 +1443,7 @@ public class Login
{ {
user.setFailedLoginCount(0); user.setFailedLoginCount(0);
user.setDateLastLogin(new Timestamp(now)); user.setDateLastLogin(new Timestamp(now));
Env.setContext(Env.getCtx(), "#AD_Client_ID", user.getAD_Client_ID());
if (!user.save()) if (!user.save())
log.severe("Failed to update user record with date last login (" + user.getName() + " / clientID = " + user.getAD_Client_ID() + ")"); log.severe("Failed to update user record with date last login (" + user.getName() + " / clientID = " + user.getAD_Client_ID() + ")");
} }
@ -1490,6 +1493,7 @@ public class Login
user.setFailedLoginCount(count); user.setFailedLoginCount(count);
user.setIsLocked(reachMaxAttempt); user.setIsLocked(reachMaxAttempt);
user.setDateAccountLocked(user.isLocked() ? new Timestamp(now) : null); user.setDateAccountLocked(user.isLocked() ? new Timestamp(now) : null);
Env.setContext(Env.getCtx(), "#AD_Client_ID", user.getAD_Client_ID());
if (!user.save()) if (!user.save())
log.severe("Failed to update user record with increase failed login count"); log.severe("Failed to update user record with increase failed login count");
} }

View File

@ -159,6 +159,7 @@ public class CompiereService {
Properties savedCache = new Properties(); Properties savedCache = new Properties();
savedCache.putAll(Env.getCtx()); savedCache.putAll(Env.getCtx());
ctxMap.put(key.toString(), savedCache); ctxMap.put(key.toString(), savedCache);
if (log.isLoggable(Level.INFO)) log.info("Saving " + this + " in cache");
} }
} }
} }
@ -201,15 +202,16 @@ public class CompiereService {
String loginInfo = null; String loginInfo = null;
// Verify existence of User/Client/Org/Role and User's acces to Client & Org // Verify existence of User/Client/Org/Role and User's acces to Client & Org
String sql = "SELECT u.Name || '@' || c.Name || '.' || o.Name AS Text " String sql = "SELECT u.Name || '@' || c.Name || '.' || o.Name AS Text "
+ "FROM AD_User u, AD_Client c, AD_Org o, AD_User_Roles ur " + "FROM AD_User u, AD_Client c, AD_Org o, AD_User_Roles ur, AD_Role r "
+ "WHERE u.AD_User_ID=?" // #1 + "WHERE u.AD_User_ID=?" // #1
+ " AND c.AD_Client_ID=?" // #2 + " AND c.AD_Client_ID=?" // #2
+ " AND o.AD_Org_ID=?" // #3 + " AND o.AD_Org_ID=?" // #3
+ " AND ur.AD_Role_ID=?" // #4 + " AND ur.AD_Role_ID=?" // #4
+ " AND ur.AD_User_ID=u.AD_User_ID" + " AND ur.AD_User_ID=u.AD_User_ID"
+ " AND ur.AD_Role_ID=r.AD_Role_ID"
+ " AND (o.AD_Client_ID = 0 OR o.AD_Client_ID=c.AD_Client_ID)" + " AND (o.AD_Client_ID = 0 OR o.AD_Client_ID=c.AD_Client_ID)"
+ " AND c.AD_Client_ID IN (SELECT AD_Client_ID FROM AD_Role_OrgAccess ca WHERE ca.AD_Role_ID=ur.AD_Role_ID)" + " AND (r.IsAccessAllOrgs='Y' OR (c.AD_Client_ID IN (SELECT AD_Client_ID FROM AD_Role_OrgAccess ca WHERE ca.AD_Role_ID=ur.AD_Role_ID)"
+ " AND o.AD_Org_ID IN (SELECT AD_Org_ID FROM AD_Role_OrgAccess ca WHERE ca.AD_Role_ID=ur.AD_Role_ID)"; + " AND o.AD_Org_ID IN (SELECT AD_Org_ID FROM AD_Role_OrgAccess ca WHERE ca.AD_Role_ID=ur.AD_Role_ID)))";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null; ResultSet rs = null;
try try
@ -406,10 +408,13 @@ public class CompiereService {
l_cs = csMap.get(key); l_cs = csMap.get(key);
if (l_cs != null) { if (l_cs != null) {
if (l_cs.isExpired()) { if (l_cs.isExpired()) {
csMap.remove(key);
ctxMap.remove(key);
l_cs = null; l_cs = null;
} else { } else {
Properties cachedCtx = ctxMap.get(key); Properties cachedCtx = ctxMap.get(key);
Env.getCtx().putAll(cachedCtx); Env.getCtx().putAll(cachedCtx);
if (log.isLoggable(Level.INFO)) log.info("Reusing " + l_cs);
} }
} }
} }
@ -445,6 +450,7 @@ public class CompiereService {
); );
if (m_connected && expired) if (m_connected && expired)
{ {
if (log.isLoggable(Level.INFO)) log.info("Closing expired/invalid " + this);
Env.logout(); Env.logout();
ServerContext.dispose(); ServerContext.dispose();
m_loggedin = false; m_loggedin = false;

View File

@ -35,6 +35,7 @@ public class CompiereUtil {
try try
{ {
if (log.isLoggable(Level.INFO)) log.info("Starting webservices iDempiere session");
s_initOK = Adempiere.startup(false); s_initOK = Adempiere.startup(false);
} }
catch (Exception ex) catch (Exception ex)