backend.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @author Robin McCorkell <robin@mccorkell.me.uk>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  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, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files_External\Lib\Backend;
  22. use \OCA\Files_External\Lib\StorageConfig;
  23. use \OCA\Files_External\Lib\VisibilityTrait;
  24. use \OCA\Files_External\Lib\FrontendDefinitionTrait;
  25. use \OCA\Files_External\Lib\PriorityTrait;
  26. use \OCA\Files_External\Lib\DependencyTrait;
  27. use \OCA\Files_External\Lib\StorageModifierTrait;
  28. use \OCA\Files_External\Lib\IdentifierTrait;
  29. use \OCA\Files_External\Lib\Auth\AuthMechanism;
  30. /**
  31. * Storage backend
  32. *
  33. * A backend can have services injected during construction,
  34. * such as \OCP\IDB for database operations. This allows a backend
  35. * to perform advanced operations based on provided information.
  36. *
  37. * An authenication scheme defines the parameter interface, common to the
  38. * storage implementation, the backend and the authentication mechanism.
  39. * A storage implementation expects parameters according to the authentication
  40. * scheme, which are provided from the authentication mechanism.
  41. *
  42. * This class uses the following traits:
  43. * - VisibilityTrait
  44. * Restrict usage to admin-only/none
  45. * - FrontendDefinitionTrait
  46. * Specify configuration parameters and other definitions
  47. * - PriorityTrait
  48. * Allow objects to prioritize over others with the same mountpoint
  49. * - DependencyTrait
  50. * The object requires certain dependencies to be met
  51. * - StorageModifierTrait
  52. * Object can affect storage mounting
  53. */
  54. class Backend implements \JsonSerializable {
  55. use VisibilityTrait;
  56. use FrontendDefinitionTrait;
  57. use PriorityTrait;
  58. use DependencyTrait;
  59. use StorageModifierTrait;
  60. use IdentifierTrait;
  61. /** @var string storage class */
  62. private $storageClass;
  63. /** @var array 'scheme' => true, supported authentication schemes */
  64. private $authSchemes = [];
  65. /** @var AuthMechanism|callable authentication mechanism fallback */
  66. private $legacyAuthMechanism;
  67. /**
  68. * @return string
  69. */
  70. public function getStorageClass() {
  71. return $this->storageClass;
  72. }
  73. /**
  74. * @param string $class
  75. * @return self
  76. */
  77. public function setStorageClass($class) {
  78. $this->storageClass = $class;
  79. return $this;
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function getAuthSchemes() {
  85. if (empty($this->authSchemes)) {
  86. return [AuthMechanism::SCHEME_NULL => true];
  87. }
  88. return $this->authSchemes;
  89. }
  90. /**
  91. * @param string $scheme
  92. * @return self
  93. */
  94. public function addAuthScheme($scheme) {
  95. $this->authSchemes[$scheme] = true;
  96. return $this;
  97. }
  98. /**
  99. * @param array $parameters storage parameters, for dynamic mechanism selection
  100. * @return AuthMechanism
  101. */
  102. public function getLegacyAuthMechanism(array $parameters = []) {
  103. if (is_callable($this->legacyAuthMechanism)) {
  104. return call_user_func($this->legacyAuthMechanism, $parameters);
  105. }
  106. return $this->legacyAuthMechanism;
  107. }
  108. /**
  109. * @param AuthMechanism $authMechanism
  110. * @return self
  111. */
  112. public function setLegacyAuthMechanism(AuthMechanism $authMechanism) {
  113. $this->legacyAuthMechanism = $authMechanism;
  114. return $this;
  115. }
  116. /**
  117. * @param callable $callback dynamic auth mechanism selection
  118. * @return self
  119. */
  120. public function setLegacyAuthMechanismCallback(callable $callback) {
  121. $this->legacyAuthMechanism = $callback;
  122. }
  123. /**
  124. * Serialize into JSON for client-side JS
  125. *
  126. * @return array
  127. */
  128. public function jsonSerialize() {
  129. $data = $this->jsonSerializeDefinition();
  130. $data += $this->jsonSerializeIdentifier();
  131. $data['backend'] = $data['name']; // legacy compat
  132. $data['priority'] = $this->getPriority();
  133. $data['authSchemes'] = $this->getAuthSchemes();
  134. return $data;
  135. }
  136. /**
  137. * Check if parameters are satisfied in a StorageConfig
  138. *
  139. * @param StorageConfig $storage
  140. * @return bool
  141. */
  142. public function validateStorage(StorageConfig $storage) {
  143. return $this->validateStorageDefinition($storage);
  144. }
  145. }