From af707fba41634b70115d47de86efe2ce2bf3d3b6 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Sun, 8 May 2016 17:41:37 +0200 Subject: use the query builder instead of raw sql statements --- .../Authentication/Token/DefaultTokenMapper.php | 45 +++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'lib/private') 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); } } -- cgit v1.2.3