Completed
Push — master ( ec2c00...c3f83b )
by Markus
05:40
created

SampleAppUser::startup()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 0
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
    <?php
2
3
// +---------------------------------------------------------------------------+
4
// | This file is part of the Agavi package.                                   |
5
// | Copyright (c) 2005-2011 the Agavi Project.                                |
6
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr.    |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
17
use Agavi\User\RbacSecurityUser;
18
use Agavi\Exception\SecurityException;
19
20
class SampleAppUser extends RbacSecurityUser
21
{
22
	/**
23
	 * Let's pretend this is our database. For the sake of example ;)
24
	 */
25
	static $users = array(
26
		'Chuck Norris' => array(
27
			'password' => '$2a$10$2/Gmc4XpwAytFgy3wfrW9OUnkzd6ahgcMqrm4cEc4zD3IFD1GB6IG', // bcrypt, 10 rounds, "kick"
28
			'roles' => array(
29
				'photographer',
30
			)
31
		),
32
	);
33
	
34
	public function startup()
35
	{
36
		parent::startup();
37
		
38
		$reqData = $this->getContext()->getRequest()->getRequestData();
39
		
40
		if(!$this->isAuthenticated() && $reqData->hasCookie('autologon')) {
41
			$login = $reqData->getCookie('autologon');
42
			try {
43
				$this->login($login['username'], $login['password'], true);
44
			} catch(SecurityException $e) {
45
				$response = $this->getContext()->getController()->getGlobalResponse();
46
				// login didn't work. that cookie sucks, delete it.
47
				$response->setCookie('autologon[username]', false);
48
				$response->setCookie('autologon[password]', false);
49
			}
50
		}
51
	}
52
	
53
	public function login($username, $password, $isPasswordHashed = false)
54
	{
55
		if(!isset(self::$users[$username])) {
56
			throw new SecurityException('username');
57
		}
58
		
59
		if(!$isPasswordHashed) {
60
			$password = self::computeSaltedHash($password, self::$users[$username]['password']);
61
		}
62
		
63
		if($password != self::$users[$username]['password']) {
64
			throw new SecurityException('password');
65
		}
66
		
67
		$this->setAuthenticated(true);
68
		$this->clearCredentials();
69
		$this->grantRoles(self::$users[$username]['roles']);
70
	}
71
	
72
	public static function computeSaltedHash($secret, $salt)
73
	{
74
		return crypt($secret, $salt);
75
	}
76
	
77
	public static function getPassword($username)
78
	{
79
		if(self::$users[$username]) {
80
			return self::$users[$username]['password'];
81
		}
82
	}
83
	
84
	public function logout()
85
	{
86
		$this->clearCredentials();
87
		$this->setAuthenticated(false);
88
	}
89
}
90
91
?>