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

Commit 119d181

Browse files
committed
ENH: refs #237. Add default API key behavior on password change
1 parent 6daf5ea commit 119d181

File tree

5 files changed

+86
-39
lines changed

5 files changed

+86
-39
lines changed

core/controllers/UserController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ public function settingsAction()
377377
$this->userSession->Dao = $userDao;
378378
}
379379
echo JsonComponent::encode(array(true, $this->t('Changes saved')));
380+
Zend_Registry::get('notifier')->callback('CALLBACK_CORE_PASSWORD_CHANGED', array('userDao'=>$userDao));
380381
}
381382
else
382383
{
@@ -614,7 +615,7 @@ public function settingsAction()
614615
$this->view->jsonSettings['passwordErrorMatch'] = $this->t('The passwords are not the same');
615616
$this->view->jsonSettings = JsonComponent::encode($this->view->jsonSettings);
616617

617-
$this->view->customTabs = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_GET_CONFI_TABS', array());
618+
$this->view->customTabs = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_GET_CONFIG_TABS', array());
618619
}
619620

620621
/** User page action*/

modules/api/Notification.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@
33
class Api_Notification extends MIDAS_Notification
44
{
55
public $_models=array('User');
6-
6+
77
/** init notification process*/
88
public function init()
99
{
10-
$this->addCallBack('CALLBACK_CORE_GET_CONFI_TABS', 'getConfigTabs');
10+
$this->addCallBack('CALLBACK_CORE_GET_CONFIG_TABS', 'getConfigTabs');
11+
$this->addCallBack('CALLBACK_CORE_PASSWORD_CHANGED', 'setDefaultWebApiKey');
12+
$this->addCallBack('CALLBACK_CORE_NEW_USER_ADDED', 'setDefaultWebApiKey');
1113
}//end init
12-
14+
1315
/** get Config Tabs */
1416
public function getConfigTabs()
1517
{
1618
$fc = Zend_Controller_Front::getInstance();
1719
$moduleWebroot = $fc->getBaseUrl().'/api';
1820
return array('Api' => $moduleWebroot.'/config/usertab');
1921
}
22+
23+
/** Reset the user's default web API key */
24+
public function setDefaultWebApiKey($params)
25+
{
26+
if(!isset($params['userDao']))
27+
{
28+
throw new Zend_Exception('Error: userDao parameter required');
29+
}
30+
$this->ModelLoader = new MIDAS_ModelLoader();
31+
$userApiModel = $this->ModelLoader->loadModel('Userapi', 'api');
32+
$userApiModel->createDefaultApiKey($params['userDao']);
33+
}
2034
} //end class
2135
?>

modules/api/models/base/UserapiModelBase.php

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,56 @@ public function __construct()
1818
);
1919
$this->initialize(); // required
2020
} // end __construct()
21-
22-
abstract function createKeyFromEmailPassword($appname,$email,$password);
23-
abstract function getByAppAndEmail($appname,$email);
24-
abstract function getByAppAndUser($appname,$userDao);
25-
abstract function getToken($email,$apikey,$appname);
26-
abstract function getUserapiFromToken($token);
27-
abstract function getByUser($userDao);
28-
29-
21+
22+
abstract function createKeyFromEmailPassword($appname,$email,$password);
23+
abstract function getByAppAndEmail($appname,$email);
24+
abstract function getByAppAndUser($appname,$userDao);
25+
abstract function getToken($email,$apikey,$appname);
26+
abstract function getUserapiFromToken($token);
27+
abstract function getByUser($userDao);
28+
29+
/**
30+
* Create the user's default API key
31+
* @param string $userDao the user
32+
* @return success boolean
33+
*/
34+
function createDefaultApiKey($userDao)
35+
{
36+
if(!$userDao instanceof UserDao)
37+
{
38+
throw new Zend_Exception('Error parameter: must be a userDao object');
39+
}
40+
41+
// Remove prior default api key(s)
42+
$rowset = $this->database->fetchAll($this->database->select()
43+
->where('user_id = ?', $userDao->getKey())
44+
->where('application_name = ?', 'Default'));
45+
foreach($rowset as $row)
46+
{
47+
$userApiDao= $this->initDao('Userapi', $row,'api');
48+
$this->delete($userApiDao);
49+
}
50+
51+
// Save new default key
52+
$key = md5($userDao->getEmail().$userDao->getPassword().'Default');
53+
$this->loadDaoClass('UserapiDao','api');
54+
$userApiDao=new Api_UserapiDao();
55+
$userApiDao->setUserId($userDao->getKey());
56+
$userApiDao->setApplicationName('Default');
57+
$userApiDao->setApikey($key);
58+
$userApiDao->setTokenExpirationTime(100);
59+
$userApiDao->setCreationDate(date('c'));
60+
$this->save($userApiDao);
61+
}
62+
3063
/** Create a new API key */
3164
function createKey($userDao,$applicationname,$tokenexperiationtime)
3265
{
3366
if(!$userDao instanceof UserDao||!is_string($applicationname)||!is_string($tokenexperiationtime) || empty($applicationname))
3467
{
3568
throw new Zend_Exception("Error parameter");
3669
}
37-
70+
3871
// Check that the applicationname doesn't exist for this user
3972
$userapiDao=$this->getByAppAndUser($applicationname, $userDao);
4073
if(!empty($userapiDao))
@@ -61,7 +94,7 @@ function make_seed_recoverpass()
6194
{
6295
$key .= substr($keychars, rand(0, $max), 1);
6396
}
64-
97+
6598
$this->loadDaoClass('UserapiDao','api');
6699
$userApiDao=new Api_UserapiDao();
67100
$userApiDao->setUserId($userDao->getKey());
@@ -73,6 +106,6 @@ function make_seed_recoverpass()
73106
$this->save($userApiDao);
74107
return $userApiDao;
75108
}//end createKey
76-
109+
77110
} // end class AssetstoreModelBase
78111
?>

modules/api/models/pdo/UserapiModel.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
class Api_UserapiModel extends Api_UserapiModelBase
66
{
77

8-
98
/** Create an API key from a login and password */
109
function createKeyFromEmailPassword($appname,$email,$password)
1110
{
1211
if(!is_string($appname)||!is_string($email)||!is_string($password))
1312
{
1413
throw new Zend_Exception("Error parameter");
1514
}
16-
15+
1716
$this->ModelLoader = new MIDAS_ModelLoader();
1817
$userModel=$this->ModelLoader->loadModel('User');
1918

@@ -24,7 +23,7 @@ function createKeyFromEmailPassword($appname,$email,$password)
2423
{
2524
return false;
2625
}
27-
26+
2827
// Find if we already have an apikey
2928
$ret = $this->getByAppAndEmail($appname,$email);
3029

@@ -40,12 +39,12 @@ function createKeyFromEmailPassword($appname,$email,$password)
4039
}
4140
return false;
4241
} // end function createKeyFromEmailPassword
43-
42+
4443
/**
4544
* Get UserapiDao by
4645
* @param string $appname Application Name
47-
* @param string $email
48-
* @return Api_UserapiDao
46+
* @param string $email
47+
* @return Api_UserapiDao
4948
*/
5049
function getByAppAndEmail($appname,$email)
5150
{
@@ -61,16 +60,16 @@ function getByAppAndEmail($appname,$email)
6160
return false;
6261
}
6362
$row = $this->database->fetchRow($this->database->select()->where('application_name = ?', $appname)
64-
->where('user_id = ?', $userDao->getKey()));
63+
->where('user_id = ?', $userDao->getKey()));
6564
$dao= $this->initDao('Userapi', $row,'api');
6665
return $dao;
6766
} // end getByApikey
68-
67+
6968
/**
7069
* Get UserapiDao by
7170
* @param string $appname Application Name
72-
* @param UserDao $userDao
73-
* @return Api_UserapiDao
71+
* @param UserDao $userDao
72+
* @return Api_UserapiDao
7473
*/
7574
function getByAppAndUser($appname,$userDao)
7675
{
@@ -79,18 +78,18 @@ function getByAppAndUser($appname,$userDao)
7978
throw new Zend_Exception("Error parameter");
8079
}
8180
$row = $this->database->fetchRow($this->database->select()->where('application_name = ?', $appname)
82-
->where('user_id = ?', $userDao->getKey()));
81+
->where('user_id = ?', $userDao->getKey()));
8382
$dao= $this->initDao('Userapi', $row,'api');
8483
return $dao;
8584
} // end getByAppAndUser
8685

87-
86+
8887
/**
8988
* Return the tokendao
9089
* @param type $email
9190
* @param type $apikey
9291
* @param type $appname
93-
* @return Api_TokenDao
92+
* @return Api_TokenDao
9493
*/
9594
function getToken($email,$apikey,$appname)
9695
{
@@ -107,7 +106,7 @@ function getToken($email,$apikey,$appname)
107106
return false;
108107
}
109108
$now = date("c");
110-
109+
111110
$sql= $this->database->select()
112111
->setIntegrityCheck(false)
113112
->from(array('t' => 'api_token'))
@@ -117,7 +116,7 @@ function getToken($email,$apikey,$appname)
117116
->where('u.application_name = ?', $appname)
118117
->where('t.expiration_date > ?', $now)
119118
->where('u.apikey = ?', $apikey) ;
120-
119+
121120

122121
$row = $this->database->fetchRow($sql);
123122
$tokenDao= $this->initDao('Token', $row,'api');
@@ -147,7 +146,7 @@ function make_seed_recoverpass_token()
147146
}
148147

149148
// Find the api id
150-
149+
151150
$sql= $this->database->select()
152151
->setIntegrityCheck(false)
153152
->from(array('u' => 'api_userapi'))
@@ -156,8 +155,8 @@ function make_seed_recoverpass_token()
156155
->where('u.apikey = ?', $apikey) ;
157156

158157
$row = $this->database->fetchRow($sql);
159-
$userapiDao= $this->initDao('Userapi', $row,'api');
160-
158+
$userapiDao= $this->initDao('Userapi', $row,'api');
159+
161160
if(!$userapiDao)
162161
{
163162
return false;
@@ -170,9 +169,9 @@ function make_seed_recoverpass_token()
170169
$tokenDao->setExpirationDate(date("c",time()+$userapiDao->getTokenExpirationTime()*60));
171170

172171
$tokenModel=$this->ModelLoader->loadModel('Token','api');
173-
172+
174173
$tokenModel->save($tokenDao);
175-
174+
176175
// We do some cleanup of all the other keys that have expired
177176
$tokenModel->cleanExpired();
178177

@@ -188,15 +187,15 @@ function getUserapiFromToken($token)
188187
throw new Zend_Exception("Error parameter");
189188
}
190189
$now = date("c");
191-
190+
192191
$sql= $this->database->select()
193192
->setIntegrityCheck(false)
194193
->from(array('u' => 'api_userapi'))
195194
->join(array('t' => 'api_token'),
196195
' u.userapi_id = t.userapi_id',array() )
197196
->where('t.expiration_date > ?', $now)
198197
->where('t.token = ?', $token) ;
199-
198+
200199

201200
$row = $this->database->fetchRow($sql);
202201
return $this->initDao('Userapi', $row,'api');

modules/thumbnailcreator/configs/module.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ category= Filter
1111
dependencies= scheduler
1212

1313
;image magick folder
14-
imagemagick=
14+
imagemagick=

0 commit comments

Comments
 (0)