Script eseguito alla User Login

13 Feb '15, 04:49 PM
8,136 Views
No Forum Badges

Buongiorno, sto usando la versione Community 7.2.7 per un progetto di test.

Tra le funzionalità da provare c'è quella che nel momento in cui un utente esegue il login dovrei memorizzarne una serie di informazioni, tra cui la data del login e l'indirizzo IP da cui si collega.

Ho pensato di mettere in un Job con Trigger "On user Login" una modify Unit che inserisce il timestamp (ottenuto da una time unit) in un campo della entità USER del record che ottengo dal context parameter USERCTXPARAM e questo funziona correttamente.

Per inserire anche l'IP remoto ho provato ad inserire nella catena uno script Groovy con  le seguenti righe di codice:

#output String ipAddress
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import com.webratio.rtx.RTXConstants
import com.webratio.rtx.core.BeanHelper
import java.net.URLDecoder;
import java.text.SimpleDateFormat

def urlInvoked = null
def remoteAddr = null

println("SONO PARTITO*******");
//codice per gestione parametri XMLHttpRequest
try{ 
HttpServletRequest request = localContext.get(RTXConstants.HTTP_SERVLET_REQUEST_KEY);
HttpSession session = request.getSession();


    urlInvoked = request.getRequestURL();
    remoteAddr = request.getRemoteAddr();
    println("remoteADDR:"+remoteAddr+" URL:"+ urlInvoked);
}
catch(e){
}    
return ["ipAddress":remoteAddr]

Però non riesco a raccogliere il valore perchè mi segnala che il valore request è NULL e quindi non è possibile eseguire il metodo getSession().

Se eseguo lo script in una catena di operazioni innescata da pagina web invece lo script funziona bene. Forse nell'esecuzione tramite JOB manca qualche riferimento di contesto ?

In questo caso avete dei suggerimenti alternativi ?

Grazie

 

 

 
x 0
Follow
Answer Answer at this question and get points!
Forum Starter - Level 2

Hi,

 

First of all, using the request.getRemoteAddr() from script component will always retrieve the server's IP (because the script will run on server). The only way you can obtain the client's address is using a JSP scriptlet inside the login page template. For example:

<%

out.println("Client's IP address is " + request.getRemoteAddr());
%>

 

If you want to save the IP in the database (in USER table),  I think that a good approach is to create a context parameter (eg. IP_CTX_PARAM) in your model and set it into JSP tags as explained in 

https://www.webratio.com/forum/question-details/set-context-parameter?link=oln15x.redirect&nav=45

 

So, for your particular issue, lets assume that your IP_CTX_PARAM has the ctx1 id. The scriptlet that you should put inside your login page template will be the following:

 

<% {

String userIPAddress = request.getRemoteAddr()

java.util.Map sessionCtx = (java.util.Map)session.getAttribute(com.webratio.struts.WRGlobals.SESSION_CONTEXT_KEY);
 
 sessionCtx.put("ctx1", userIPAddress ); //ctx1 is the id of the IP_CTX_PARAM

 } %>

 

If the client is connecting through a proxy, this method that I presented will return the IP of the proxy. In this case you can parse the  request.getHeader("x-forwarded-for") in order to get the real IP of the client.

 

Hope that this will solve your problem

 
x 0
Forum Starter - Level 2

EDIT: I forgot a semicolon (;) in the previous script. It should be 

String userIPAddress = request.getRemoteAddr() ;

 
x 0
Forum Starter - Level 3

After a successful login, the control is transferred to the Unit with "Home" properties set of the user default module (see the User - Group - Module model in the Domain Model view).

Therefore, you may want to place your groovy code in a Script Unit, and mark it as "Home". In this way, it will be executed just after the successful login. It can be followed by a Time Unit which returns the login timestamp. Link it with another Ok Flow to a Create Unit which will store your auditing data (username, timestamp, ip address and whatever you need). Finally, add another Ok Flow to the page which you want show to the user as the webapp homepage.

I don't think it's a good idea to use a job with trigger, even it would work it seems not to be a strightforward and plain design.

 

 
x 0
Answer at this question and get points!