ClientMapper.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\OAuth2\Db;
  22. use OCA\OAuth2\Exceptions\ClientNotFoundException;
  23. use OCP\AppFramework\Db\Mapper;
  24. use OCP\DB\QueryBuilder\IQueryBuilder;
  25. use OCP\IDBConnection;
  26. class ClientMapper extends Mapper {
  27. /**
  28. * @param IDBConnection $db
  29. */
  30. public function __construct(IDBConnection $db) {
  31. parent::__construct($db, 'oauth2_clients');
  32. }
  33. /**
  34. * @param string $clientIdentifier
  35. * @return Client
  36. * @throws ClientNotFoundException
  37. */
  38. public function getByIdentifier($clientIdentifier) {
  39. $qb = $this->db->getQueryBuilder();
  40. $qb
  41. ->select('*')
  42. ->from($this->tableName)
  43. ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier)));
  44. $result = $qb->execute();
  45. $row = $result->fetch();
  46. $result->closeCursor();
  47. if($row === false) {
  48. throw new ClientNotFoundException();
  49. }
  50. return Client::fromRow($row);
  51. }
  52. /**
  53. * @param string $uid internal uid of the client
  54. * @return Client
  55. * @throws ClientNotFoundException
  56. */
  57. public function getByUid($uid) {
  58. $qb = $this->db->getQueryBuilder();
  59. $qb
  60. ->select('*')
  61. ->from($this->tableName)
  62. ->where($qb->expr()->eq('id', $qb->createNamedParameter($uid, IQueryBuilder::PARAM_INT)));
  63. $result = $qb->execute();
  64. $row = $result->fetch();
  65. $result->closeCursor();
  66. if($row === false) {
  67. throw new ClientNotFoundException();
  68. }
  69. return Client::fromRow($row);
  70. }
  71. /**
  72. * @return Client[]
  73. */
  74. public function getClients() {
  75. $qb = $this->db->getQueryBuilder();
  76. $qb
  77. ->select('*')
  78. ->from($this->tableName);
  79. return $this->findEntities($qb->getSQL());
  80. }
  81. }