diff options
author | diosmosis <benakamoorthi@fastmail.fm> | 2013-10-29 01:46:39 +0400 |
---|---|---|
committer | diosmosis <benakamoorthi@fastmail.fm> | 2013-10-29 01:47:10 +0400 |
commit | 05ef2e992ee93d8e922f4a6650d019ba8fa4a3ca (patch) | |
tree | 3434121659d91f325cb60f11962a7fcda4db3d43 | |
parent | 867da5056644d057fa74415af6b6059e188aa1c4 (diff) |
Remove Login.initSession event and move logic to new method in Auth interface.
-rw-r--r-- | core/Auth.php | 7 | ||||
-rw-r--r-- | plugins/Login/Auth.php | 37 | ||||
-rw-r--r-- | plugins/Login/Controller.php | 21 | ||||
-rw-r--r-- | plugins/Login/Login.php | 41 | ||||
-rw-r--r-- | plugins/UsersManager/Controller.php | 23 |
5 files changed, 46 insertions, 83 deletions
diff --git a/core/Auth.php b/core/Auth.php index 0b47da6ae1..9bf7433419 100644 --- a/core/Auth.php +++ b/core/Auth.php @@ -12,7 +12,7 @@ namespace Piwik; /** - * Interface for authentication modules + * Base for authentication modules * * @package Piwik * @subpackage Piwik_Auth @@ -32,6 +32,11 @@ interface Auth * @return AuthResult */ public function authenticate(); + + /** + * Authenticates the user and initializes the session. + */ + public function initSession($login, $md5Password, $rememberMe); } /** diff --git a/plugins/Login/Auth.php b/plugins/Login/Auth.php index bab6404a8d..fbbead00a0 100644 --- a/plugins/Login/Auth.php +++ b/plugins/Login/Auth.php @@ -10,11 +10,16 @@ */ namespace Piwik\Plugins\Login; +use Piwik\Piwik; use Piwik\AuthResult; use Piwik\Common; use Piwik\Config; +use Piwik\Cookie; +use Piwik\ProxyHttp; use Piwik\Db; +use Piwik\Session; use Piwik\Plugins\UsersManager\API; +use \Exception; /** * @@ -89,6 +94,38 @@ class Auth implements \Piwik\Auth } /** + * Authenticates the user and initializes the session. + */ + public function initSession($login, $md5Password, $rememberMe) + { + $tokenAuth = API::getInstance()->getTokenAuth($login, $md5Password); + + $this->setLogin($login); + $this->setTokenAuth($tokenAuth); + $authResult = $this->authenticate(); + + $authCookieName = Config::getInstance()->General['login_cookie_name']; + $authCookieExpiry = $rememberMe ? time() + Config::getInstance()->General['login_cookie_expire'] : 0; + $authCookiePath = Config::getInstance()->General['login_cookie_path']; + $cookie = new Cookie($authCookieName, $authCookieExpiry, $authCookiePath); + if (!$authResult->wasAuthenticationSuccessful()) { + $cookie->delete(); + throw new Exception(Piwik::translate('Login_LoginPasswordNotCorrect')); + } + + $cookie->set('login', $login); + $cookie->set('token_auth', $this->getHashTokenAuth($login, $authResult->getTokenAuth())); + $cookie->setSecure(ProxyHttp::isHttps()); + $cookie->setHttpOnly(true); + $cookie->save(); + + @Session::regenerateId(); + + // remove password reset entry if it exists + Login::removePasswordResetInfo($login); + } + + /** * Accessor to set login name * * @param string $login user login diff --git a/plugins/Login/Controller.php b/plugins/Login/Controller.php index abcf05d65f..b1b4cc41e0 100644 --- a/plugins/Login/Controller.php +++ b/plugins/Login/Controller.php @@ -164,27 +164,10 @@ class Controller extends \Piwik\Plugin\Controller */ protected function authenticateAndRedirect($login, $md5Password, $rememberMe, $urlToRedirect = 'index.php') { - $info = array('login' => $login, - 'md5Password' => $md5Password, - 'rememberMe' => $rememberMe, - ); Nonce::discardNonce('Login.login'); - /** - * This event is triggered to initialize a user session. You can use this event to authenticate user against - * third party systems. - * - * Example: - * ``` - * public function initSession($info) - * { - * $login = $info['login']; - * $md5Password = $info['md5Password']; - * $rememberMe = $info['rememberMe']; - * } - * ``` - */ - Piwik::postEvent('Login.initSession', array(&$info)); + \Piwik\Registry::get('auth')->initSession($login, $md5Password, $rememberMe); + Url::redirectToUrl($urlToRedirect); } diff --git a/plugins/Login/Login.php b/plugins/Login/Login.php index 5abd11b98a..261067c6ca 100644 --- a/plugins/Login/Login.php +++ b/plugins/Login/Login.php @@ -35,7 +35,6 @@ class Login extends \Piwik\Plugin 'Request.initAuthenticationObject' => 'initAuthenticationObject', 'User.isNotAuthorized' => 'noAccess', 'API.Request.authenticate' => 'ApiRequestAuthenticate', - 'Login.initSession' => 'initSession', ); return $hooks; } @@ -94,46 +93,6 @@ class Login extends \Piwik\Plugin } /** - * Authenticate user and initializes the session. - * Listens to Login.initSession hook. - * - * @throws Exception - */ - public function initSession($info) - { - $login = $info['login']; - $md5Password = $info['md5Password']; - $rememberMe = $info['rememberMe']; - - $tokenAuth = API::getInstance()->getTokenAuth($login, $md5Password); - - $auth = \Piwik\Registry::get('auth'); - $auth->setLogin($login); - $auth->setTokenAuth($tokenAuth); - $authResult = $auth->authenticate(); - - $authCookieName = Config::getInstance()->General['login_cookie_name']; - $authCookieExpiry = $rememberMe ? time() + Config::getInstance()->General['login_cookie_expire'] : 0; - $authCookiePath = Config::getInstance()->General['login_cookie_path']; - $cookie = new Cookie($authCookieName, $authCookieExpiry, $authCookiePath); - if (!$authResult->wasAuthenticationSuccessful()) { - $cookie->delete(); - throw new Exception(Piwik::translate('Login_LoginPasswordNotCorrect')); - } - - $cookie->set('login', $login); - $cookie->set('token_auth', $auth->getHashTokenAuth($login, $authResult->getTokenAuth())); - $cookie->setSecure(ProxyHttp::isHttps()); - $cookie->setHttpOnly(true); - $cookie->save(); - - @Session::regenerateId(); - - // remove password reset entry if it exists - self::removePasswordResetInfo($login); - } - - /** * Stores password reset info for a specific login. * * @param string $login The user login for whom a password change was requested. diff --git a/plugins/UsersManager/Controller.php b/plugins/UsersManager/Controller.php index 78d9dda313..9bd6eac1c9 100644 --- a/plugins/UsersManager/Controller.php +++ b/plugins/UsersManager/Controller.php @@ -316,28 +316,7 @@ class Controller extends \Piwik\Plugin\ControllerAdmin // logs the user in with the new password if ($newPassword !== false) { - $info = array( - 'login' => $userLogin, - 'md5Password' => md5($newPassword), - 'rememberMe' => false, - ); - - /** - * This event is triggered to initialize a user session. You can use this event to authenticate user against - * third party systems. - * - * Example: - * ``` - * public function initSession($info) - * { - * $login = $info['login']; - * $md5Password = $info['md5Password']; - * $rememberMe = $info['rememberMe']; - * } - * ``` - * @todo this event is also triggered twice. - */ - Piwik::postEvent('Login.initSession', array($info)); + \Piwik\Registry::get('auth')->initSession($userLogin, md5($newPassword), $rememberMe = false); } APIUsersManager::getInstance()->setUserPreference($userLogin, |