1
0

BackendService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-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\Service;
  8. use OCA\Files_External\Config\IConfigHandler;
  9. use OCA\Files_External\Lib\Auth\AuthMechanism;
  10. use OCA\Files_External\Lib\Backend\Backend;
  11. use OCA\Files_External\Lib\Config\IAuthMechanismProvider;
  12. use OCA\Files_External\Lib\Config\IBackendProvider;
  13. use OCP\EventDispatcher\GenericEvent;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\IConfig;
  16. /**
  17. * Service class to manage backend definitions
  18. */
  19. class BackendService {
  20. /** Visibility constants for VisibilityTrait */
  21. public const VISIBILITY_NONE = 0;
  22. public const VISIBILITY_PERSONAL = 1;
  23. public const VISIBILITY_ADMIN = 2;
  24. //const VISIBILITY_ALIENS = 4;
  25. public const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN
  26. /** Priority constants for PriorityTrait */
  27. public const PRIORITY_DEFAULT = 100;
  28. /** @var bool */
  29. private $userMountingAllowed = true;
  30. /** @var string[] */
  31. private $userMountingBackends = [];
  32. /** @var Backend[] */
  33. private $backends = [];
  34. /** @var IBackendProvider[] */
  35. private $backendProviders = [];
  36. /** @var AuthMechanism[] */
  37. private $authMechanisms = [];
  38. /** @var IAuthMechanismProvider[] */
  39. private $authMechanismProviders = [];
  40. /** @var callable[] */
  41. private $configHandlerLoaders = [];
  42. private $configHandlers = [];
  43. /**
  44. * @param IConfig $config
  45. */
  46. public function __construct(
  47. protected IConfig $config,
  48. ) {
  49. // Load config values
  50. if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') {
  51. $this->userMountingAllowed = false;
  52. }
  53. $this->userMountingBackends = explode(',',
  54. $this->config->getAppValue('files_external', 'user_mounting_backends', '')
  55. );
  56. // if no backend is in the list an empty string is in the array and user mounting is disabled
  57. if ($this->userMountingBackends === ['']) {
  58. $this->userMountingAllowed = false;
  59. }
  60. }
  61. /**
  62. * Register a backend provider
  63. *
  64. * @since 9.1.0
  65. * @param IBackendProvider $provider
  66. */
  67. public function registerBackendProvider(IBackendProvider $provider) {
  68. $this->backendProviders[] = $provider;
  69. }
  70. private function callForRegistrations() {
  71. static $eventSent = false;
  72. if (!$eventSent) {
  73. \OC::$server->get(IEventDispatcher::class)->dispatch(
  74. 'OCA\\Files_External::loadAdditionalBackends',
  75. new GenericEvent()
  76. );
  77. $eventSent = true;
  78. }
  79. }
  80. private function loadBackendProviders() {
  81. $this->callForRegistrations();
  82. foreach ($this->backendProviders as $provider) {
  83. $this->registerBackends($provider->getBackends());
  84. }
  85. $this->backendProviders = [];
  86. }
  87. /**
  88. * Register an auth mechanism provider
  89. *
  90. * @since 9.1.0
  91. * @param IAuthMechanismProvider $provider
  92. */
  93. public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) {
  94. $this->authMechanismProviders[] = $provider;
  95. }
  96. private function loadAuthMechanismProviders() {
  97. $this->callForRegistrations();
  98. foreach ($this->authMechanismProviders as $provider) {
  99. $this->registerAuthMechanisms($provider->getAuthMechanisms());
  100. }
  101. $this->authMechanismProviders = [];
  102. }
  103. /**
  104. * Register a backend
  105. *
  106. * @deprecated 9.1.0 use registerBackendProvider()
  107. * @param Backend $backend
  108. */
  109. public function registerBackend(Backend $backend) {
  110. if (!$this->isAllowedUserBackend($backend)) {
  111. $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL);
  112. }
  113. foreach ($backend->getIdentifierAliases() as $alias) {
  114. $this->backends[$alias] = $backend;
  115. }
  116. }
  117. /**
  118. * @deprecated 9.1.0 use registerBackendProvider()
  119. * @param Backend[] $backends
  120. */
  121. public function registerBackends(array $backends) {
  122. foreach ($backends as $backend) {
  123. $this->registerBackend($backend);
  124. }
  125. }
  126. /**
  127. * Register an authentication mechanism
  128. *
  129. * @deprecated 9.1.0 use registerAuthMechanismProvider()
  130. * @param AuthMechanism $authMech
  131. */
  132. public function registerAuthMechanism(AuthMechanism $authMech) {
  133. if (!$this->isAllowedAuthMechanism($authMech)) {
  134. $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL);
  135. }
  136. foreach ($authMech->getIdentifierAliases() as $alias) {
  137. $this->authMechanisms[$alias] = $authMech;
  138. }
  139. }
  140. /**
  141. * @deprecated 9.1.0 use registerAuthMechanismProvider()
  142. * @param AuthMechanism[] $mechanisms
  143. */
  144. public function registerAuthMechanisms(array $mechanisms) {
  145. foreach ($mechanisms as $mechanism) {
  146. $this->registerAuthMechanism($mechanism);
  147. }
  148. }
  149. /**
  150. * Get all backends
  151. *
  152. * @return Backend[]
  153. */
  154. public function getBackends() {
  155. $this->loadBackendProviders();
  156. // only return real identifiers, no aliases
  157. $backends = [];
  158. foreach ($this->backends as $backend) {
  159. $backends[$backend->getIdentifier()] = $backend;
  160. }
  161. return $backends;
  162. }
  163. /**
  164. * Get all available backends
  165. *
  166. * @return Backend[]
  167. */
  168. public function getAvailableBackends() {
  169. return array_filter($this->getBackends(), function ($backend) {
  170. return !$backend->checkDependencies();
  171. });
  172. }
  173. /**
  174. * @param string $identifier
  175. * @return Backend|null
  176. */
  177. public function getBackend($identifier) {
  178. $this->loadBackendProviders();
  179. if (isset($this->backends[$identifier])) {
  180. return $this->backends[$identifier];
  181. }
  182. return null;
  183. }
  184. /**
  185. * Get all authentication mechanisms
  186. *
  187. * @return AuthMechanism[]
  188. */
  189. public function getAuthMechanisms() {
  190. $this->loadAuthMechanismProviders();
  191. // only return real identifiers, no aliases
  192. $mechanisms = [];
  193. foreach ($this->authMechanisms as $mechanism) {
  194. $mechanisms[$mechanism->getIdentifier()] = $mechanism;
  195. }
  196. return $mechanisms;
  197. }
  198. /**
  199. * Get all authentication mechanisms for schemes
  200. *
  201. * @param string[] $schemes
  202. * @return AuthMechanism[]
  203. */
  204. public function getAuthMechanismsByScheme(array $schemes) {
  205. return array_filter($this->getAuthMechanisms(), function ($authMech) use ($schemes) {
  206. return in_array($authMech->getScheme(), $schemes, true);
  207. });
  208. }
  209. /**
  210. * @param string $identifier
  211. * @return AuthMechanism|null
  212. */
  213. public function getAuthMechanism($identifier) {
  214. $this->loadAuthMechanismProviders();
  215. if (isset($this->authMechanisms[$identifier])) {
  216. return $this->authMechanisms[$identifier];
  217. }
  218. return null;
  219. }
  220. /**
  221. * @return bool
  222. */
  223. public function isUserMountingAllowed() {
  224. return $this->userMountingAllowed;
  225. }
  226. /**
  227. * Check a backend if a user is allowed to mount it
  228. *
  229. * @param Backend $backend
  230. * @return bool
  231. */
  232. protected function isAllowedUserBackend(Backend $backend) {
  233. if ($this->userMountingAllowed &&
  234. array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends)
  235. ) {
  236. return true;
  237. }
  238. return false;
  239. }
  240. /**
  241. * Check an authentication mechanism if a user is allowed to use it
  242. *
  243. * @param AuthMechanism $authMechanism
  244. * @return bool
  245. */
  246. protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) {
  247. return true; // not implemented
  248. }
  249. /**
  250. * registers a configuration handler
  251. *
  252. * The function of the provided $placeholder is mostly to act a sorting
  253. * criteria, so longer placeholders are replaced first. This avoids
  254. * "$user" overwriting parts of "$userMail" and "$userLang", for example.
  255. * The provided value should not contain the $ prefix, only a-z0-9 are
  256. * allowed. Upper case letters are lower cased, the replacement is case-
  257. * insensitive.
  258. *
  259. * The configHandlerLoader should just instantiate the handler on demand.
  260. * For now all handlers are instantiated when a mount is loaded, independent
  261. * of whether the placeholder is present or not. This may change in future.
  262. *
  263. * @since 16.0.0
  264. */
  265. public function registerConfigHandler(string $placeholder, callable $configHandlerLoader) {
  266. $placeholder = trim(strtolower($placeholder));
  267. if (!(bool)\preg_match('/^[a-z0-9]*$/', $placeholder)) {
  268. throw new \RuntimeException(sprintf(
  269. 'Invalid placeholder %s, only [a-z0-9] are allowed', $placeholder
  270. ));
  271. }
  272. if ($placeholder === '') {
  273. throw new \RuntimeException('Invalid empty placeholder');
  274. }
  275. if (isset($this->configHandlerLoaders[$placeholder]) || isset($this->configHandlers[$placeholder])) {
  276. throw new \RuntimeException(sprintf('A handler is already registered for %s', $placeholder));
  277. }
  278. $this->configHandlerLoaders[$placeholder] = $configHandlerLoader;
  279. }
  280. protected function loadConfigHandlers():void {
  281. $this->callForRegistrations();
  282. $newLoaded = false;
  283. foreach ($this->configHandlerLoaders as $placeholder => $loader) {
  284. $handler = $loader();
  285. if (!$handler instanceof IConfigHandler) {
  286. throw new \RuntimeException(sprintf(
  287. 'Handler for %s is not an instance of IConfigHandler', $placeholder
  288. ));
  289. }
  290. $this->configHandlers[$placeholder] = $handler;
  291. $newLoaded = true;
  292. }
  293. $this->configHandlerLoaders = [];
  294. if ($newLoaded) {
  295. // ensure those with longest placeholders come first,
  296. // to avoid substring matches
  297. uksort($this->configHandlers, function ($phA, $phB) {
  298. return strlen($phB) <=> strlen($phA);
  299. });
  300. }
  301. }
  302. /**
  303. * @since 16.0.0
  304. */
  305. public function getConfigHandlers() {
  306. $this->loadConfigHandlers();
  307. return $this->configHandlers;
  308. }
  309. }