BackendService.php 9.1 KB

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