Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 4ba5ca7

Browse files
author
Jamie Snape
committed
Fix exception when user does not have a default API key
Fixes #103.
1 parent eb96e26 commit 4ba5ca7

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

core/controllers/components/ApisystemComponent.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ public function login($args)
159159
*/
160160
public function userApikeyDefault($args)
161161
{
162-
163162
/** @var ApihelperComponent $apihelperComponent */
164163
$apihelperComponent = MidasLoader::loadComponent('Apihelper');
165164
$apihelperComponent->validateParams($args, array('email', 'password'));
@@ -178,41 +177,44 @@ public function userApikeyDefault($args)
178177
} catch (Zend_Exception $exc) {
179178
throw new Exception('Login failed', MIDAS_INVALID_PARAMETER);
180179
}
181-
$authModule = false;
182-
foreach ($notifications as $user) {
183-
if ($user) {
184-
$userDao = $user;
185-
$authModule = true;
180+
$userDao = false;
181+
foreach ($notifications as $notification) {
182+
if ($notification) {
183+
$userDao = $notification;
186184
break;
187185
}
188186
}
187+
$hasAuthenticationModule = $userDao !== false;
189188

190189
/** @var UserModel $userModel */
191190
$userModel = MidasLoader::loadModel('User');
192191

193192
/** @var UserapiModel $userApiModel */
194193
$userApiModel = MidasLoader::loadModel('Userapi');
195-
if (!$authModule) {
194+
if ($userDao === false) {
196195
$userDao = $userModel->getByEmail($email);
197-
if (!$userDao) {
196+
if ($userDao === false) {
198197
throw new Exception('Login failed', MIDAS_INVALID_PARAMETER);
199198
}
200199
}
201200

202-
$instanceSalt = Zend_Registry::get('configGlobal')->password->prefix;
203-
if ($authModule || $userModel->hashExists(
204-
hash($userDao->getHashAlg(), $instanceSalt.$userDao->getSalt().$password)
201+
$prefix = Zend_Registry::get('configGlobal')->password->prefix;
202+
if ($hasAuthenticationModule || $userModel->hashExists(
203+
hash($userDao->getHashAlg(), $prefix.$userDao->getSalt().$password)
205204
)
206205
) {
207206
if ($userDao->getSalt() == '') {
208207
$userModel->convertLegacyPasswordHash($userDao, $password);
209208
}
210-
$defaultApiKey = $userApiModel->getByAppAndEmail('Default', $email)->getApikey();
209+
$userApiDao = $userApiModel->getByAppAndEmail('Default', $email);
210+
if ($userApiDao === false) {
211+
throw new Exception('User has no default API key', MIDAS_INVALID_PARAMETER);
212+
}
211213

212-
return array('apikey' => $defaultApiKey);
213-
} else {
214-
throw new Exception('Login failed', MIDAS_INVALID_PARAMETER);
214+
return array('apikey' => $userApiDao->getApikey());
215215
}
216+
217+
throw new Exception('Login failed', MIDAS_INVALID_PARAMETER);
216218
}
217219

218220
/**

0 commit comments

Comments
 (0)