2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class Syj_Controller_Action_Helper_SyjSession extends Zend_Controller_Action_Helper_Abstract
7 protected static $cache = array();
9 static public function login($userid) {
10 $userMapper = new Syj_Model_UserMapper();
11 $user = new Syj_Model_User();
12 if (!$userMapper->find($userid, $user)) {
13 throw new Zend_Exception();
16 $storage = new Zend_Session_Namespace('userSettings');
17 $storage->user = $user->id;
18 Zend_Session::rememberMe();
21 static public function logout() {
22 $storage = new Zend_Session_Namespace('userSettings');
23 unset($storage->user);
24 Zend_Session::rememberMe();
27 static public function user() {
28 $storage = new Zend_Session_Namespace('userSettings');
33 if (isset (self::$cache[$id])) {
34 return self::$cache[$id];
37 $userMapper = new Syj_Model_UserMapper();
38 $user = new Syj_Model_User();
39 if ($userMapper->find($id, $user)) {
40 self::$cache[$id] = $user;
48 public function needsLogin() {
53 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
54 $view = $viewRenderer->view;
55 $request = $this->getRequest();
57 $encodeduri = $view->UriPath(true);
58 $loginurl = $view->addParamToUrl($view->baseUrl() . '/' . 'login', 'redirect', $encodeduri);
59 $translator = Zend_Registry::get('Zend_Translate');
60 $this->getActionController()->getHelper('Redirector')->gotoURL($loginurl, array('prependBase' => false));