From 37a7da7b5f29ad68fe387f578f0cee0cf5bc4a9d Mon Sep 17 00:00:00 2001 From: PavelBegunkov <asml.Silence@gmail.com> Date: Sat, 22 Nov 2014 01:01:40 +0300 Subject: [PATCH] session: alert disappearance --- .../classes/Controller/Handler/Session.php | 9 +++++++- ~dev_rating/media/js/profile.js | 21 ++++++++++++++++--- .../modules/account/classes/Kohana/User.php | 9 ++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/~dev_rating/application/classes/Controller/Handler/Session.php b/~dev_rating/application/classes/Controller/Handler/Session.php index 8f505469a..1b4f7dce9 100644 --- a/~dev_rating/application/classes/Controller/Handler/Session.php +++ b/~dev_rating/application/classes/Controller/Handler/Session.php @@ -11,7 +11,7 @@ class Controller_Handler_Session extends Controller_Handler { $session = Session::instance(); if ($session->get('LoggedIn')) { $dif_time = $session->get('dif_time'); - $timeout = $session->get('timeout'); + $timeout = User::SESSION_LIFETIME; $remain = $timeout - $dif_time; if ($remain <= 0) { $this->completeSignOut(); @@ -23,4 +23,11 @@ class Controller_Handler_Session extends Controller_Handler { } $this->response->body(json_encode($remain)); } + + public function action_closeSession() { + if ($session->get('LoggedIn')) { + $this->completeSignOut(); + } + $this->response->body(json_encode(0)); + } } \ No newline at end of file diff --git a/~dev_rating/media/js/profile.js b/~dev_rating/media/js/profile.js index 9caa3b304..01a94160c 100644 --- a/~dev_rating/media/js/profile.js +++ b/~dev_rating/media/js/profile.js @@ -16,6 +16,21 @@ $(function() }); + function closeSession(time) { + if (time < 0) + time = 0; + + setTimeout(function() { + $.ajax({ + type: "POST", + url: URLdir + "handler/Session/closeSession", + success: function(data) {} + }); + window.location.replace(URLdir); + }, time*1000); + } + + function setSessionTimer(time) { timer = setTimeout(function() { $.ajax({ @@ -25,11 +40,11 @@ $(function() { data = $.parseJSON(data); wait = parseInt(data, 10); - if (wait <= 20) { - alert("it's over!"); + if (wait <= 10) { + closeSession(wait); return; } else { - setSessionTimer(wait-20); + setSessionTimer(wait-10); } } }); diff --git a/~dev_rating/modules/account/classes/Kohana/User.php b/~dev_rating/modules/account/classes/Kohana/User.php index 49a038682..848918946 100644 --- a/~dev_rating/modules/account/classes/Kohana/User.php +++ b/~dev_rating/modules/account/classes/Kohana/User.php @@ -8,6 +8,8 @@ class Kohana_User implements ArrayAccess { protected $_model; protected $_userInfo; + const SESSION_LIFETIME = 600; //seconds + /** * Вовзращает экземпляр класса (singleton-паттерн) * @@ -40,20 +42,17 @@ class Kohana_User implements ArrayAccess { if ($isSignedIn) { $last_time = $this->_session->get('last_time'); $cur_time = time(); - $timeout = 50; // second + $timeout = self::SESSION_LIFETIME; if (isset($last_time) AND $last_time != null) { $dif_time = $cur_time - $last_time; if ($dif_time > $timeout) { $this->completeSignOut(); - //throw HTTP_Exception::factory (403); - //return; } $this->_session->set('dif_time', $dif_time); } else { $this->_session->set('dif_time', $timeout+10); } - $this->_session->set('last_time', $cur_time); - $this->_session->set('timeout', $timeout); + $this->_session->set('last_time', $cur_time); } } -- GitLab