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

Commit

Permalink
ENH: Refs #0963. Moved userapi and token models to core; moved authen…
Browse files Browse the repository at this point in the history
…tication component to core.
  • Loading branch information
yuzhengZ committed Mar 29, 2013
1 parent 19afa97 commit d65efd4
Show file tree
Hide file tree
Showing 46 changed files with 265 additions and 435 deletions.
2 changes: 1 addition & 1 deletion core/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function init()
/** Return the user dao */
protected function _getUser($args)
{
$authComponent = MidasLoader::loadComponent('Authentication', 'api');
$authComponent = MidasLoader::loadComponent('Authentication');
return $authComponent->getUser($args, $this->userSession->Dao);
}

Expand Down
123 changes: 84 additions & 39 deletions core/controllers/components/ApiComponent.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
=========================================================================*/

/** Web API Authentication Component */
class Api_AuthenticationComponent extends AppComponent
class AuthenticationComponent extends AppComponent
{

/** Constructor */
Expand All @@ -45,7 +45,7 @@ public function getUser($args, $sessionDao)
return 0;
}
$token = $args['token'];
$userApiModel = MidasLoader::loadModel('Userapi', 'api');
$userApiModel = MidasLoader::loadModel('Userapi');
$userapiDao = $userApiModel->getUserapiFromToken($token);
if(!$userapiDao)
{
Expand Down
85 changes: 85 additions & 0 deletions core/database/upgrade/3.2.13.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* Upgrade 3.2.13 move userapi and token to core
*/
class Upgrade_3_2_13 extends MIDASUpgrade
{

public function preUpgrade()
{

}

public function mysql()
{
$this->db->query("CREATE TABLE IF NOT EXISTS `api_userapi` (
`userapi_id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`apikey` varchar(40) NOT NULL,
`application_name` varchar(256) NOT NULL,
`token_expiration_time` int(11) NOT NULL,
`creation_date` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`userapi_id`)
)");
$this->db->query("RENAME TABLE `api_userapi` to `userapi`");

$this->db->query("CREATE TABLE IF NOT EXISTS `api_token` (
`token_id` bigint(20) NOT NULL AUTO_INCREMENT,
`userapi_id` bigint(20) NOT NULL,
`token` varchar(40) NOT NULL,
`expiration_date` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`token_id`)
)");
$this->db->query("RENAME TABLE `api_token` to `token`");
}

public function pgsql()
{
$this->db->query("CREATE TABLE api_userapi (
userapi_id serial PRIMARY KEY,
user_id bigint NOT NULL,
apikey character varying(40) NOT NULL,
application_name character varying(256) NOT NULL,
token_expiration_time integer NOT NULL,
creation_date timestamp without time zone
)");
$this->db->query("ALTER TABLE api_userapi_userapi_id_seq RENAME TO userapi_userapi_id_seq");
$this->db->query("ALTER TABLE api_userapi RENAME TO userapi");
$this->db->query("ALTER INDEX api_userapi_pkey RENAME TO userapi_pkey");

$this->db->query("CREATE TABLE api_token (
token_id serial PRIMARY KEY,
userapi_id bigint NOT NULL,
token character varying(40) NOT NULL,
expiration_date timestamp without time zone
)");
$this->db->query("ALTER TABLE api_token_token_id_seq RENAME TO token_token_id_seq");
$this->db->query("ALTER TABLE api_token RENAME TO token");
$this->db->query("ALTER INDEX api_token_pkey RENAME TO token_pkey");
}

public function postUpgrade()
{
$userModel = MidasLoader::loadModel('User');
$userapiModel = MidasLoader::loadModel('Userapi');

//limit this to 100 users; there shouldn't be very many when api is installed
$users = $userModel->getAll(false, 100, 'admin');
foreach($users as $user)
{
$userApiDao = $userapiModel->getByAppAndEmail('Default', $user->getEmail());
if($userApiDao != false)
{
$userDefaultApiKey = $userApiDao->getApikey();
if(!empty($userDefaultApiKey))
{
continue;
}
}
$userapiModel->createDefaultApiKey($user);
}
}

}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
abstract class Api_TokenModelBase extends Api_AppModel
abstract class TokenModelBase extends AppModel
{
/** constructor */
public function __construct()
{
parent::__construct();
$this->_name = 'api_token';
$this->_name = 'token';
$this->_key = 'token_id';

$this->_mainData = array(
'token_id' => array('type' => MIDAS_DATA),
'userapi_id' => array('type' => MIDAS_DATA),
'token' => array('type' => MIDAS_DATA),
'expiration_date' => array('type' => MIDAS_DATA),
'userapi' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Userapi', 'module' => 'api', 'parent_column' => 'userapi_id', 'child_column' => 'userapi_id'),
'userapi' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Userapi', 'parent_column' => 'userapi_id', 'child_column' => 'userapi_id'),
);
$this->initialize(); // required
} // end __construct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
abstract class Api_UserapiModelBase extends Api_AppModel
abstract class UserapiModelBase extends AppModel
{
/** constructor */
public function __construct()
{
parent::__construct();
$this->_name = 'api_userapi';
$this->_name = 'userapi';
$this->_key = 'userapi_id';

$this->_mainData = array(
Expand Down Expand Up @@ -63,14 +63,14 @@ function createDefaultApiKey($userDao)

if(count($rowset)) //update existing record if we have one already
{
$userApiDao = $this->initDao('Userapi', $rowset[0], 'api');
$userApiDao = $this->initDao('Userapi', $rowset[0]);
$userApiDao->setApikey($key);
$this->save($userApiDao);
return;
}

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

$key = UtilityComponent::generateRandomString(40);

$userApiDao = MidasLoader::newDao('UserapiDao', 'api');
$userApiDao = MidasLoader::newDao('UserapiDao');
$userApiDao->setUserId($userDao->getKey());
$userApiDao->setApikey($key);
$userApiDao->setApplicationName($applicationname);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
limitations under the License.
=========================================================================*/
/** Dao for the api token */
class Api_TokenDao extends AppDao
class TokenDao extends AppDao
{
public $_model = 'Token';
public $_module = 'api';
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
limitations under the License.
=========================================================================*/
/** Dao for user api key */
class Api_UserapiDao extends AppDao
class UserapiDao extends AppDao
{
public $_model = 'Userapi';
public $_module = 'api';
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
limitations under the License.
=========================================================================*/

require_once BASE_PATH.'/modules/api/models/base/TokenModelBase.php';
require_once BASE_PATH.'/core/models/base/TokenModelBase.php';

/** Api token model implementation */
class Api_TokenModel extends Api_TokenModelBase
/** Api Token model implementation */
class TokenModel extends TokenModelBase
{
/** Remove all expired api tokens */
function cleanExpired()
Expand All @@ -30,7 +30,7 @@ function cleanExpired()
$rowset = $this->database->fetchAll($sql);
foreach($rowset as $row)
{
$tmpDao = $this->initDao('Token', $row, 'api');
$tmpDao = $this->initDao('Token', $row);
parent::delete($tmpDao);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
limitations under the License.
=========================================================================*/
//App::import("Vendor",'Sanitize');
require_once BASE_PATH.'/modules/api/models/base/UserapiModelBase.php';
require_once BASE_PATH.'/core/models/base/UserapiModelBase.php';

/** User api key model implementation */
class Api_UserapiModel extends Api_UserapiModelBase
class UserapiModel extends UserapiModelBase
{
/**
* Get UserapiDao by
* @param string $appname Application Name
* @param string $email
* @return Api_UserapiDao
* @return UserapiDao
*/
function getByAppAndEmail($appname, $email)
{
Expand All @@ -43,15 +43,15 @@ function getByAppAndEmail($appname, $email)
}
$row = $this->database->fetchRow($this->database->select()->where('application_name = ?', $appname)
->where('user_id = ?', $userDao->getKey()));
$dao = $this->initDao('Userapi', $row, 'api');
$dao = $this->initDao('Userapi', $row);
return $dao;
} // end getByApikey

/**
* Get UserapiDao by
* @param string $appname Application Name
* @param UserDao $userDao
* @return Api_UserapiDao
* @return UserapiDao
*/
function getByAppAndUser($appname, $userDao)
{
Expand All @@ -61,7 +61,7 @@ function getByAppAndUser($appname, $userDao)
}
$row = $this->database->fetchRow($this->database->select()->where('application_name = ?', $appname)
->where('user_id = ?', $userDao->getKey()));
$dao = $this->initDao('Userapi', $row, 'api');
$dao = $this->initDao('Userapi', $row);
return $dao;
} // end getByAppAndUser

Expand Down Expand Up @@ -90,8 +90,8 @@ function getToken($email, $apikey, $appname)

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


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

if(!empty($tokenDao))
{
Expand All @@ -126,25 +126,25 @@ function getToken($email, $apikey, $appname)

$sql = $this->database->select()
->setIntegrityCheck(false)
->from(array('u' => 'api_userapi'))
->from(array('u' => 'userapi'))
->where('u.user_id = ?', $userDao->getKey())
->where('u.application_name = ?', $appname)
->where('u.apikey = ?', $apikey);

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

if(!$userapiDao)
{
return false;
}

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

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

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

$sql = $this->database->select()
->setIntegrityCheck(false)
->from(array('u' => 'api_userapi'))
->join(array('t' => 'api_token'),
->from(array('u' => 'userapi'))
->join(array('t' => 'token'),
' u.userapi_id = t.userapi_id', array() )
->where('t.expiration_date > ?', $now)
->where('t.token = ?', $token);

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

/** Get the user's keys */
Expand All @@ -186,7 +186,7 @@ function getByUser($userDao)
$return = array();
foreach($rowset as $row)
{
$return[] = $this->initDao('Userapi', $row, 'api');
$return[] = $this->initDao('Userapi', $row);
}
return $return;
}
Expand Down
3 changes: 2 additions & 1 deletion core/tests/databaseDataset/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@

<itempolicyuser item_id="1006" user_id="4" policy='0' />
<itempolicyuser item_id="1006" user_id="5" policy='1' />


<newuserinvitation newuserinvitation_id="1001" email="dummy@dummy.com" auth_key="12345" inviter_id="4" community_id="2001" group_id="3003" date_creation="2011-01-27 12:11:02" />
<pendinguser pendinguser_id="1001" email="dummy@dummy.com" auth_key="12345" firstname="Dummy" lastname="Dummy" date_creation="2011-01-27 12:11:02" />
<password hash="dummy_hash" />

</dataset>
9 changes: 9 additions & 0 deletions core/tests/databaseDataset/userapi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>

<token token_id="1000" userapi_id="1000" token="cleanTable" expiration_date="2011-01-27 12:09:02" />
<userapi userapi_id="1000" user_id="1000" apikey="cleanTable" application_name="cleanTable"
token_expiration_time="100" creation_date="2011-01-27 12:09:02" />

</dataset>

1 change: 0 additions & 1 deletion core/tests/models/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ add_midas_test( ItempolicygroupModel ItempolicygroupModelTest.php )
add_midas_test( ItempolicyuserModel ItempolicyuserModelTest.php )
add_midas_test( MetadataModel MetadataModelTest.php )
add_midas_test( TreeIndices TreeIndicesTest.php )
add_midas_test( UserModel UserModelTest.php )
Loading

0 comments on commit d65efd4

Please sign in to comment.