You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.2 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Remote\Api;
  25. use GuzzleHttp\Exception\ClientException;
  26. use OC\ForbiddenException;
  27. use OC\Remote\User;
  28. use OCP\API;
  29. use OCP\Remote\Api\ICapabilitiesApi;
  30. use OCP\Remote\Api\IUserApi;
  31. class OCS extends ApiBase implements ICapabilitiesApi, IUserApi {
  32. /**
  33. * @param string $method
  34. * @param string $url
  35. * @param array $body
  36. * @param array $query
  37. * @param array $headers
  38. * @return array
  39. * @throws ForbiddenException
  40. * @throws NotFoundException
  41. * @throws \Exception
  42. */
  43. protected function request($method, $url, array $body = [], array $query = [], array $headers = []) {
  44. try {
  45. $response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true);
  46. } catch (ClientException $e) {
  47. if ($e->getResponse()->getStatusCode() === 404) {
  48. throw new NotFoundException();
  49. } elseif ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) {
  50. throw new ForbiddenException();
  51. } else {
  52. throw $e;
  53. }
  54. }
  55. if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) {
  56. throw new \Exception('Invalid ocs response');
  57. }
  58. if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) {
  59. throw new ForbiddenException();
  60. }
  61. if ($response['ocs']['meta']['statuscode'] === API::RESPOND_NOT_FOUND) {
  62. throw new NotFoundException();
  63. }
  64. if ($response['ocs']['meta']['status'] !== 'ok') {
  65. throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']);
  66. }
  67. return $response['ocs']['data'];
  68. }
  69. /**
  70. * @param array $data
  71. * @param string $type
  72. * @param string[] $keys
  73. * @throws \Exception
  74. */
  75. private function checkResponseArray(array $data, $type, array $keys) {
  76. foreach ($keys as $key) {
  77. if (!array_key_exists($key, $data)) {
  78. throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found');
  79. }
  80. }
  81. }
  82. public function getUser($userId) {
  83. $result = $this->request('get', 'cloud/users/' . $userId);
  84. $this->checkResponseArray($result, 'user', User::EXPECTED_KEYS);
  85. return new User($result);
  86. }
  87. /**
  88. * @return array The capabilities in the form of [$appId => [$capability => $value]]
  89. */
  90. public function getCapabilities() {
  91. $result = $this->request('get', 'cloud/capabilities');
  92. return $result['capabilities'];
  93. }
  94. }