]> dev.renevier.net Git - syj.git/blob - application/controllers/helpers/SyjUserManager.php
use cookies instead of session to manage login
[syj.git] / application / controllers / helpers / SyjUserManager.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class Syj_Controller_Action_Helper_SyjUserManager extends Zend_Controller_Action_Helper_Abstract
6 {
7     // -1 for undeterminated, null for non logged, Syj_Model_User for a logged user
8     protected static $_current = -1;
9
10     static public function validate($username, $hash) {
11         // TODO: try to make only one sql request
12         $adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
13         $authAdapter = new Zend_Auth_Adapter_DbTable($adapter, 'users', 'pseudo', 'password');
14         $authAdapter->setIdentity($username)->setCredential($hash);
15         $auth = Zend_Auth::getInstance();
16         $result = $auth->authenticate($authAdapter);
17         if (!$result->isValid()) {
18             self::$_current = null;
19             return false;
20         }
21         $userid = $authAdapter->getResultRowObject('id')->id;
22         $userMapper = new Syj_Model_UserMapper();
23         $user = new Syj_Model_User();
24         if (!$userMapper->find($userid, $user)) {
25             throw new Zend_Exception();
26         }
27
28         if (!isset ($_COOKIE['syj_user']) or (!isset ($_COOKIE['syj_hashpass']))) {
29             setcookie("syj_user", $username, 0, "", "", false, true);
30             setcookie("syj_hashpass", $hash, 0, "", "", false, true);
31         }
32         self::$_current = $user;
33         return true;
34     }
35
36     static public function logout() {
37         self::$_current = null;
38         if (isset ($_COOKIE['syj_user'])) {
39             setcookie ('syj_user', "", time() - 3600, "" , "",false, true);
40         }
41         if (isset ($_COOKIE['syj_hashpass'])) {
42             setcookie ('syj_hashpass', "", time() - 3600, "" , "",false, true);
43         }
44     }
45
46     static public function current() {
47         if (self::$_current === -1) {
48             if ((!isset ($_COOKIE['syj_user'])) || (!isset ($_COOKIE['syj_hashpass']))
49                  || (!self::validate($_COOKIE['syj_user'], $_COOKIE['syj_hashpass']))) {
50                     self::logout();
51             }
52         }
53         return self::$_current;
54     }
55
56     public function needsLogin() {
57         if (self::current()) {
58             return;
59         }
60
61         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
62         $view = $viewRenderer->view;
63         $request = $this->getRequest();
64
65         $encodeduri = $view->UriPath(true);
66         $loginurl = $view->addParamToUrl($view->baseUrl() . '/' . 'login', 'redirect', $encodeduri);
67         $translator = Zend_Registry::get('Zend_Translate');
68         $this->getActionController()->getHelper('Redirector')->gotoURL($loginurl, array('prependBase' => false));
69     }
70
71 }