Backend.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib\Backend;
  8. use OCA\Files_External\Lib\Auth\AuthMechanism;
  9. use OCA\Files_External\Lib\DependencyTrait;
  10. use OCA\Files_External\Lib\FrontendDefinitionTrait;
  11. use OCA\Files_External\Lib\IdentifierTrait;
  12. use OCA\Files_External\Lib\IFrontendDefinition;
  13. use OCA\Files_External\Lib\IIdentifier;
  14. use OCA\Files_External\Lib\PriorityTrait;
  15. use OCA\Files_External\Lib\StorageConfig;
  16. use OCA\Files_External\Lib\StorageModifierTrait;
  17. use OCA\Files_External\Lib\VisibilityTrait;
  18. use OCP\Files\Storage\IStorage;
  19. /**
  20. * Storage backend
  21. *
  22. * A backend can have services injected during construction,
  23. * such as \OCP\IDB for database operations. This allows a backend
  24. * to perform advanced operations based on provided information.
  25. *
  26. * An authentication scheme defines the parameter interface, common to the
  27. * storage implementation, the backend and the authentication mechanism.
  28. * A storage implementation expects parameters according to the authentication
  29. * scheme, which are provided from the authentication mechanism.
  30. *
  31. * This class uses the following traits:
  32. * - VisibilityTrait
  33. * Restrict usage to admin-only/none
  34. * - FrontendDefinitionTrait
  35. * Specify configuration parameters and other definitions
  36. * - PriorityTrait
  37. * Allow objects to prioritize over others with the same mountpoint
  38. * - DependencyTrait
  39. * The object requires certain dependencies to be met
  40. * - StorageModifierTrait
  41. * Object can affect storage mounting
  42. */
  43. class Backend implements \JsonSerializable, IIdentifier, IFrontendDefinition {
  44. use VisibilityTrait;
  45. use FrontendDefinitionTrait;
  46. use PriorityTrait;
  47. use DependencyTrait;
  48. use StorageModifierTrait;
  49. use IdentifierTrait;
  50. /** @var string storage class */
  51. private $storageClass;
  52. /** @var array 'scheme' => true, supported authentication schemes */
  53. private $authSchemes = [];
  54. /** @var AuthMechanism|callable authentication mechanism fallback */
  55. private $legacyAuthMechanism;
  56. /**
  57. * @return class-string<IStorage>
  58. */
  59. public function getStorageClass() {
  60. return $this->storageClass;
  61. }
  62. /**
  63. * @param string $class
  64. * @return $this
  65. */
  66. public function setStorageClass($class) {
  67. $this->storageClass = $class;
  68. return $this;
  69. }
  70. /**
  71. * @return array
  72. */
  73. public function getAuthSchemes() {
  74. if (empty($this->authSchemes)) {
  75. return [AuthMechanism::SCHEME_NULL => true];
  76. }
  77. return $this->authSchemes;
  78. }
  79. /**
  80. * @param string $scheme
  81. * @return self
  82. */
  83. public function addAuthScheme($scheme) {
  84. $this->authSchemes[$scheme] = true;
  85. return $this;
  86. }
  87. /**
  88. * @param array $parameters storage parameters, for dynamic mechanism selection
  89. * @return AuthMechanism
  90. */
  91. public function getLegacyAuthMechanism(array $parameters = []) {
  92. if (is_callable($this->legacyAuthMechanism)) {
  93. return call_user_func($this->legacyAuthMechanism, $parameters);
  94. }
  95. return $this->legacyAuthMechanism;
  96. }
  97. public function setLegacyAuthMechanism(AuthMechanism $authMechanism): self {
  98. $this->legacyAuthMechanism = $authMechanism;
  99. return $this;
  100. }
  101. /**
  102. * @param callable $callback dynamic auth mechanism selection
  103. */
  104. public function setLegacyAuthMechanismCallback(callable $callback): self {
  105. $this->legacyAuthMechanism = $callback;
  106. return $this;
  107. }
  108. /**
  109. * Serialize into JSON for client-side JS
  110. */
  111. public function jsonSerialize(): array {
  112. $data = $this->jsonSerializeDefinition();
  113. $data += $this->jsonSerializeIdentifier();
  114. $data['backend'] = $data['name']; // legacy compat
  115. $data['priority'] = $this->getPriority();
  116. $data['authSchemes'] = $this->getAuthSchemes();
  117. return $data;
  118. }
  119. /**
  120. * Check if parameters are satisfied in a StorageConfig
  121. *
  122. * @param StorageConfig $storage
  123. * @return bool
  124. */
  125. public function validateStorage(StorageConfig $storage) {
  126. return $this->validateStorageDefinition($storage);
  127. }
  128. }