summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@owncloud.com>2016-05-08 17:41:37 +0200
committerThomas Müller <thomas.mueller@tmit.eu>2016-05-11 13:36:46 +0200
commitaf707fba41634b70115d47de86efe2ce2bf3d3b6 (patch)
treea41b36f73a514fc5dc211242b26b1d0799feb2eb
parent46bdf6ea2b1e10c2f4d2fae214ecc81b188fa981 (diff)
downloadnextcloud-server-af707fba41634b70115d47de86efe2ce2bf3d3b6.tar.gz
nextcloud-server-af707fba41634b70115d47de86efe2ce2bf3d3b6.zip
use the query builder instead of raw sql statements
-rw-r--r--lib/private/Authentication/Token/DefaultTokenMapper.php45
-rw-r--r--tests/lib/authentication/token/defaulttokenmappertest.php35
2 files changed, 51 insertions, 29 deletions
diff --git a/lib/private/Authentication/Token/DefaultTokenMapper.php b/lib/private/Authentication/Token/DefaultTokenMapper.php
index f4d979183e6..18adbe48d78 100644
--- a/lib/private/Authentication/Token/DefaultTokenMapper.php
+++ b/lib/private/Authentication/Token/DefaultTokenMapper.php
@@ -24,6 +24,7 @@ namespace OC\Authentication\Token;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Mapper;
+use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class DefaultTokenMapper extends Mapper {
@@ -38,24 +39,25 @@ class DefaultTokenMapper extends Mapper {
* @param string $token
*/
public function invalidate($token) {
- $sql = 'DELETE FROM `' . $this->getTableName() . '` '
- . 'WHERE `token` = ?';
- return $this->execute($sql, [
- $token
- ]);
+ $qb = $this->db->getQueryBuilder();
+ $qb->delete('authtoken')
+ ->andWhere($qb->expr()->eq('token', $qb->createParameter('token')))
+ ->setParameter('token', $token)
+ ->execute();
}
/**
* @param int $olderThan
*/
public function invalidateOld($olderThan) {
- $sql = 'DELETE FROM `' . $this->getTableName() . '` '
- . 'WHERE `last_activity` < ? '
- . 'AND `type` = ?';
- $this->execute($sql, [
- $olderThan,
- IToken::TEMPORARY_TOKEN,
- ]);
+ /* @var $qb IQueryBuilder */
+ $qb = $this->db->getQueryBuilder();
+ $qb->delete('authtoken')
+ ->where($qb->expr()->lt('last_activity', $qb->createParameter('last_activity')))
+ ->andWhere($qb->expr()->eq('type', $qb->createParameter('type')))
+ ->setParameter('last_activity', $olderThan, IQueryBuilder::PARAM_INT)
+ ->setParameter('type', IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT)
+ ->execute();
}
/**
@@ -66,12 +68,19 @@ class DefaultTokenMapper extends Mapper {
* @return DefaultToken
*/
public function getToken($token) {
- $sql = 'SELECT `id`, `uid`, `password`, `name`, `token`, `last_activity` '
- . 'FROM `' . $this->getTableName() . '` '
- . 'WHERE `token` = ?';
- return $this->findEntity($sql, [
- $token
- ]);
+ /* @var $qb IQueryBuilder */
+ $qb = $this->db->getQueryBuilder();
+ $result = $qb->select('id', 'uid', 'password', 'name', 'type', 'token', 'last_activity')
+ ->from('authtoken')
+ ->where($qb->expr()->eq('token', $qb->createParameter('token')))
+ ->setParameter('token', $token)
+ ->execute();
+
+ $data = $result->fetch();
+ if ($data === false) {
+ throw new DoesNotExistException('token does not exist');
+ }
+ return DefaultToken::fromRow($data);
}
}
diff --git a/tests/lib/authentication/token/defaulttokenmappertest.php b/tests/lib/authentication/token/defaulttokenmappertest.php
index b77bf31aa48..9a21e143fb4 100644
--- a/tests/lib/authentication/token/defaulttokenmappertest.php
+++ b/tests/lib/authentication/token/defaulttokenmappertest.php
@@ -23,7 +23,9 @@
namespace Test\Authentication\Token;
use OC;
+use OC\Authentication\Token\DefaultToken;
use OC\Authentication\Token\DefaultTokenMapper;
+use OC\Authentication\Token\IToken;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Test\TestCase;
@@ -38,11 +40,13 @@ class DefaultTokenMapperTest extends TestCase {
/** @var DefaultTokenMapper */
private $mapper;
private $dbConnection;
+ private $time;
protected function setUp() {
parent::setUp();
- $this->dbConnection = \OC::$server->getDatabaseConnection();
+ $this->dbConnection = OC::$server->getDatabaseConnection();
+ $this->time = time();
$this->resetDatabase();
$this->mapper = new DefaultTokenMapper($this->dbConnection);
@@ -56,24 +60,24 @@ class DefaultTokenMapperTest extends TestCase {
'password' => $qb->createNamedParameter('a75c7116460c082912d8f6860a850904|3nz5qbG1nNSLLi6V|c55365a0e54cfdfac4a175bcf11a7612aea74492277bba6e5d96a24497fa9272488787cb2f3ad34d8b9b8060934fce02f008d371df3ff3848f4aa61944851ff0'),
'name' => $qb->createNamedParameter('Firefox on Linux'),
'token' => $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'),
- 'type' => $qb->createNamedParameter(OC\Authentication\Token\IToken::TEMPORARY_TOKEN),
- 'last_activity' => $qb->createNamedParameter(time() - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
+ 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
+ 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
])->execute();
$qb->insert('authtoken')->values([
'uid' => $qb->createNamedParameter('user2'),
'password' => $qb->createNamedParameter('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'),
'name' => $qb->createNamedParameter('Firefox on Android'),
'token' => $qb->createNamedParameter('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'),
- 'type' => $qb->createNamedParameter(OC\Authentication\Token\IToken::TEMPORARY_TOKEN),
- 'last_activity' => $qb->createNamedParameter(time() - 60 * 60 * 24 * 3, IQueryBuilder::PARAM_INT), // Three days ago
+ 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
+ 'last_activity' => $qb->createNamedParameter($this->time - 60 * 60 * 24 * 3, IQueryBuilder::PARAM_INT), // Three days ago
])->execute();
$qb->insert('authtoken')->values([
'uid' => $qb->createNamedParameter('user1'),
'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
'name' => $qb->createNamedParameter('Iceweasel on Linux'),
'token' => $qb->createNamedParameter('47af8697ba590fb82579b5f1b3b6e8066773a62100abbe0db09a289a62f5d980dc300fa3d98b01d7228468d1ab05c1aa14c8d14bd5b6eee9cdf1ac14864680c3'),
- 'type' => $qb->createNamedParameter(OC\Authentication\Token\IToken::TEMPORARY_TOKEN),
- 'last_activity' => $qb->createNamedParameter(time() - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
+ 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
+ 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
])->execute();
}
@@ -83,7 +87,6 @@ class DefaultTokenMapperTest extends TestCase {
->from('authtoken')
->execute()
->fetch();
- print_r($result);
return (int) $result['count'];
}
@@ -104,7 +107,7 @@ class DefaultTokenMapperTest extends TestCase {
}
public function testInvalidateOld() {
- $olderThan = time() - 60 * 60; // One hour
+ $olderThan = $this->time - 60 * 60; // One hour
$this->mapper->invalidateOld($olderThan);
@@ -113,10 +116,20 @@ class DefaultTokenMapperTest extends TestCase {
public function testGetToken() {
$token = '1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b';
+ $token = new DefaultToken();
+ $token->setUid('user2');
+ $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
+ $token->setName('Firefox on Android');
+ $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
+ $token->setType(IToken::TEMPORARY_TOKEN);
+ $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
- $dbToken = $this->mapper->getToken($token);
+ $dbToken = $this->mapper->getToken($token->getToken());
- $this->assertNotNull($dbToken);
+ $token->setId($dbToken->getId()); // We don't know the ID
+ $token->resetUpdatedFields();
+
+ $this->assertEquals($token, $dbToken);
}
/**