From ce60f5b71d1c1e18a65f8064575786afe0854fa9 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 8 Dec 2013 17:01:54 +0100 Subject: [PATCH] Moved to helper methods. --- app/controllers/sessions_controller.rb | 13 ++++--------- lib/session_helper.rb | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 37dc4bb0e..ad30ef2da 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -68,7 +68,7 @@ class SessionsController < ApplicationController # check logon session if params['logon_session'] - session = ActiveRecord::SessionStore::Session.where( :session_id => params['logon_session'] ).first + session = SessionHelper::get( params['logon_session'] ) if session user_id = session.data[:user_id] end @@ -193,10 +193,9 @@ class SessionsController < ApplicationController def list return if deny_if_not_role('Admin') - sessions = ActiveRecord::SessionStore::Session.order('updated_at DESC').limit(10000) assets = {} sessions_clean = [] - sessions.each {|session| + SessionHelper.list.each {|session| next if !session.data['user_id'] sessions_clean.push session if session.data['user_id'] @@ -211,17 +210,13 @@ class SessionsController < ApplicationController end def delete_old - ActiveRecord::SessionStore::Session.where('request_type = ? AND updated_at < ?', 1, Time.now - 90.days ).delete_all - ActiveRecord::SessionStore::Session.where('request_type = ? AND updated_at < ?', 2, Time.now - 2.days ).delete_all + SessionHelper::cleanup_expired render :json => {} end def delete return if deny_if_not_role('Admin') - session = ActiveRecord::SessionStore::Session.where( :id => params[:id] ).first - if session - session.destroy - end + SessionHelper::destroy( params[:id] ) render :json => {} end end diff --git a/lib/session_helper.rb b/lib/session_helper.rb index 98b9b90ef..ba30e230a 100644 --- a/lib/session_helper.rb +++ b/lib/session_helper.rb @@ -29,4 +29,27 @@ module SessionHelper return push_collections end + + def self.cleanup_expired + + # web sessions + ActiveRecord::SessionStore::Session.where('request_type = ? AND updated_at < ?', 1, Time.now - 90.days ).delete_all + + # http basic auth calls + ActiveRecord::SessionStore::Session.where('request_type = ? AND updated_at < ?', 2, Time.now - 2.days ).delete_all + end + + def self.get(id) + ActiveRecord::SessionStore::Session.where( :id => id ).first + end + + def self.list(limit = 10000) + ActiveRecord::SessionStore::Session.order('updated_at DESC').limit(limit) + end + + def self.destroy(id) + session = ActiveRecord::SessionStore::Session.where( :id => id ).first + return if !session + session.destroy + end end \ No newline at end of file