| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Custom Authentication manager permits authentication using
|
|
|
4 |
* a string token saved in the cookie.
|
|
|
5 |
*
|
|
|
6 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
7 |
* @version $Id: TrackerAuthManager.php 1400 2006-09-09 03:13:44Z wei $
|
|
|
8 |
* @package Demos
|
|
|
9 |
* @since 3.1
|
|
|
10 |
*/
|
|
|
11 |
class TrackerAuthManager extends TAuthManager
|
|
|
12 |
{
|
|
|
13 |
/**
|
|
|
14 |
* @const string signon token cookie name.
|
|
|
15 |
*/
|
|
|
16 |
const SignonCookieName = 'time-tracker-signon';
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Performs the real authentication work. Overrides and calls parent
|
|
|
20 |
* implementation. Trys to authenticate using token saved in cookie.
|
|
|
21 |
* @param mixed parameter to be passed to OnAuthenticate event
|
|
|
22 |
*/
|
|
|
23 |
public function onAuthenticate($param)
|
|
|
24 |
{
|
|
|
25 |
parent::onAuthenticate($param);
|
|
|
26 |
$currentUser = $this->Application->User;
|
|
|
27 |
if(!$currentUser || $currentUser->IsGuest)
|
|
|
28 |
$this->authenticateFromCookie($param);
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* If the user is not set or is still a guest, try to authenticate the user
|
|
|
33 |
* using a string token saved in the cookie if any.
|
|
|
34 |
* @param mixed parameter to be passed to OnAuthenticate event
|
|
|
35 |
*/
|
|
|
36 |
protected function authenticateFromCookie($param)
|
|
|
37 |
{
|
|
|
38 |
$cookie = $this->Request->Cookies[self::SignonCookieName];
|
|
|
39 |
if(!is_null($cookie))
|
|
|
40 |
{
|
|
|
41 |
$daos = $this->getApplication()->getModule('daos');
|
|
|
42 |
$userDao = $daos->getDao('UserDao');
|
|
|
43 |
$user = $userDao->validateSignon($cookie->Value);
|
|
|
44 |
if($user instanceof TimeTrackerUser)
|
|
|
45 |
$this->updateCredential($user);
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Changes the user credentials.
|
|
|
51 |
* @param TUser new user details.
|
|
|
52 |
*/
|
|
|
53 |
public function updateCredential($user)
|
|
|
54 |
{
|
|
|
55 |
$user->IsGuest = false;
|
|
|
56 |
$this->updateSessionUser($user);
|
|
|
57 |
$this->Application->User = $user;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Generate a token to be saved in the cookie for later authentication.
|
|
|
62 |
* @param TimeTrackerUser user details.
|
|
|
63 |
*/
|
|
|
64 |
public function rememberSignon($user)
|
|
|
65 |
{
|
|
|
66 |
$daos = $this->getApplication()->getModule('daos');
|
|
|
67 |
$userDao = $daos->getDao('UserDao');
|
|
|
68 |
$token = $userDao->createSignonToken($user);
|
|
|
69 |
$cookie = new THttpCookie(self::SignonCookieName, $token);
|
|
|
70 |
$cookie->Expire = strtotime('+1 month');
|
|
|
71 |
$this->Response->Cookies[] = $cookie;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Logs out the user and delete the token from cookie.
|
|
|
76 |
*/
|
|
|
77 |
public function logout()
|
|
|
78 |
{
|
|
|
79 |
parent::logout();
|
|
|
80 |
$cookie = new THttpCookie(self::SignonCookieName,'');
|
|
|
81 |
$this->Response->Cookies[] = $cookie;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
?>
|