diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2018-12-06 15:35:57 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2018-12-07 09:13:16 +0100 |
commit | a670a9e443aca232e2a8986aae15eac38dc8499f (patch) | |
tree | 628c834fe086ba1976dd8de085fa847739f0376e /apps/oauth2/lib | |
parent | 14dc979ec8446322b52f443e0e303c13aff5da56 (diff) | |
download | nextcloud-server-a670a9e443aca232e2a8986aae15eac38dc8499f.tar.gz nextcloud-server-a670a9e443aca232e2a8986aae15eac38dc8499f.zip |
Move OAuth2 db code to QBMapper and make it strict
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/oauth2/lib')
-rw-r--r-- | apps/oauth2/lib/Db/AccessTokenMapper.php | 23 | ||||
-rw-r--r-- | apps/oauth2/lib/Db/ClientMapper.php | 42 |
2 files changed, 35 insertions, 30 deletions
diff --git a/apps/oauth2/lib/Db/AccessTokenMapper.php b/apps/oauth2/lib/Db/AccessTokenMapper.php index 2661c853372..e50ea9bbd3e 100644 --- a/apps/oauth2/lib/Db/AccessTokenMapper.php +++ b/apps/oauth2/lib/Db/AccessTokenMapper.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch> * @@ -22,11 +23,12 @@ namespace OCA\OAuth2\Db; use OCA\OAuth2\Exceptions\AccessTokenNotFoundException; -use OCP\AppFramework\Db\Mapper; +use OCP\AppFramework\Db\IMapperException; +use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; -class AccessTokenMapper extends Mapper { +class AccessTokenMapper extends QBMapper { /** * @param IDBConnection $db @@ -40,19 +42,20 @@ class AccessTokenMapper extends Mapper { * @return AccessToken * @throws AccessTokenNotFoundException */ - public function getByCode($code) { + public function getByCode(string $code): AccessToken { $qb = $this->db->getQueryBuilder(); $qb ->select('*') ->from($this->tableName) ->where($qb->expr()->eq('hashed_code', $qb->createNamedParameter(hash('sha512', $code)))); - $result = $qb->execute(); - $row = $result->fetch(); - $result->closeCursor(); - if($row === false) { - throw new AccessTokenNotFoundException(); + + try { + $token = $this->findEntity($qb); + } catch (IMapperException $e) { + throw new AccessTokenNotFoundException('Could not find access token', 0, $e); } - return AccessToken::fromRow($row); + + return $token; } /** @@ -60,7 +63,7 @@ class AccessTokenMapper extends Mapper { * * @param int $id */ - public function deleteByClientId($id) { + public function deleteByClientId(int $id) { $qb = $this->db->getQueryBuilder(); $qb ->delete($this->tableName) diff --git a/apps/oauth2/lib/Db/ClientMapper.php b/apps/oauth2/lib/Db/ClientMapper.php index 9df07e2789f..bfd3d949d1c 100644 --- a/apps/oauth2/lib/Db/ClientMapper.php +++ b/apps/oauth2/lib/Db/ClientMapper.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch> * @@ -22,11 +23,12 @@ namespace OCA\OAuth2\Db; use OCA\OAuth2\Exceptions\ClientNotFoundException; -use OCP\AppFramework\Db\Mapper; +use OCP\AppFramework\Db\IMapperException; +use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; -class ClientMapper extends Mapper { +class ClientMapper extends QBMapper { /** * @param IDBConnection $db @@ -40,50 +42,50 @@ class ClientMapper extends Mapper { * @return Client * @throws ClientNotFoundException */ - public function getByIdentifier($clientIdentifier) { + public function getByIdentifier(string $clientIdentifier): Client { $qb = $this->db->getQueryBuilder(); $qb ->select('*') ->from($this->tableName) ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier))); - $result = $qb->execute(); - $row = $result->fetch(); - $result->closeCursor(); - if($row === false) { - throw new ClientNotFoundException(); + + try { + $client = $this->findEntity($qb); + } catch (IMapperException $e) { + throw new ClientNotFoundException('could not find client '.$clientIdentifier, 0, $e); } - return Client::fromRow($row); + return $client; } /** - * @param string $uid internal uid of the client + * @param int $id internal id of the client * @return Client * @throws ClientNotFoundException */ - public function getByUid($uid) { + public function getByUid(int $id): Client { $qb = $this->db->getQueryBuilder(); $qb ->select('*') ->from($this->tableName) - ->where($qb->expr()->eq('id', $qb->createNamedParameter($uid, IQueryBuilder::PARAM_INT))); - $result = $qb->execute(); - $row = $result->fetch(); - $result->closeCursor(); - if($row === false) { - throw new ClientNotFoundException(); + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); + + try { + $client = $this->findEntity($qb); + } catch (IMapperException $e) { + throw new ClientNotFoundException('could not find client with id '.$id, 0, $e); } - return Client::fromRow($row); + return $client; } /** * @return Client[] */ - public function getClients() { + public function getClients(): array { $qb = $this->db->getQueryBuilder(); $qb ->select('*') ->from($this->tableName); - return $this->findEntities($qb->getSQL()); + return $this->findEntities($qb); } } |