Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extend session of Liferay when performing AJAX call

I created a portlet that uses AJAX every function. That is, the portlet is only rendered once and i didnt utilize processAction or the like.

Is there a way to extend the user's session using built in Liferay function using javascript?

I tried

Liferay.Session.extend();

but it does not seem to work..

I also tried a solution in the ICEfaces forum, which is

    if (Liferay.Session._stateCheck) {
           window.clearTimeout(Liferay.Session._stateCheck);
           Liferay.Session._stateCheck = null;
         }
         Liferay.Session.init({
           autoExtend: false,
           timeout: Liferay.Session._timeout,
           timeoutWarning:  Liferay.Session._warning
           });jQuery.ajax({url: Liferay.Session._sessionUrls.extend});

also not working..

I put those blocks of code whenever the user clicks a button

Any tip would greatly help..

like image 950
ton Avatar asked Mar 01 '12 02:03

ton


1 Answers

I had same issue and solved it. The main idea is to override session.js into ext-plugin, and add some additional method:

extendExternal: function() {
  var instance = this;

  if (instance != undefined) {
    instance.extend();
  }
}

also setCookie method should be updated:

 setCookie: function(status) {
   var instance = this;

   var currentTime = new Date().getTime();

   var options = {
      secure: A.UA.secure,
      path: "/"
   };

   A.Cookie.set(instance._cookieKey, status || currentTime, options);
 }

In order to use the same cookie path for each page.

And some global ajax event can be used for 'automatical' call of extendExternal method:

AUI().use('event', function(A){
 A.on('io:start', function(transactionid, arguments){
   if (Liferay.Session._cookieKey != undefined && Liferay.Session._cookieKey != null) {
     if (arguments != 'sessionExtend') {
       Liferay.Session.extendExternal();
     }
   }
 });
});

in this case extend method have to be updated with:

// added in order to differentiate session extend ajax calls and other ajax call, to     avoid infinit loop
 A.io.request(instance._sessionUrls.extend, {
    arguments: 'sessionExtend'
 });

You can check solution here.

like image 82
Pavlo Butenko Avatar answered Oct 20 '22 10:10

Pavlo Butenko