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

Commit d65efd4

Browse files
committed
ENH: Refs #0963. Moved userapi and token models to core; moved authentication component to core.
1 parent 19afa97 commit d65efd4

File tree

46 files changed

+265
-435
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+265
-435
lines changed

core/ApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function init()
4242
/** Return the user dao */
4343
protected function _getUser($args)
4444
{
45-
$authComponent = MidasLoader::loadComponent('Authentication', 'api');
45+
$authComponent = MidasLoader::loadComponent('Authentication');
4646
return $authComponent->getUser($args, $this->userSession->Dao);
4747
}
4848

core/controllers/components/ApiComponent.php

Lines changed: 84 additions & 39 deletions
Large diffs are not rendered by default.

modules/api/controllers/components/AuthenticationComponent.php renamed to core/controllers/components/AuthenticationComponent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
=========================================================================*/
2020

2121
/** Web API Authentication Component */
22-
class Api_AuthenticationComponent extends AppComponent
22+
class AuthenticationComponent extends AppComponent
2323
{
2424

2525
/** Constructor */
@@ -45,7 +45,7 @@ public function getUser($args, $sessionDao)
4545
return 0;
4646
}
4747
$token = $args['token'];
48-
$userApiModel = MidasLoader::loadModel('Userapi', 'api');
48+
$userApiModel = MidasLoader::loadModel('Userapi');
4949
$userapiDao = $userApiModel->getUserapiFromToken($token);
5050
if(!$userapiDao)
5151
{

core/database/upgrade/3.2.13.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/**
4+
* Upgrade 3.2.13 move userapi and token to core
5+
*/
6+
class Upgrade_3_2_13 extends MIDASUpgrade
7+
{
8+
9+
public function preUpgrade()
10+
{
11+
12+
}
13+
14+
public function mysql()
15+
{
16+
$this->db->query("CREATE TABLE IF NOT EXISTS `api_userapi` (
17+
`userapi_id` bigint(20) NOT NULL AUTO_INCREMENT,
18+
`user_id` bigint(20) NOT NULL,
19+
`apikey` varchar(40) NOT NULL,
20+
`application_name` varchar(256) NOT NULL,
21+
`token_expiration_time` int(11) NOT NULL,
22+
`creation_date` timestamp NULL DEFAULT NULL,
23+
PRIMARY KEY (`userapi_id`)
24+
)");
25+
$this->db->query("RENAME TABLE `api_userapi` to `userapi`");
26+
27+
$this->db->query("CREATE TABLE IF NOT EXISTS `api_token` (
28+
`token_id` bigint(20) NOT NULL AUTO_INCREMENT,
29+
`userapi_id` bigint(20) NOT NULL,
30+
`token` varchar(40) NOT NULL,
31+
`expiration_date` timestamp NULL DEFAULT NULL,
32+
PRIMARY KEY (`token_id`)
33+
)");
34+
$this->db->query("RENAME TABLE `api_token` to `token`");
35+
}
36+
37+
public function pgsql()
38+
{
39+
$this->db->query("CREATE TABLE api_userapi (
40+
userapi_id serial PRIMARY KEY,
41+
user_id bigint NOT NULL,
42+
apikey character varying(40) NOT NULL,
43+
application_name character varying(256) NOT NULL,
44+
token_expiration_time integer NOT NULL,
45+
creation_date timestamp without time zone
46+
)");
47+
$this->db->query("ALTER TABLE api_userapi_userapi_id_seq RENAME TO userapi_userapi_id_seq");
48+
$this->db->query("ALTER TABLE api_userapi RENAME TO userapi");
49+
$this->db->query("ALTER INDEX api_userapi_pkey RENAME TO userapi_pkey");
50+
51+
$this->db->query("CREATE TABLE api_token (
52+
token_id serial PRIMARY KEY,
53+
userapi_id bigint NOT NULL,
54+
token character varying(40) NOT NULL,
55+
expiration_date timestamp without time zone
56+
)");
57+
$this->db->query("ALTER TABLE api_token_token_id_seq RENAME TO token_token_id_seq");
58+
$this->db->query("ALTER TABLE api_token RENAME TO token");
59+
$this->db->query("ALTER INDEX api_token_pkey RENAME TO token_pkey");
60+
}
61+
62+
public function postUpgrade()
63+
{
64+
$userModel = MidasLoader::loadModel('User');
65+
$userapiModel = MidasLoader::loadModel('Userapi');
66+
67+
//limit this to 100 users; there shouldn't be very many when api is installed
68+
$users = $userModel->getAll(false, 100, 'admin');
69+
foreach($users as $user)
70+
{
71+
$userApiDao = $userapiModel->getByAppAndEmail('Default', $user->getEmail());
72+
if($userApiDao != false)
73+
{
74+
$userDefaultApiKey = $userApiDao->getApikey();
75+
if(!empty($userDefaultApiKey))
76+
{
77+
continue;
78+
}
79+
}
80+
$userapiModel->createDefaultApiKey($user);
81+
}
82+
}
83+
84+
}
85+
?>

modules/api/models/base/TokenModelBase.php renamed to core/models/base/TokenModelBase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
See the License for the specific language governing permissions and
1818
limitations under the License.
1919
=========================================================================*/
20-
abstract class Api_TokenModelBase extends Api_AppModel
20+
abstract class TokenModelBase extends AppModel
2121
{
2222
/** constructor */
2323
public function __construct()
2424
{
2525
parent::__construct();
26-
$this->_name = 'api_token';
26+
$this->_name = 'token';
2727
$this->_key = 'token_id';
2828

2929
$this->_mainData = array(
3030
'token_id' => array('type' => MIDAS_DATA),
3131
'userapi_id' => array('type' => MIDAS_DATA),
3232
'token' => array('type' => MIDAS_DATA),
3333
'expiration_date' => array('type' => MIDAS_DATA),
34-
'userapi' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Userapi', 'module' => 'api', 'parent_column' => 'userapi_id', 'child_column' => 'userapi_id'),
34+
'userapi' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Userapi', 'parent_column' => 'userapi_id', 'child_column' => 'userapi_id'),
3535
);
3636
$this->initialize(); // required
3737
} // end __construct()

modules/api/models/base/UserapiModelBase.php renamed to core/models/base/UserapiModelBase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
See the License for the specific language governing permissions and
1818
limitations under the License.
1919
=========================================================================*/
20-
abstract class Api_UserapiModelBase extends Api_AppModel
20+
abstract class UserapiModelBase extends AppModel
2121
{
2222
/** constructor */
2323
public function __construct()
2424
{
2525
parent::__construct();
26-
$this->_name = 'api_userapi';
26+
$this->_name = 'userapi';
2727
$this->_key = 'userapi_id';
2828

2929
$this->_mainData = array(
@@ -63,14 +63,14 @@ function createDefaultApiKey($userDao)
6363

6464
if(count($rowset)) //update existing record if we have one already
6565
{
66-
$userApiDao = $this->initDao('Userapi', $rowset[0], 'api');
66+
$userApiDao = $this->initDao('Userapi', $rowset[0]);
6767
$userApiDao->setApikey($key);
6868
$this->save($userApiDao);
6969
return;
7070
}
7171

7272
// Otherwise save new default key
73-
$userApiDao = MidasLoader::newDao('UserapiDao', 'api');
73+
$userApiDao = MidasLoader::newDao('UserapiDao');
7474
$userApiDao->setUserId($userDao->getKey());
7575
$userApiDao->setApplicationName('Default');
7676
$userApiDao->setApikey($key);
@@ -97,7 +97,7 @@ function createKey($userDao, $applicationname, $tokenexperiationtime)
9797

9898
$key = UtilityComponent::generateRandomString(40);
9999

100-
$userApiDao = MidasLoader::newDao('UserapiDao', 'api');
100+
$userApiDao = MidasLoader::newDao('UserapiDao');
101101
$userApiDao->setUserId($userDao->getKey());
102102
$userApiDao->setApikey($key);
103103
$userApiDao->setApplicationName($applicationname);

modules/api/models/dao/TokenDao.php renamed to core/models/dao/TokenDao.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
limitations under the License.
1919
=========================================================================*/
2020
/** Dao for the api token */
21-
class Api_TokenDao extends AppDao
21+
class TokenDao extends AppDao
2222
{
2323
public $_model = 'Token';
24-
public $_module = 'api';
2524
}
2625
?>

modules/api/models/dao/UserapiDao.php renamed to core/models/dao/UserapiDao.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
limitations under the License.
1919
=========================================================================*/
2020
/** Dao for user api key */
21-
class Api_UserapiDao extends AppDao
21+
class UserapiDao extends AppDao
2222
{
2323
public $_model = 'Userapi';
24-
public $_module = 'api';
2524
}
2625
?>

modules/api/models/pdo/TokenModel.php renamed to core/models/pdo/TokenModel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
limitations under the License.
1919
=========================================================================*/
2020

21-
require_once BASE_PATH.'/modules/api/models/base/TokenModelBase.php';
21+
require_once BASE_PATH.'/core/models/base/TokenModelBase.php';
2222

23-
/** Api token model implementation */
24-
class Api_TokenModel extends Api_TokenModelBase
23+
/** Api Token model implementation */
24+
class TokenModel extends TokenModelBase
2525
{
2626
/** Remove all expired api tokens */
2727
function cleanExpired()
@@ -30,7 +30,7 @@ function cleanExpired()
3030
$rowset = $this->database->fetchAll($sql);
3131
foreach($rowset as $row)
3232
{
33-
$tmpDao = $this->initDao('Token', $row, 'api');
33+
$tmpDao = $this->initDao('Token', $row);
3434
parent::delete($tmpDao);
3535
}
3636
}

modules/api/models/pdo/UserapiModel.php renamed to core/models/pdo/UserapiModel.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
limitations under the License.
1919
=========================================================================*/
2020
//App::import("Vendor",'Sanitize');
21-
require_once BASE_PATH.'/modules/api/models/base/UserapiModelBase.php';
21+
require_once BASE_PATH.'/core/models/base/UserapiModelBase.php';
2222

2323
/** User api key model implementation */
24-
class Api_UserapiModel extends Api_UserapiModelBase
24+
class UserapiModel extends UserapiModelBase
2525
{
2626
/**
2727
* Get UserapiDao by
2828
* @param string $appname Application Name
2929
* @param string $email
30-
* @return Api_UserapiDao
30+
* @return UserapiDao
3131
*/
3232
function getByAppAndEmail($appname, $email)
3333
{
@@ -43,15 +43,15 @@ function getByAppAndEmail($appname, $email)
4343
}
4444
$row = $this->database->fetchRow($this->database->select()->where('application_name = ?', $appname)
4545
->where('user_id = ?', $userDao->getKey()));
46-
$dao = $this->initDao('Userapi', $row, 'api');
46+
$dao = $this->initDao('Userapi', $row);
4747
return $dao;
4848
} // end getByApikey
4949

5050
/**
5151
* Get UserapiDao by
5252
* @param string $appname Application Name
5353
* @param UserDao $userDao
54-
* @return Api_UserapiDao
54+
* @return UserapiDao
5555
*/
5656
function getByAppAndUser($appname, $userDao)
5757
{
@@ -61,7 +61,7 @@ function getByAppAndUser($appname, $userDao)
6161
}
6262
$row = $this->database->fetchRow($this->database->select()->where('application_name = ?', $appname)
6363
->where('user_id = ?', $userDao->getKey()));
64-
$dao = $this->initDao('Userapi', $row, 'api');
64+
$dao = $this->initDao('Userapi', $row);
6565
return $dao;
6666
} // end getByAppAndUser
6767

@@ -90,8 +90,8 @@ function getToken($email, $apikey, $appname)
9090

9191
$sql = $this->database->select()
9292
->setIntegrityCheck(false)
93-
->from(array('t' => 'api_token'))
94-
->join(array('u' => 'api_userapi'),
93+
->from(array('t' => 'token'))
94+
->join(array('u' => 'userapi'),
9595
' u.userapi_id= t.userapi_id', array() )
9696
->where('u.user_id = ?', $userDao->getKey())
9797
->where('u.application_name = ?', $appname)
@@ -100,7 +100,7 @@ function getToken($email, $apikey, $appname)
100100

101101

102102
$row = $this->database->fetchRow($sql);
103-
$tokenDao = $this->initDao('Token', $row, 'api');
103+
$tokenDao = $this->initDao('Token', $row);
104104

105105
if(!empty($tokenDao))
106106
{
@@ -126,25 +126,25 @@ function getToken($email, $apikey, $appname)
126126

127127
$sql = $this->database->select()
128128
->setIntegrityCheck(false)
129-
->from(array('u' => 'api_userapi'))
129+
->from(array('u' => 'userapi'))
130130
->where('u.user_id = ?', $userDao->getKey())
131131
->where('u.application_name = ?', $appname)
132132
->where('u.apikey = ?', $apikey);
133133

134134
$row = $this->database->fetchRow($sql);
135-
$userapiDao = $this->initDao('Userapi', $row, 'api');
135+
$userapiDao = $this->initDao('Userapi', $row);
136136

137137
if(!$userapiDao)
138138
{
139139
return false;
140140
}
141141

142-
$tokenDao = MidasLoader::newDao('TokenDao', 'api');
142+
$tokenDao = MidasLoader::newDao('TokenDao');
143143
$tokenDao->setUserapiId($userapiDao->getKey());
144144
$tokenDao->setToken($token);
145145
$tokenDao->setExpirationDate(date("c", time() + $userapiDao->getTokenExpirationTime() * 60));
146146

147-
$tokenModel = MidasLoader::loadModel('Token', 'api');
147+
$tokenModel = MidasLoader::loadModel('Token');
148148
$tokenModel->save($tokenDao);
149149

150150
// We do some cleanup of all the other keys that have expired
@@ -165,14 +165,14 @@ function getUserapiFromToken($token)
165165

166166
$sql = $this->database->select()
167167
->setIntegrityCheck(false)
168-
->from(array('u' => 'api_userapi'))
169-
->join(array('t' => 'api_token'),
168+
->from(array('u' => 'userapi'))
169+
->join(array('t' => 'token'),
170170
' u.userapi_id = t.userapi_id', array() )
171171
->where('t.expiration_date > ?', $now)
172172
->where('t.token = ?', $token);
173173

174174
$row = $this->database->fetchRow($sql);
175-
return $this->initDao('Userapi', $row, 'api');
175+
return $this->initDao('Userapi', $row);
176176
}
177177

178178
/** Get the user's keys */
@@ -186,7 +186,7 @@ function getByUser($userDao)
186186
$return = array();
187187
foreach($rowset as $row)
188188
{
189-
$return[] = $this->initDao('Userapi', $row, 'api');
189+
$return[] = $this->initDao('Userapi', $row);
190190
}
191191
return $return;
192192
}

0 commit comments

Comments
 (0)