Authlogic method when logout_on_timout or send action from session

237 views Asked by At

I save the login time when the session is destroyed

UserSessionsController (controller)

  def destroy
      connectime = (Time.now - current_user.current_login_at).to_i
      current_user.logtime(connectime)
      current_user_session.destroy
      redirect_to new_user_session_url
  end

User (model)

  def logtime(connectime)
      self.log_duration ||= 0
      self.log_duration += connectime
      self.save
  end

It's OK when the user logout, but doesn't work for logout_on_timeout

Where can I call a method when the user is logged out after timeout out to save this time of connexion ?

[Edit] An other way I'm looking at, is to send some method when the persistence_token is changed with after_persisting, but cannot access to current_user in the model.

The problem is to find the right place to send instruction from a controller (session or user) when the user make some action on the server.

1

There are 1 answers

0
Rufilix On

I find a solution trough application_controller. I'm afraid that multiple call slow down the server, but don't know how to make it better…

I add a column log_total_duration When the user interact to the server it call this before_filter to store the time the user is logged (log_duration)

  def set_time
    if current_user
      @user = User.where(:id => current_user).first
      @user.log_duration = (@user.last_request_at - @user.current_login_at).to_i
      @user.save
    end
  end

When The user log in I use in the create method of the UserSessionController. It add the log_duration to the log_duration_total

def create
      @user_session = UserSession.new(params[:user_session])      
      if @user_session.save        
        current_user = UserSession.find.user
        current_user.logtime

in the User Model

  def logtime
      self.log_duration_total ||= 0
      self.log_duration_total += log_duration
      self.save
  end