diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2015-08-20 22:45:47 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-08-22 00:31:23 +0300 |
commit | 0c96ca8ce664605dcfaf473e98c84399b11f3b6d (patch) | |
tree | 1bb42fd20be74413810362b7cfe594c029525cfd /plugins/Login | |
parent | 022d043aff0c6b2130d2a8786570a05d547aeafb (diff) |
Reorganize code, no behavior change
Diffstat (limited to 'plugins/Login')
-rw-r--r-- | plugins/Login/Auth.php | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/plugins/Login/Auth.php b/plugins/Login/Auth.php index ade7fdfeca..e99b2a268a 100644 --- a/plugins/Login/Auth.php +++ b/plugins/Login/Auth.php @@ -13,7 +13,6 @@ use Piwik\AuthResult; use Piwik\Db; use Piwik\Plugins\UsersManager\Model; use Piwik\Session; -use Piwik\Plugins\UsersManager\API as UsersManagerAPI; class Auth implements \Piwik\Auth { @@ -22,6 +21,16 @@ class Auth implements \Piwik\Auth protected $md5Password; /** + * @var Model + */ + private $userModel; + + public function __construct() + { + $this->userModel = new Model(); + } + + /** * Authentication module's name, e.g., "Login" * * @return string @@ -51,14 +60,10 @@ class Auth implements \Piwik\Auth private function authenticateWithPassword($login, $password) { - $model = new Model(); - $user = $model->getUser($login); + $user = $this->userModel->getUser($login); if (!empty($user['login']) && $user['password'] === $password) { - $this->setTokenAuth($user['token_auth']); - $code = $user['superuser_access'] ? AuthResult::SUCCESS_SUPERUSER_AUTH_CODE : AuthResult::SUCCESS; - - return new AuthResult($code, $user['login'], $user['token_auth']); + return $this->authenticationSuccess($user); } return new AuthResult(AuthResult::FAILURE, $login, null); @@ -66,37 +71,40 @@ class Auth implements \Piwik\Auth private function authenticateWithToken($token) { - $model = new Model(); - $user = $model->getUserByTokenAuth($token); + $user = $this->userModel->getUserByTokenAuth($token); - if (empty($user['login'])) { - return new AuthResult(AuthResult::FAILURE, null, $token); + if (!empty($user['login'])) { + return $this->authenticationSuccess($user); } - $code = $user['superuser_access'] ? AuthResult::SUCCESS_SUPERUSER_AUTH_CODE : AuthResult::SUCCESS; - - return new AuthResult($code, $user['login'], $token); + return new AuthResult(AuthResult::FAILURE, null, $token); } private function authenticateWithTokenOrHashToken($token, $login) { - $model = new Model(); - $user = $model->getUser($login); + $user = $this->userModel->getUser($login); if (!empty($user['token_auth']) // authenticate either with the token or the "hash token" && ((SessionInitializer::getHashTokenAuth($login, $user['token_auth']) === $token) || $user['token_auth'] === $token) ) { - $this->setTokenAuth($user['token_auth']); - $code = $user['superuser_access'] ? AuthResult::SUCCESS_SUPERUSER_AUTH_CODE : AuthResult::SUCCESS; - - return new AuthResult($code, $user['login'], $user['token_auth']); + return $this->authenticationSuccess($user); } return new AuthResult(AuthResult::FAILURE, $login, $token); } + private function authenticationSuccess(array $user) + { + $this->setTokenAuth($user['token_auth']); + + $isSuperUser = $user['superuser_access']; + $code = $isSuperUser ? AuthResult::SUCCESS_SUPERUSER_AUTH_CODE : AuthResult::SUCCESS; + + return new AuthResult($code, $user['login'], $user['token_auth']); + } + /** * Returns the login of the user being authenticated. * |