Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Auth.php « modules - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 907f98a6093697ded3d03396e1ea8d7c03f44e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
 * @version $Id$
 *
 * @package Piwik
 */

/**
 * Authentication object.
 * Should be reviewed and refactor to allow simple plugin overwrite
 * See OpenId authentication plugin, using Zend_Auth_OpenId on http://dev.piwik.org/trac/ticket/160
 * See Review the Login plugin to make it really modular  http://dev.piwik.org/trac/ticket/144
 * 
 * @package Piwik
 */
class Piwik_Auth extends Zend_Auth_Adapter_DbTable
{
	const SUCCESS_SUPERUSER_AUTH_CODE = 42;

	public function __construct()
	{
		$db = Zend_Registry::get('db');
		parent::__construct($db);
	}

	public function authenticate()
	{
		// we first try if the user is the super user
		$rootLogin = Zend_Registry::get('config')->superuser->login;
		$rootPassword = Zend_Registry::get('config')->superuser->password;
		$rootToken = Piwik_UsersManager_API::getTokenAuth($rootLogin,$rootPassword);

		//		echo $rootToken;
		//		echo "<br>". $this->_credential;exit;
		if($this->_identity == $rootLogin
		&& $this->_credential == $rootToken)
		{
			return new Piwik_Auth_Result(Piwik_Auth::SUCCESS_SUPERUSER_AUTH_CODE,
			$this->_identity,
			array() // message empty
			);
		}

		// we then look if the user is API authenticated
		// API authentication works without login name, but only with the token
		// TODO the logic (sql select) should be in the Login plugin, not here
		// this class should stay simple. Another Login plugin should only have to create an auth entry
		// of this class in the zend_registry and it should work
		if(is_null($this->_identity))
		{
			$authenticated = false;
				
			if($this->_credential === $rootToken)
			{
				return new Piwik_Auth_Result(Piwik_Auth::SUCCESS_SUPERUSER_AUTH_CODE,
											$rootLogin,
											array() // message empty
				);
			}
				
			$login = Zend_Registry::get('db')->fetchOne(
						'SELECT login FROM '.Piwik::prefixTable('user').' WHERE token_auth = ?',
						array($this->_credential)
			);
			if($login !== false)
			{
				return new Piwik_Auth_Result(Zend_Auth_Result::SUCCESS,
											$login,
											array() // message empty
											);
			}
			else
			{
				return new Piwik_Auth_Result( Zend_Auth_Result::FAILURE,
											$this->_identity,
											array()
											);
			}
		}

		// if not then we return the result of the database authentification provided by zend
		return parent::authenticate();
	}

	public function getTokenAuth()
	{
		return $this->_credential;
	}
}



/**
 *
 * @package Piwik
 */
class Piwik_Auth_Result extends Zend_Auth_Result
{
	public function __construct($code, $identity, array $messages = array())
	{
		$this->_code		= (int)$code;
		$this->_identity	= $identity;
		$this->_messages	= $messages;
	}
}