From: arno Date: Mon, 2 Aug 2010 09:44:15 +0000 (+0200) Subject: syj session controller helper X-Git-Tag: v0.2~82 X-Git-Url: https://dev.renevier.net/?p=syj.git;a=commitdiff_plain;h=05ce9238adfa982746bfc6211b240fd24526af09 syj session controller helper --- diff --git a/application/configs/application.ini b/application/configs/application.ini index 290fa8c..393684c 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -21,6 +21,7 @@ resources.frontController.defaultloc.minlat = -70.0 resources.frontController.defaultloc.maxlat = 70.0 resources.frontController.actionhelperpaths.Syj_Controller_Action_Helper = APPLICATION_PATH "/controllers/helpers" resources.frontController.plugins.locale = "Syj_Controllers_Plugins_SyjLocale" +resources.frontController.plugins.loggeduser = "Syj_Controllers_Plugins_SyjLoggedUser" resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" resources.db.adapter = "Pdo_Pgsql" ; i18n diff --git a/application/controllers/AccountController.php b/application/controllers/AccountController.php index 30daa5b..7612490 100644 --- a/application/controllers/AccountController.php +++ b/application/controllers/AccountController.php @@ -15,7 +15,7 @@ class AccountController extends Zend_Controller_Action } public function indexAction() { - $user = $this->view->loggedUser(); + $user = $this->_helper->SyjSession->user(); $request = $this->getRequest(); if ($user === null) { diff --git a/application/controllers/ContactController.php b/application/controllers/ContactController.php index 9a7ade1..e8e2843 100644 --- a/application/controllers/ContactController.php +++ b/application/controllers/ContactController.php @@ -84,7 +84,7 @@ class ContactController extends Zend_Controller_Action } if (empty($formData)) { - $user = $this->view->loggedUser(); + $user = $this->_helper->SyjSession->user(); if ($user) { $form->contact_email->setValue($user->email) ->setAttrib('readonly', 'true'); diff --git a/application/controllers/IdxController.php b/application/controllers/IdxController.php index e606ebc..ac31558 100644 --- a/application/controllers/IdxController.php +++ b/application/controllers/IdxController.php @@ -54,31 +54,21 @@ class IdxController extends Zend_Controller_Action $this->view->loginform = $loginform; $this->view->userform = $userform; $this->view->newpwdform = $newpwdform; + $this->view->loggedUser = $this->_helper->SyjSession->user(); } protected function _jsLoggedInfo(Syj_Model_Path $path = null) { $loggedinfo = new phptojs\JsObject('gLoggedInfo', array('connections' => 0)); - $sessionStorage = Zend_Auth::getInstance()->getStorage(); - $sessionData = $sessionStorage->read(); - - if ($sessionStorage->isEmpty()) { - $loggedinfo->logged = false; + $user = $this->_helper->SyjSession->user(); + if ($user) { + $loggedinfo->logged = true; } else { - $userMapper = new Syj_Model_UserMapper(); - $obj = new Syj_Model_User(); - if ($userMapper->find($sessionData['user'], $obj)) { - $loggedinfo->logged = true; - } else { - // non existent user - Zend_Session::start(); - Zend_Session::destroy(); - $loggedinfo->logged = false; - } + $loggedinfo->logged = false; } if (isset($path)) { - if ($path->owner->id == $sessionData['user']) { + if ($user and $path->owner->id == $user->id) { $loggedinfo->isowner = true; } else { $loggedinfo->isowner = false; diff --git a/application/controllers/LoginController.php b/application/controllers/LoginController.php index 4863621..dd07b1e 100644 --- a/application/controllers/LoginController.php +++ b/application/controllers/LoginController.php @@ -51,9 +51,7 @@ class LoginController extends Zend_Controller_Action } $userid = $authAdapter->getResultRowObject('id')->id; - $auth->getStorage()->write(array('user' => $userid)); - Zend_Session::rememberMe(); // zend default expiration delay is 2 weeks. Ok, use that value - + $this->_helper->SyjSession->login($userid); if ($httprequest) { $api = $this->_helper->SyjApi->setCode(200); @@ -79,8 +77,7 @@ class LoginController extends Zend_Controller_Action } public function logoutAction() { - Zend_Session::start(); - Zend_Session::destroy(); + $this->_helper->SyjSession->logout(); $this->redirect(); } diff --git a/application/controllers/NewpwdController.php b/application/controllers/NewpwdController.php index 93d7f9f..012738c 100644 --- a/application/controllers/NewpwdController.php +++ b/application/controllers/NewpwdController.php @@ -25,7 +25,7 @@ class NewpwdController extends Zend_Controller_Action } if (empty($formData)) { - $loggeduser = $this->view->loggedUser(); + $loggeduser = $this->_helper->SyjSession->user(); if ($loggeduser) { $form->newpwd_email->setValue($loggeduser->email) ->setAttrib('readonly', 'true'); @@ -45,7 +45,7 @@ class NewpwdController extends Zend_Controller_Action $user = new Syj_Model_User(); if ($userMapper->findByEmail($formData['newpwd_email'], $user)) { // if no user exist with posted email, pretend everything went correct - $loggeduser = isset($loggeduser) ? $loggeduser: $this->view->loggedUser(); + $loggeduser = isset($loggeduser) ? $loggeduser: $this->_helper->SyjSession->user(); if ($loggeduser and ($loggeduser != $user)) { throw new Syj_Exception_Request(); } diff --git a/application/controllers/PathController.php b/application/controllers/PathController.php index bd4d339..37394d0 100644 --- a/application/controllers/PathController.php +++ b/application/controllers/PathController.php @@ -7,17 +7,8 @@ class PathController extends Zend_Controller_Action public function indexAction() { $formData = $this->_helper->SyjPostData->getPostData('Syj_Form_Geom'); - $sessionStorage = Zend_Auth::getInstance()->getStorage(); - if ($sessionStorage->isEmpty()) { - throw new Syj_Exception_Forbidden(); - } - $sessionData = $sessionStorage->read(); - - $user = new Syj_Model_User(); - $userMapper = new Syj_Model_UserMapper(); - if (!$userMapper->find($sessionData['user'], $user)) { - // we could also throw a forbidden exception, but client session - // should not contain reference to a non existent user. So, it's considered a bug. + $user = $this->_helper->SyjSession->user(); + if (!$user) { throw new Syj_Exception_Forbidden(); } diff --git a/application/controllers/PendingController.php b/application/controllers/PendingController.php index 025f6c9..d025023 100644 --- a/application/controllers/PendingController.php +++ b/application/controllers/PendingController.php @@ -52,10 +52,7 @@ class PendingController extends Zend_Controller_Action $title = $this->view->translate("password changed"); $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/password_validate.:suffix'); - - // logout - Zend_Session::start(); - Zend_Session::destroy(); + $this->_helper->SyjSession->logout(); } else if (array_key_exists('pending_cancel', $formData)) { if (!$pending->cancel()) { diff --git a/application/controllers/UserController.php b/application/controllers/UserController.php index 2bb3fe9..ed078ad 100644 --- a/application/controllers/UserController.php +++ b/application/controllers/UserController.php @@ -47,9 +47,7 @@ class UserController extends Zend_Controller_Action } } - $auth = Zend_Auth::getInstance(); - $auth->getStorage()->write(array('user' => $user->id)); - Zend_Session::rememberMe(); // zend default expiration delay is 2 weeks. Ok, use that value + $this->_helper->SyjSession->login($user->id); $this->_helper->SyjApi->setCode(200); } } diff --git a/application/controllers/helpers/SyjSession.php b/application/controllers/helpers/SyjSession.php new file mode 100644 index 0000000..702bfa2 --- /dev/null +++ b/application/controllers/helpers/SyjSession.php @@ -0,0 +1,56 @@ +find($userid, $user)) { + throw new Zend_Exception(); + } + + $storage = Zend_Auth::getInstance()->getStorage(); + $storage->clear(); + $storage->write(array('user' => $user->id)); + Zend_Session::rememberMe(); // zend default expiration delay is 2 weeks. Ok, use that value + } + + static public function logout() { + Zend_Session::start(); + Zend_Session::destroy(); + } + + static public function user() { + try { + $sessionStorage = Zend_Auth::getInstance()->getStorage(); + } catch(Exception $e) { + return null; + } + $sessionData = $sessionStorage->read(); + if ($sessionStorage->isEmpty()) { + return null; + } + + $id = $sessionData['user']; + if (!isset($id)) { + return null; + } + if (isset (self::$cache[$id])) { + return self::$cache[$id]; + } + + $userMapper = new Syj_Model_UserMapper(); + $user = new Syj_Model_User(); + if ($userMapper->find($id, $user)) { + self::$cache[$id] = $user; + return $user; + } else { + self::logout(); + return null; + } + } +} diff --git a/application/controllers/plugins/SyjLoggedUser.php b/application/controllers/plugins/SyjLoggedUser.php new file mode 100644 index 0000000..af5c796 --- /dev/null +++ b/application/controllers/plugins/SyjLoggedUser.php @@ -0,0 +1,13 @@ +view; + $sessionHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('SyjSession'); + $view->loggedUser = $sessionHelper->user(); + } +} diff --git a/application/layouts/scripts/footer.phtml b/application/layouts/scripts/footer.phtml index 1d73c67..a90874e 100644 --- a/application/layouts/scripts/footer.phtml +++ b/application/layouts/scripts/footer.phtml @@ -31,7 +31,7 @@ echo $this->footerLink(array( /* * login, logout, account links */ -if ($this->loggedUser()) { +if ($this->loggedUser) { echo $this->footerLink(array( 'route' => 'account', 'action' => 'index', diff --git a/application/views/helpers/LoggedUser.php b/application/views/helpers/LoggedUser.php deleted file mode 100644 index 59af93d..0000000 --- a/application/views/helpers/LoggedUser.php +++ /dev/null @@ -1,28 +0,0 @@ -getStorage(); - } catch(Exception $e) { - return null; - } - $sessionData = $sessionStorage->read(); - if ($sessionStorage->isEmpty()) { - return null; - } - - $userMapper = new Syj_Model_UserMapper(); - $user = new Syj_Model_User(); - if ($userMapper->find($sessionData['user'], $user)) { - return $user; - } else { - return null; - } - } -} - -?> diff --git a/application/views/scripts/idx/index.phtml b/application/views/scripts/idx/index.phtml index 38f1e3b..ce8b6f7 100644 --- a/application/views/scripts/idx/index.phtml +++ b/application/views/scripts/idx/index.phtml @@ -11,7 +11,7 @@
- path and !$this->loggedUser()) {?> + path and !$this->loggedUser) {?> translate('Welcome on Syj.')?>
translate('To create a journey, just click on the map to add points to your route. You can zoom and move map with controls upper left.')?>