2017-05-04 17:46:59 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-12-06 09:35:57 -05:00
|
|
|
declare(strict_types=1);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2017-05-04 17:46:59 -04:00
|
|
|
/**
|
2024-05-30 14:13:41 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-05-04 17:46:59 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\OAuth2\Db;
|
|
|
|
|
|
2017-05-18 13:09:59 -04:00
|
|
|
use OCA\OAuth2\Exceptions\ClientNotFoundException;
|
2018-12-06 09:35:57 -05:00
|
|
|
use OCP\AppFramework\Db\IMapperException;
|
|
|
|
|
use OCP\AppFramework\Db\QBMapper;
|
2017-05-12 10:14:32 -04:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2017-05-04 17:46:59 -04:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
2021-03-11 08:33:15 -05:00
|
|
|
/**
|
|
|
|
|
* @template-extends QBMapper<Client>
|
|
|
|
|
*/
|
2018-12-06 09:35:57 -05:00
|
|
|
class ClientMapper extends QBMapper {
|
2017-05-04 17:46:59 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param IDBConnection $db
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(IDBConnection $db) {
|
|
|
|
|
parent::__construct($db, 'oauth2_clients');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $clientIdentifier
|
|
|
|
|
* @return Client
|
2017-05-18 13:09:59 -04:00
|
|
|
* @throws ClientNotFoundException
|
2017-05-04 17:46:59 -04:00
|
|
|
*/
|
2018-12-06 09:35:57 -05:00
|
|
|
public function getByIdentifier(string $clientIdentifier): Client {
|
2017-05-04 17:46:59 -04:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb
|
|
|
|
|
->select('*')
|
|
|
|
|
->from($this->tableName)
|
2017-05-04 17:55:38 -04:00
|
|
|
->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier)));
|
2018-12-06 09:35:57 -05:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$client = $this->findEntity($qb);
|
|
|
|
|
} catch (IMapperException $e) {
|
|
|
|
|
throw new ClientNotFoundException('could not find client '.$clientIdentifier, 0, $e);
|
2017-05-12 06:43:22 -04:00
|
|
|
}
|
2018-12-06 09:35:57 -05:00
|
|
|
return $client;
|
2017-05-04 17:46:59 -04:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 10:14:32 -04:00
|
|
|
/**
|
2018-12-06 09:35:57 -05:00
|
|
|
* @param int $id internal id of the client
|
2017-05-12 10:14:32 -04:00
|
|
|
* @return Client
|
2017-05-18 13:09:59 -04:00
|
|
|
* @throws ClientNotFoundException
|
2017-05-12 10:14:32 -04:00
|
|
|
*/
|
2018-12-06 09:35:57 -05:00
|
|
|
public function getByUid(int $id): Client {
|
2017-05-12 10:14:32 -04:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb
|
|
|
|
|
->select('*')
|
|
|
|
|
->from($this->tableName)
|
2018-12-06 09:35:57 -05:00
|
|
|
->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);
|
2017-05-12 10:14:32 -04:00
|
|
|
}
|
2018-12-06 09:35:57 -05:00
|
|
|
return $client;
|
2017-05-12 10:14:32 -04:00
|
|
|
}
|
|
|
|
|
|
2017-05-04 17:46:59 -04:00
|
|
|
/**
|
|
|
|
|
* @return Client[]
|
|
|
|
|
*/
|
2018-12-06 09:35:57 -05:00
|
|
|
public function getClients(): array {
|
2017-05-04 17:46:59 -04:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb
|
|
|
|
|
->select('*')
|
|
|
|
|
->from($this->tableName);
|
|
|
|
|
|
2018-12-06 09:35:57 -05:00
|
|
|
return $this->findEntities($qb);
|
2017-05-04 17:46:59 -04:00
|
|
|
}
|
|
|
|
|
}
|