Backend.php 4.6 KB

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