]> dev.renevier.net Git - syj.git/commitdiff
syj session controller helper
authorarno <arno@renevier.net>
Mon, 2 Aug 2010 09:44:15 +0000 (11:44 +0200)
committerarno <arno@renevier.net>
Fri, 13 Aug 2010 11:55:04 +0000 (13:55 +0200)
14 files changed:
application/configs/application.ini
application/controllers/AccountController.php
application/controllers/ContactController.php
application/controllers/IdxController.php
application/controllers/LoginController.php
application/controllers/NewpwdController.php
application/controllers/PathController.php
application/controllers/PendingController.php
application/controllers/UserController.php
application/controllers/helpers/SyjSession.php [new file with mode: 0644]
application/controllers/plugins/SyjLoggedUser.php [new file with mode: 0644]
application/layouts/scripts/footer.phtml
application/views/helpers/LoggedUser.php [deleted file]
application/views/scripts/idx/index.phtml

index 290fa8c94efe59e2e67604a4716bceef6344bbb9..393684cb918e224e48ceabe91975d065f0ad0763 100644 (file)
@@ -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
index 30daa5bc3eead3ad461c261e215358705c130a8c..76124908d504b90645d13658f53b87f5d9f0a7e1 100644 (file)
@@ -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) {
index 9a7ade195d7a3e883f5d415471c83ec2fa24fa2f..e8e2843d35d9d064760407428ee47da4e6eae844 100644 (file)
@@ -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');
index e606ebc260efd54f53422dd57c73c4fb5e08f1e5..ac315581eafbc29eb300c65cbba1a7c9c908b835 100644 (file)
@@ -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;
index 48636214855fbcd672d941ab275c2201ed325565..dd07b1e07d833c9c700e6a55456638bf69c38f9a 100644 (file)
@@ -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();
     }
 
index 93d7f9fc5ffddc4a3f3f850d6d569dfc94940780..012738c07045d765a865779abd367844d232bd8a 100644 (file)
@@ -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();
             }
index bd4d33908bdfeb409b0a9eff47e654ae10908d2d..37394d02dbfd4bc3331a3771543f09d772365187 100644 (file)
@@ -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();
         }
 
index 025f6c97a4048af750eb1c5888311c9888209fff..d025023dd8197987025597099b1bb24e166cd847 100644 (file)
@@ -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()) {
index 2bb3fe96032f5bb32bed0270c94d6acf5e95e4e3..ed078ad9688f2442965301f61e06964249735daa 100644 (file)
@@ -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 (file)
index 0000000..702bfa2
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+/*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
+    and is published under the AGPL license. */
+
+class Syj_Controller_Action_Helper_SyjSession extends Zend_Controller_Action_Helper_Abstract
+{
+    protected static $cache = array();
+
+    static public function login($userid) {
+        $userMapper = new Syj_Model_UserMapper();
+        $user = new Syj_Model_User();
+        if (!$userMapper->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 (file)
index 0000000..af5c796
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+/*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
+    and is published under the AGPL license. */
+
+class Syj_Controllers_Plugins_SyjLoggedUser extends Zend_Controller_Plugin_Abstract
+{
+    public function postDispatch(Zend_Controller_Request_Abstract $request) {
+        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
+        $view = $viewRenderer->view;
+        $sessionHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('SyjSession');
+        $view->loggedUser = $sessionHelper->user();
+    }
+}
index 1d73c676c6fd07f5c83e28bd4bc2b50406429d9c..a90874e7219e35e7e2ff8e36e2050d77722966a1 100644 (file)
@@ -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 (file)
index 59af93d..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-/*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
-    and is published under the AGPL license. */
-
-class Syj_View_Helper_LoggedUser extends Zend_View_Helper_Abstract
-{
-    public function LoggedUser() {
-        try {
-            $sessionStorage = Zend_Auth::getInstance()->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;
-        }
-    }
-}
-
-?>
index 38f1e3b7fad448bfe692892767db0082ac6fe932..ce8b6f72a24672673f513b4a4821f98df834b30c 100644 (file)
@@ -11,7 +11,7 @@
     <div id="map"></div>
 
     <div id="message" class="message info">
-    <?php if (!$this->path and !$this->loggedUser()) {?>
+    <?php if (!$this->path and !$this->loggedUser) {?>
     <?php echo $this->translate('Welcome on Syj.')?>
     <br/>
     <?php echo $this->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.')?>